ApolloClient-同一组件中同时进行多个订阅
创始人
2024-09-09 07:30:22
0

在同一组件中进行多个 GraphQL 订阅时,需要注意订阅 id 不同,而且每个订阅都需要创建单独的 subscription,并在 componentWillUnmount 中取消订阅。

代码示例:

import React, { Component } from 'react';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';

const subscription1 = gql`
  subscription newMessages($id: ID!) {
    newMessage(id: $id) {
      content
    }
  }
`;

const subscription2 = gql`
  subscription newNotifications($id: ID!) {
    newNotification(id: $id) {
      content
    }
  }
`;

class MyComponent extends Component {
  componentDidMount() {
    this.subscription1 = this.props.subscribeToNewMessages({ variables: { id: this.props.id } });
    this.subscription2 = this.props.subscribeToNewNotifications({ variables: { id: this.props.id } });
  }

  componentWillUnmount() {
    this.subscription1();
    this.subscription2();
  }

  render() {
    // ...
  }
}

export default graphql(subscription1, {
  name: 'subscribeToNewMessages',
  props: ({ subscribeToMore }) => ({
    subscribeToNewMessages: params =>
      subscribeToMore({
        document: subscription1,
        variables: params.variables,
      }),
  }),
})(graphql(subscription2, {
  name: 'subscribeToNewNotifications',
  props: ({ subscribeToMore }) => ({
    subscribeToNewNotifications: params =>
      subscribeToMore({
        document: subscription2,
        variables: params.variables,
      }),
  }),
})(MyComponent));

相关内容

热门资讯

5分钟实锤!全民牛牛app拼三... 5分钟实锤!全民牛牛app拼三张辅助器(辅助挂)外挂透视辅助插件(2024已更新)(哔哩哔哩)1、每...
发现玩家!wopoker分析器... 发现玩家!wopoker分析器下载,wPk原来真的有挂,2024新版教程!(有挂教学)-哔哩哔哩;亲...
玩家亲测!德州辅助分析软件(辅... 您好,德州这款游戏可以开挂的,确实是有挂的,需要了解加微【487309276】很多玩家在这款游戏中打...
推荐十款(AaPOKER)外挂... 推荐十款(AaPOKER)外挂辅助器(辅助挂)透视辅助(2024已更新)(哔哩哔哩)准备好在AaPO...
一分钟普及!功夫川麻辅助有用,... 一分钟普及!功夫川麻辅助有用,雀神小程序脚本,介绍教程(有挂头条);功夫川麻辅助插件是一个在免局卡环...
透视智能ai!poker ma... 透视智能ai!poker master安卓版外挂(约局吧)外挂透明挂辅助器安装(辅助挂)辅助教程(2...
推荐十款!pokermaste... 推荐十款!pokermaster外挂(辅助挂)透视辅助工具(2024已更新)(哔哩哔哩),您好,po...
盘点一款(wpk修改器)外挂透... 盘点一款(wpk修改器)外挂透明挂辅助神器(辅助挂)攻略教程!(2025已更新)(哔哩哔哩);原来确...
四分钟攻略!曦梦奇缘有外挂,小... 四分钟攻略!曦梦奇缘有外挂,小程序雀神麻将来牌技巧,可靠技巧(有挂总结)是一款可以让一直输的玩家,快...
重大来袭!wepoke软件(德... 重大来袭!wepoke软件(德州ai智能系统)外挂透视辅助器安装(透视)2024新版教程!(2023...