Apollo GraphQL - Handling onConnect and OnDisconnect events

701 views Asked by At

I'm developing a real-time chat app using Apollo GraphQL, and I want to keep the clients updated about the other client's status.

Mean that I need to do something with the onConnect and OnDisconnect events from the subscriptions object (which defined in app.js) and send a subscription event to the clients whenever a user connected or disconnected.

I could not find an explanation on how to do this, and I would appreciate any help.

1

There are 1 answers

0
loekTheDreamer On

you have to use onDisconnect property with context.initPromise like this:

    onDisconnect: async (webSocket, context) => {
      console.log('what is the context?: ', context); 

      try {
        const initialContext = await context.initPromise;
        if (
          initialContext &&
          typeof initialContext === 'object' &&
          Reflect.has(initialContext, 'user')
        ) {
          console.log('initialContext: ', initialContext);
        }
      } catch (err) {
        console.log('error', err); // TypeError: 
      }
    }
  },

link and link2 for reference.

warning: this is currently working on my local build but not on my heroku production build