Relay Modern - 订阅更新程序(连接处理程序)方法(没有关于此事项的文档)

时间:2017-10-31 15:09:14

标签: reactjs graphql graphql-js relaymodern

我正在尝试为我的查询执行订阅。所以我怀疑是连接(来自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);
};

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

记录DEBUG触发:RelayRecordSourceSelectorProxy

日志DEBUG2触发:RelayRecordProxy //为特定ID(59f88d417fae441eb567c453)创建,

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

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

记录DEBUG5:undefined

记录DEBUG6:ConnectionHandler方法,

记录DEBUG7:user.id请求查询。

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

1 个答案:

答案 0 :(得分:0)

订阅:Updating the client on each response

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