mickhansen / graphql-sequelize

GraphQL & Relay for MySQL & Postgres via Sequelize
MIT License
1.9k stars 172 forks source link

Exposing toCursor and fromCursor #561

Closed intellix closed 4 years ago

intellix commented 6 years ago

I'm currently creating a leaderboard of users based on a field that I order by: points. I can get a user's rank by exposing an index field for the rank:

edgeFields: {
  index: {
    type: GraphQLInt,
    resolve: edge => (new Buffer(edge.cursor, 'base64')).toString('ascii').split('$').pop(),
  },
},

Now, I can create a leaderboard or top 10 of points by creating a query like so:

Query:

{
  users(last: 10, orderBy: POINTS) {
    edges {
      index
      node {
        name
        points
      }
    }
  }
}

Result:

{
  "data": {
    "users": {
      "edges": [
        {
          "index": 0,
          "node": {
            "name": "Bob"
            "points": 100
          }
        },
        {
          "index": 1,
          "node": {
            "name": "Bill",
            "points": 90
          }
        },
        ...
      ]
    }
  }
}

Right now I'm duplicating the work of fromCursor to get ahold of the index. It'd be cool to expose those 2 methods: https://github.com/mickhansen/graphql-sequelize/blob/master/src/relay.js#L168 https://github.com/mickhansen/graphql-sequelize/blob/master/src/relay.js#L178

mickhansen commented 6 years ago

Another solution might be to add index to the edge here: https://github.com/mickhansen/graphql-sequelize/blob/master/src/relay.js#L210

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.