I am writing a graphql subscriptions server. If I write a query it is no problem to have resolvers nested one within the other, so the query would look something like this:
query {
messages {
privateMessage {
id
message
userId
}
}
}
So first the messages
resolver is executed, then the privateMessage
resolver is executed.
I would like to know if the same structure is achievable for subscriptions so it would look like this:
subscription {
messages {
privateMessage {
id
message
userId
}
}
}
EDIT:
This is the current root subscription schema I have:
const RootSubscriptions = new GraphQLObjectType({
name: 'RootSubscriptions',
fields: {
privateMessage: {
type: PrivateMessage.type,
resolve: PrivateMessage.resolve,
subscribe: PrivateMessage.subscribe,
},
flaggedMessage: {
type: FlaggedMessage.type,
resolve: FlaggedMessage.resolve,
subscribe: FlaggedMessage.subscribe,
},
teamMessage: {
type: TeamMessage.type,
resolve: TeamMessage.resolve,
subscribe: TeamMessage.subscribe,
},
},
})
I would like it to look like this:
const RootSubscriptions = new GraphQLObjectType({
name: 'RootSubscriptions',
fields: {
messages: {
type: new GraphQLObjectType({
name: 'MessagesSubType',
fields: {
privateMessage: {
type: PrivateMessage.type,
resolve: PrivateMessage.resolve,
subscribe: PrivateMessage.subscribe,
},
flaggedMessage: {
type: FlaggedMessage.type,
resolve: FlaggedMessage.resolve,
subscribe: FlaggedMessage.subscribe,
},
teamMessage: {
type: TeamMessage.type,
resolve: TeamMessage.resolve,
subscribe: TeamMessage.subscribe,
},
}
})
}
},
})
EDIT END
Problem is that I get the messages
subscribe function to run but not the privateMessage
subscribe function to run. Would love to know if it is possible and how to achieve it.
Since I'm writing it with node.js, I would appreciate an example in js, but any pointer to a solution would be helpful.
Thanks in advance!
obj
, and any additional data merged here to hydrate the return sub payload. You could use theinfo
to determine whether you are firing in the correct context. – Greg Brodzik Jul 17 '20 at 23:04