0

I am trying to perform a subscription for my query. So my suspicion is that the connection (from the ConnectionHandler) is not working. I cannot find any proper documentation on this one.

My subscription looks like:

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

As you may see in the code i ran a lot of logs to see what i did wrong.

Log DEBUG fires: RelayRecordSourceSelectorProxy,

Log DEBUG2 fires:RelayRecordProxy // for the specific id(59f88d417fae441eb567c453) CREATED,

Log DEBUG3 fires: RelayRecordProxy// for client:root,

Log DEBUG4 fires: RelayRecordProxy// for client:root:59f88d417fae441eb567c453,

Log DEBUG5: undefined,

Log DEBUG6: ConnectionHandler methods,

Log DEBUG7: user.id who requested the query.

Question1:Can you please help with some connection suggestions?

1 Answers1

0

Subscriptions: 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);
};
Ng Thong
  • 104
  • 1
  • 6
  • I have read the documentation suggested. No info about the `connectionHandler`. Thought this was a test subscription, i will work further in generating the real schema for my app. The above question is still valid. A second/third Question2: Does Relay Modern subscriptions work without having edges/viewer? Question3: Can anyone spare a console.log on a `connectionHandler.getConnection`? – Cristian Irimiea Nov 02 '17 at 14:57