Code Monkey home page Code Monkey logo

Comments (2)

tuanquynet avatar tuanquynet commented on June 12, 2024

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?

from graphql-compose-relay.

nodkz avatar nodkz commented on June 12, 2024

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

from graphql-compose-relay.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.