How do I update subscription variables in a useSubscription hook - Apollo v3

Viewed 1603

I am currently trying to update a GraphQL subscription using the useSubscription hook from apollo v3 in a useEffect hook:

let containerSubscription = useSubscription<CreatedDateSubscription, CreatedDateSubscriptionVariables>(
  gql(containerUpdatedOnCreatedDate),
  {
    variables: { createdDate: selectedDate },
    shouldResubscribe: true, // is this needed?
  },
);

// update subscription on date change
React.useEffect(() => {
  // how do I update the subscription here?
  // setting containerSubscription.variables = ... does not change the subscription
}, [selectedDate]);

I could not find any solution in the apollo docs on how to address this problem.

Any help would be appreciated!

1 Answers

There's no need to use useEffect -- you just need to change selectedDate. If any of the options passed to the hook change, the current subscription will be unsubscribed and a new one will be started using the new options.

Related