graphql-compose / graphql-compose-relay

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add recordEdge to mutation payloads

nodkz opened this issue · comments

If detected connection resolver, then composeWithRelay should add nodeEdge(sort: SortType) field to payload, for passing it to RANGE_ADD on the client mutation config. Right now this wrapper does not allow to do it out of the box.

Is it easy to implement this feature? Do you have plan to implement it? Could you give us a guideline to implement it if you have no time?

No, I have not plan to implement it, cause does not use RANGE_ADD. For simplicity, I completely reload relay connection in my app.

But with pleasure share ideas how it should be implemented.

Before, let look on what is it cursor and how it works under the hood:

  • for every document cursor keeps values from fields, by which you make the sort (e.g. .sort({ _id: 1, age: 1}) then you should construct such cursor base64({_id: 34ef85...34, age: 28})
  • sort can be proceeded only by unique indexes (to avoid overlapping)
  • when proceeding before and after arguments with the cursor, you should parse values from it and use them via search with operators .where({ _id: {$gt: 34ef85...34}, age: {$gt: 28}})

So the most tricky part is determining correct cursor value for the nodeEdge. It can not be calculated in mutation without knowledge of how your connection (with which order/sorting) fetched your data on the client. And when you sending a mutation, you should provide sort fields as arg to nodeEdge for gathering correct cursor value.

The code will look something like this:

UserTC.get('$createOne').getTypeComposer().setField('nodeEdge', {
  args: {
    sort: UserTC.get('$connection.@sort'),
  },
  type: UserTC.get('$connection.edges'),
  resolve: (source, args, context, info) => {
    // PREPARE `node` 
    // console.dir(source, { depth: 4, color: true });
    const node = source.record;

    // PREPARE `cursor`
    // console.dir(args, { depth: 4, color: true });
    // see how constructed cursor https://github.com/nodkz/graphql-compose-connection/blob/master/src/connectionResolver.js#L190
    const cursor = ...;

    return {
      cursor,
      node
    };
  },
});