问题
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);
},
},
...
回答1:
The object used in publish
method as payload is then root
parameter of your subscription resolver
method - so in this case this is _
in your testRunChanged
resolver function. You should simply do return _
.
来源:https://stackoverflow.com/questions/42651007/graphql-subscriptions-how-to-get-published-object-in-subscription-resolver