1

I have setup a Apollo Server and Client with Subscriptions. Everything works as expected in basic setup. I am using Apollo Client's Subscribe method for subscription. Something like below:

graphqlClient.subscribe({ query, variables }).subscribe({
next: d => {
  onData(d);
},
error: error => {
  handleErrorResponse(error).catch(e => {
    Logger.notifyError(new Error(`Real-time update error | ${e.message}.`));
  });
}

});

This works and I can see my onData callback firing on events.

Question - In our application, the Subscription filter is dynamic and user can select different values which will impact the filter. I have gone through documentation and many (many) articles but could not find a way to update filters or how to handle this use case?

Expectation - Once user selects any value and apply filter, I want to update my subscription on server to start using new filters?

AjayK
  • 433
  • 5
  • 18

1 Answers1

0

Once created, you cannot modify the subscription. When the filter changes, you would need to call unsubscribe on the current Observer and then create a new Observer by calling the subscribe method with the updated variables. If you look at the source code, this is how react-apollo's Subscription component handles it. If you happen to be using React already, I would highly advise using the available component rather than trying to reinvent the wheel.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • Hey Daniel, Thanks for the pointer. I did look at that code, and my bad in missing endSubscription part. We are trying to add this support in existing React app, which already has web sockets support and state management using Mobx. Current target is to add Graphql on anincremental basis. – AjayK May 06 '19 at 22:07