-
Expected Behaviour I expected it to keep the connection when there there's no user interaction with the server Actual Behaviour Debug Information AFAICT, there's nothing returning null on the server-side subscription. I am probably missing the inner specifics of Here are the relevant code snippets: client: // client.ts
const getWsLink = (token: string) =>
new GraphQLWsLink(
createClient({
url: process.env.GRAPHQL_WS_URL,
connectionParams: {
authToken: token,
},
shouldRetry: (closeEvent: CloseEvent) => {
// retries on client unexpected socket closing ( error 1006 )
const UNEXPECTED_CHROMIUM_CLOSE_EVENT = 1006;
return closeEvent.code === UNEXPECTED_CHROMIUM_CLOSE_EVENT;
},
retryAttempts: 10,
})
); //subscription-client.ts
useSubscription(DOPE_SUBSCRIPTION, {
onData: ({ data }) => {
if (data?.data?.dopeSubscription?.message !== undefined) {
doSomethingDope();
}
},
}); server // server.ts
const app = express();
const httpServer = createServer(app);
const wsServer = new WebSocketServer({
server: httpServer,
path: '/dope-subscription-path',
});
const wsContext = async (ctx) => {
const token = ctx.connectionParams.authToken || '';
const user = await getUser(token);
return { user, dataSources };
};
const serverCleanup = useServer({ schema, context: wsContext }, wsServer);
const plugins = [
ApolloServerPluginDrainHttpServer({ httpServer }),
{
async serverWillStart() {
return {
async drainServer() {
await serverCleanup.dispose();
},
};
},
];
const server = new ApolloServer({
schema,
dataSources: () => dataSources,
context,
plugins,
introspection: true,
});
await server.start();
server.applyMiddleware({ app, cors: true, path: '/' });
await httpServer.listen(PORT);
// subsciption-resolver.ts
@Subscription(() => DopeResultType, { topics: DOPE_TOPIC })
async dopeSubscription(
@Ctx() { dataSources, user }: GraphqlContext,
@Root() { message , action }: Payload
): Promise<typeof DopeResultType> {
try {
if (!user) {
return {
message: undefined,
action: undefined,
};
}
// some dope processings
return {
message
action,
};
}
} catch (err) {
return err;
}
} Further Information Librairies used:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The error originates from the subscription resolver in your schema. It could be that the subscription resets, or the browser reconnects the WS connection and then the subscription resolver returns |
Beta Was this translation helpful? Give feedback.
-
For future reference, it was indeed resolver returning null. We weren't gracefully handling user token expiration with Firebase admin SDK.
|
Beta Was this translation helpful? Give feedback.
The error originates from the subscription resolver in your schema. It could be that the subscription resets, or the browser reconnects the WS connection and then the subscription resolver returns
null
instead of the iterator.