I attempted to implement a Subscription in my nestjs project with GraphQL, but encountered the following error message:
Cannot return null for non-nullable field Subscription
Below is the code snippet:
//* ~~~~~~~~~~~~~~~~~~~ Subscription ~~~~~~~~~~~~~~~~~~~ */
@Mutation((returns) => Boolean)
testSubscription() {
pubsub.publish('somethingOnTrack', {
somethingOnTrack: 'something',
});
return true;
}
@Subscription((returns) => String)
orderSubscription() {
return pubsub.asyncIterator('somethingOnTrack');
}
This is how I set up the GraphQLModule in app.module
within nestjs:
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile: true,
subscriptions: {
'graphql-ws': true,
'subscriptions-transport-ws': true,
},
}),
I tried utilizing graphql-ws
& subscriptions-transport-ws
Next, I tested it in the graphql:
subscription {
orderSubscription
}
mutation {
testSubscription
}
The Mutation returned the expected result:
{
"data": {
"testSubscription": true
}
}
However, the Subscription triggered an error:
{
"errors": [
{
"message": "Cannot return null for non-nullable field Subscription.orderSubscription.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"orderSubscription"
]
}
],
"data": null
}
It appears that the subscription is unable to receive the payload from the pubsub.publish function. Why am I encountering this issue?