Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recordEdge to mutation payloads #1

Open
nodkz opened this issue Aug 2, 2016 · 2 comments
Open

Add recordEdge to mutation payloads #1

nodkz opened this issue Aug 2, 2016 · 2 comments

Comments

@nodkz
Copy link
Member

nodkz commented Aug 2, 2016

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.

@tuanquynet
Copy link

tuanquynet commented Apr 27, 2017

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?

@nodkz
Copy link
Member Author

nodkz commented Apr 28, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants