gombo
gombo

Reputation: 225

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

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!

Upvotes: 5

Views: 2456

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84657

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.

Upvotes: 6

Related Questions