如何更新 useSubscription 挂钩中的订阅变量 - Apollo v3

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

我目前正在尝试在 useEffect 挂钩中使用来自 apollo v3 的 useSubscription 挂钩更新 GraphQL 订阅:

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]);

我在 apollo 文档中找不到任何关于如何解决这个问题的解决方案。

如有任何帮助,我们将不胜感激!

无需使用 useEffect -- 您只需更改 selectedDate。如果传递给挂钩的任何选项发生变化,当前订阅将被取消订阅,并使用新选项开始新的订阅。