Apollo-server: Unable to determine the channel name dynamically in a subscription resolver

Created on 20 Jul 2018  路  2Comments  路  Source: apollographql/apollo-server

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), } }

馃К subscriptions

Most helpful comment

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: [email protected]
Best regards,
Mihajlo

All 2 comments

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: [email protected]
Best regards,
Mihajlo

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jpcbarros picture jpcbarros  路  3Comments

danilobuerger picture danilobuerger  路  3Comments

dupski picture dupski  路  3Comments

attdona picture attdona  路  3Comments

mathroc picture mathroc  路  3Comments