I am using an express server with GraphQL subscriptions and subscriptions-transport-ws.
I have set up the subscription with a given channel:
...
const subscriptionManager = new SubscriptionManager({
schema: executableSchema,
pubsub: pubsub,
setupFunctions: {
testRunChanged: (options, args) => {
return {
testRunChangedChannel: {
filter: (testRun) => {
return testRun.id === args.testRunId;
}
},
};
},
},
});
...
After a mutation is received a process is started on the server where the database entry of the test run is updated when finished. Now when the update promise for the database action passes the client should be informed.
Using the publish functionality from pubsub the subscription manager gets the information about the updated test run:
...
RunningTestDbService.setToFinished(testRun).then(updatedTestRun => {
pubsub.publish("testRunChangedChannel", updatedTestRun);
})
...
After the subscription manager filters the subscriptions depending on the published testRun and the subscribed testRunId the subscription resolver function is called. To update the client i have to fetch the updated test run again.
How can i get the published test run object inside of the subscription resolver function?
The subscription and the resolver look like this:
...
`testRunChanged(testRunId: ID!): TestRun!`
...
Subscription: {
testRunChanged(_, { testRunId }) {
// need to fetch the test run from database again
return TestRunDbService.getTestRunWith(testRunId);
},
},
...
The object used in
publish
method as payload is thenroot
parameter of your subscriptionresolver
method - so in this case this is_
in yourtestRunChanged
resolver function. You should simply doreturn _
.