node.js – graphql cursor based pagination with mongodb

I m trying to design relay type cursor based pagination in graphql using mongoDb. I followed some of the instructions in this blog www.reindex.io/blog/relay-graphql-pagination-with-mongodb/.

so here is the thing. Normally all the tutorials and stackoverflow questions based on ObjectID of mongoDb. For example,

Graphql Query

product(first:10, cursor){
 ...
}


MongoDb request

db.product.find({_id: { $gt : ObjectId(cursor) } }).limit(first)

So this is fine until we are not using any sorting based on any fields. In my situation I’m using custom Ids as _id using uuid package and how can I design this relay based cursor pagination when _id is a custom string and sorting changes? How to change the cursor if I’m sorted results on product based on ratings, sales? (Each product document has a summerized sales count, ratings counts)

Read more here: Source link