-
When running a subscription operation, if the AsyncIterator errors, then it logs I believe this is due to the onMessage handler for subscription operations not having any error handling: https://github.com/enisdenjo/graphql-ws/blob/master/src/server.ts#L799-L811 For example: const resolvers: Resolvers = {
Subscription: {
notifications: {
resolve: (source: any) => {
if (source instanceof Error) {
throw source;
}
return source;
},
subscribe: () => {
return {
async *[Symbol.asyncIterator]() {
await new Promise((resolve) => setTimeout(resolve, 500));
yield 'Hello world';
// when this error is thrown, the whole websocket closes
throw new Error('boom');
},
};
},
},
},
}; I would've expected throwing the error in Further Information |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 26 replies
-
Are you sure it's the thrown error making problems and not your malformed yield? Please share the full error message. const resolvers: Resolvers = {
Subscription: {
notifications: {
resolve: (source: any) => {
if (source instanceof Error) {
throw source;
}
return source;
},
subscribe: () => {
return {
async *[Symbol.asyncIterator]() {
await new Promise((resolve) => setTimeout(resolve, 500));
- yield 'Hello world';
+ yield { notifications: 'Hello world' };
// when this error is thrown, the whole websocket closes
throw new Error('boom');
},
};
},
},
},
}; |
Beta Was this translation helpful? Give feedback.
-
Seems like this is intentional, please see graphql/graphql-js#1539. |
Beta Was this translation helpful? Give feedback.
-
Please refer to the recipe showcasing how someone may implement a custom subscribe for graceful handling thrown errors with just 15 lines of code. |
Beta Was this translation helpful? Give feedback.
Please refer to the recipe showcasing how someone may implement a custom subscribe for graceful handling thrown errors with just 15 lines of code.