Apollo订阅问题 - updateQuery未解雇

时间:2017-12-28 18:09:32

标签: reactjs websocket react-apollo

我有一个使用Apollo的WebSocketLink接口创建的功能正常的Web套接字。我设法使用subscribeToMore订阅了一个事件,并且服务器推送了一条消息(可以在网络选项卡中看到它)。不幸的是,永远不会触发updateQuery函数。我想知道消息结构是不正确的(因此服务器实现错误)还是我的客户端代码出错了。

作为参考,我添加了从服务器发送的消息:

enter image description here

这里是我的组件的graphql配置:

import { graphql } from "react-apollo/index";
import Insights from 'components/insights/Insights';
import gql from "graphql-tag";
import { withRouter } from "react-router-dom";
import get from 'lodash/get';

const query = gql`
  query CampaignInsights($campaignId: ID) {
    campaigns (id: $campaignId) {
      edges {
        node {
          insights {
            campaignPlanningInsight {
              campaign
              plannedTotals {
                totalOptimizationRules
                totalOfferGroups
                totalOffers
              }
              liveTotals {
                totalOptimizationRules
                totalOfferGroups
                totalOffers
              }
            }
          }
        }
      }
    }
  }  
`;

const insightsSubscription = gql`
  subscription onInsightsUpdated($campaignId: ID) {
    campaignPlanningInsightUpdated(id: $campaignId) {
      id
      plannedTotals {
        totalOptimizationRules
        totalOfferGroups
        totalOffers
      }
      liveTotals {
        totalOptimizationRules
        totalOfferGroups
        totalOffers
      }
    }
  }
`;

const InsightsWithData = graphql(query, {
  options: (props) => {
    return {
      variables: {
        campaignId: props.match.params.campaignId
      }
    }
  },
  props: ({ data: { campaigns, subscribeToMore }, ownProps: { match } 
}) => {
    return {
      insights: get(campaigns, 
'edges[0].node.insights[0].campaignPlanningInsight', null),
      subscribeToInsightsUpdate: () => {
        return subscribeToMore({
          document: insightsSubscription,
          variables: {
            campaignId: match.params.campaignId
          },
          updateQuery: (prev, { subscriptionData }) => {
            debugger; // never gets here
            if (!subscriptionData.data) {
              return prev;
            }
          }
        })
      }
    }
  }
})(Insights);

export default withRouter(InsightsWithData);

1 个答案:

答案 0 :(得分:1)

我认为问题可能是id Websocket协议的graphql-ws

id必须与GQL_START消息中前端发送的匹配。否则,该组件将不会在新消息上重新呈现。

有关更多详细信息,请查看subscription-transport-ws protocol