graphql - 如何取消订阅 Relay Modern

标签 graphql relayjs relay relaymodern

如何在 Relay Modern 中取消订阅?

我已经按照 How to GraphQL React + Relay 上的订阅教程进行操作但它没有提及您如何取消订阅,也没有提及 Relay Modern 网站。

任何帮助都会很棒。

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

根据 Lee Byron( see GitHub issue ) 你只需要调用 dispose()requestSubscription()

对示例进行如下修改后:

./src/subscriptions/NewVoteSubscription.js(添加return到requestSubscription)

export default () => {
    const subscriptionConfig = {
    subscription: newVoteSubscription,
    variables: {},
    updater: proxyStore => {
        const createVoteField = proxyStore.getRootField('Vote')
        const newVote = createVoteField.getLinkedRecord('node')
        const updatedLink = newVote.getLinkedRecord('link')
        const linkId = updatedLink.getValue('id')
        const newVotes = updatedLink.getLinkedRecord('_votesMeta')
        const newVoteCount = newVotes.getValue('count')

        const link = proxyStore.get(linkId)
        link.getLinkedRecord('votes').setValue(newVoteCount, 'count')
    },
    onError: error => console.log(`An error occured:`, error)
  }

  return requestSubscription(
      environment,
      subscriptionConfig
  )

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

componentDidMount() {
    this.subscription = NewVoteSubscription()
}

componentWillUnmount() {
    this.subscription.dispose()
}

这是我得到的错误:

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
  };
}

关于graphql - 如何取消订阅 Relay Modern,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46065800/

相关文章:

graphql - 中继编译器不生成 .graphql 文件

javascript - 未捕获错误 : react-apollo only supports a query, 订阅,或每个 HOC 的突变

javascript - 使用相对路径而不是 API 服务器进行中继突变

javascript - 如何修复 GraphQL Relay 连接的循环依赖

reactjs - 中继片段传播不起作用

graphql - 如何在 GraphQL buildSchema 中使用联合

reactjs - Graphql - Apollo Client/React - 缓存 - fetchMore 不更新查询的数据状态

javascript - React Relay 项目目录结构组织

javascript - Graphql 和 Relay 可扩展性

javascript - 如何在现有 GraphQL 服务器上添加根和查看器查询以支持中继