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?
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.