Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to determine the channel name dynamically in a subscription resolver #1385

Closed
serle opened this issue Jul 20, 2018 · 2 comments
Closed

Comments

@serle
Copy link

serle commented Jul 20, 2018

Looking at all the examples the subscription resolver is of the form:

messageCreated: { subscribe: () => pubsub.asyncIterator(MESSAGE_CREATED), }

i.e. it returns an object with a subscribe property. However I need to be able to dynamically set the channel based on the set of parameters coming through from the client, when they subscribe. I would like to request that the subscription resolver be generalized to a function that returns the object with the subscribe property. The resolver should then look like:

messageCreated(variables) { return { subscribe: pubsub.asyncIterator(variables.topic), } }

@serle serle changed the title Feature Request: pass through parameters to subscribe resolver Unable to determine the channel name dynamically in a subscription resolver Jul 31, 2018
@mihajlon97
Copy link

After a couple of days of debugging this, I came up with this solution.
I am using graphql-redis-subscriptions library for Redis PubSub

My GraphQL schema:
type Subscription { updateMarket (event: String): JSON }
Subscription:
export const Subscription = { updateMarket: { subscribe: (model, args, context, info) => { console.log("SUBSCRIBED TO EVENT: " + args.event); return PubSub.asyncIterator(args.event); } },

As you can see, we can pass arguments to subscribe function:
subscribe: (model, args, context, info)
Apollo Team should update their documentation for subscriptions because this works now.

On the frontend GraphQL Query looks like this:
subscription updateMarket($event: String) { updateMarket(event: $event) }

Modules:
"graphql": "^14.0.2",
"apollo-server-express": "^2.1.0",
"graphql-redis-subscriptions": "^2.0.0"

Questions: mihajlo@morpher.io
Best regards,
Mihajlo

@jbaxleyiii
Copy link
Contributor

Thanks very much for reporting this! We think this is a great idea, and would definitely love a PR for the docs. If you (or anyone else) is interested in working on this, that would be great - thanks!

@abernix abernix removed 🚧👷‍♀️👷‍♂️🚧 in triage Issue currently being triaged labels Jul 9, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants