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

gom*_*mbo 4 apollo reactjs graphql react-hooks

我目前正在尝试使用 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]);
Run Code Online (Sandbox Code Playgroud)

我在阿波罗文档中找不到有关如何解决此问题的任何解决方案。

任何帮助,将不胜感激!

Dan*_*den 5

没有必要使用useEffect——你只需要改变selectedDate。如果传递给挂钩的任何选项发生更改,则当前订阅将被取消订阅,并且将使用新选项启动新订阅。