-
Within the subscribe method of a resolver, is it possible to find out which websocket initiated the subscription? The reason I ask is because of the use of generator functions and async iterates in the example:
This makes good sense because the iterable set is finite. In a real world scenario the end of the set is typically defined by socket closure hence the request as to whether there is a way I can find out the id of the web socket is in the subscribe function. The other alternative is to use a pubsub iterator following the Apollo server approach:
I find Apollo Server rather huge and indigestible and though there are various npm pubsub packages, none of them fit the bill. If there was access to the web socket in the subscribe function (via a context?), the publishing could be done quite simply with the nodejs eventEmitter. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Exactly, this is the way to do it. You can find the socket in the context extra which you can pass to the GraphQL context: import { WebSocketServer } from 'ws';
import { useServer } from 'graphql-ws/lib/use/ws';
import { schema } from './my-graphql-schema';
const server = new WebSocketServer({
port: 4000,
path: '/graphql',
});
useServer(
{
schema,
// pass the socket to the GraphQL context
context: (ctx) => ({ socket: ctx.extra.socket }),
},
server,
); |
Beta Was this translation helpful? Give feedback.
Exactly, this is the way to do it. You can find the socket in the context extra which you can pass to the GraphQL context: