Graphql 如何退订现代接力赛

Graphql 如何退订现代接力赛,graphql,relayjs,relay,relaymodern,Graphql,Relayjs,Relay,Relaymodern,如何在Relay Modern中取消订阅 我已经在上关注了订阅教程,但它没有提到如何取消订阅,也没有提到Relay Modern网站 任何帮助都会很棒 更新---------- 根据Lee Byron()的说法,您只需调用dispose() 在requestSubscription()上 对示例进行以下修改后: ./src/subscriptions/NewVoteSubscription.js(向requestSubscription添加return) ./src/components/Lin

如何在Relay Modern中取消订阅

我已经在上关注了订阅教程,但它没有提到如何取消订阅,也没有提到Relay Modern网站

任何帮助都会很棒

更新----------

根据Lee Byron()的说法,您只需调用
dispose()
requestSubscription()上

对示例进行以下修改后:

./src/subscriptions/NewVoteSubscription.js(向requestSubscription添加
return

./src/components/LinkList.js(在组件上设置订阅,然后使用
componentWillUnmount
dispose()
it)

以下是我得到的错误:

Uncaught TypeError: Cannot read property 'dispose' of undefined
    at RelayObservable.js:94
    at doCleanup (RelayObservable.js:453)
    at Object.unsubscribe (RelayObservable.js:474)
    at RelayObservable.js:330
    at doCleanup (RelayObservable.js:453)
    at Object.unsubscribe (RelayObservable.js:474)
    at doCleanup (RelayObservable.js:450)
    at Object.unsubscribe [as dispose] (RelayObservable.js:474)
    at LinkList.componentWillUnmount (LinkList.js:18)
    at callComponentWillUnmountWithTimerInDev (react-dom.development.js:11123)
    at HTMLUnknownElement.callCallback (react-dom.development.js:1309)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:1348)
    at invokeGuardedCallback (react-dom.development.js:1205)
    at safelyCallComponentWillUnmount (react-dom.development.js:11131)
    at commitUnmount (react-dom.development.js:11421)
    at unmountHostComponents (react-dom.development.js:11362)
    at commitDeletion (react-dom.development.js:11392)
    at commitAllHostEffects (react-dom.development.js:12279)
    at HTMLUnknownElement.callCallback (react-dom.development.js:1309)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:1348)
    at invokeGuardedCallback (react-dom.development.js:1205)
    at commitAllWork (react-dom.development.js:12384)
    at workLoop (react-dom.development.js:12695)
    at HTMLUnknownElement.callCallback (react-dom.development.js:1309)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:1348)
    at invokeGuardedCallback (react-dom.development.js:1205)
    at performWork (react-dom.development.js:12808)
    at batchedUpdates (react-dom.development.js:13262)
    at performFiberBatchedUpdates (react-dom.development.js:1656)
    at stackBatchedUpdates (react-dom.development.js:1647)
    at batchedUpdates (react-dom.development.js:1661)
    at Object.batchedUpdatesWithControlledComponents [as batchedUpdates] (react-dom.development.js:1674)
    at dispatchEvent (react-dom.development.js:1884)

和设置相关环境

function setupSubscription(config, variables, cacheConfig, observer) {
  const query = config.text;

  const subscriptionClient = new SubscriptionClient(websocketURL, {
    reconnect: true
  });

  const client = subscriptionClient.request({ query, variables }).subscribe({
    next: result => {
      observer.onNext({ data: result.data });
    },
    complete: () => {
      observer.onCompleted();
    },
    error: error => {
      observer.onError(error);
    }
  });

  return {
    dispose: client.unsubscribe
  };
}

自<代码>继电器-modern@6
您需要return observable,否则它将由于以下错误而崩溃:
RelayObservable:未处理的错误类型error:source.subscribe不是函数

而不是

return {
    dispose: client.unsubscribe
};
你需要回报

return Observable.create(() => {
  // return cleanup function
  return yourFunctionToCleanUp;
});
在可观察的位置,您可以从
中继运行时导入

比如:

因此,当您在
组件中调用
this.subscription.dispose()
时,实际上它将调用函数
您的函数tocleanup()

return {
    dispose: client.unsubscribe
};
return Observable.create(() => {
  // return cleanup function
  return yourFunctionToCleanUp;
});
import {
  Observable,
} from 'relay-runtime';