0

我正在尝试为我的查询执行订阅。所以我怀疑连接(来自 ConnectionHandler)不起作用。我找不到有关此文件的任何适当文档。

我的订阅看起来像:

  const LinkSubscription = graphql`
  subscription LinkSubscription {
    Link(filter:{
      mutation_in:[CREATED]
    }){
      node{
        id
      }
    }
  }
`;

export default () => {
  const subscriptionConfig = {
    subscription: LinkSubscription,
    variables: {},
    onCompleted: () => { alert('done!'); },
    updater: (Store, data) => {
      const newLink = Store.getRootField('Link').getLinkedRecord('node');
      const allLinks = Store.getRoot();
      const edge = ConnectionHandler.createEdge(Store, allLinks, newLink, 'allLinks');
      const userId = localStorage.getItem('user_id');
      const connection = ConnectionHandler.getConnection(allLinks, newLink, 'allLinks');
      if (connection) {
        ConnectionHandler.insertEdgeAfter(connection, newLink);
        console.log('DONE');
      }
      console.log('DEBUG', Store);
      console.log('DEBUG2', newLink);
      console.log('DEBUG3', allLinks);
      console.log('DEBUG4', edge);
      console.log('DEBUG5', connection);
      console.log('DEBUG6', ConnectionHandler);
      console.log('DEBUG7', userId);
      console.log('Debug8', data);
    },
    onError: error => console.log('An error occured:', error),
  };
  requestSubscription(Environment, subscriptionConfig);
};

正如您在代码中看到的那样,我运行了很多日志来查看我做错了什么。

日志调试触发:RelayRecordSourceSelectorProxy,

日志 DEBUG2 触发:RelayRecordProxy// 针对特定 id(59f88d417fae441eb567c453) 已创建,

日志 DEBUG3 触发:RelayRecordProxy// 对于客户端:root,

日志 DEBUG4 触发:RelayRecordProxy// 对于客户端:root:59f88d417fae441eb567c453,

记录 DEBUG5: undefined,

记录 DEBUG6:ConnectionHandler方法,

日志 DEBUG7:user.id谁请求了查询。

问题1:您能提供一些连接建议吗?

4

1 回答 1

0

订阅:在每次响应时更新客户端

const LinkSubscription = graphql`
  subscription LinkSubscription {
    Link(filter: { mutation_in: [CREATED] }) {
      node {
        id
      }
    }
  }
`;

export const test = () => {
  const subscriptionConfig = {
    subscription: LinkSubscription,
    variables: {},
    onCompleted: () => {
      alert('done!');
    },
    updater: (Store, data) => {
      const newLink = Store.getRootField('Link').getLinkedRecord('node');
      const viewerProxy = Store.getRoot().getLinkedRecord('viewer');

      const connection = ConnectionHandler.getConnection(
        viewerProxy,
        '<nameConnection>', // 'Viewer_links' or <Name_links>
        { mutation_in: ['CREATED'] }
      );
      const edge = ConnectionHandler.createEdge(
        Store,
        connection,
        newLink,
        'LinkEdge'
      );
      if (connection) {
        ConnectionHandler.insertEdgeBefore(connection, edge);
        console.log('DONE');
      }
    },
    onError: error => console.log('An error occured:', error)
  };

  requestSubscription(Environment, subscriptionConfig);
};
于 2017-11-01T02:12:00.617 回答