How to control cursors and sorting in Relay?

We have a graphql server (not written in javascript) serving a broken list of objects. We are trying to meet the specification of the relay, but we are in an interesting case that could use explanations.

In particular: can cursors depend on other inputs to the connection? Like https://github.com/graphql/graphql-relay-js/issues/20 , our connection accepts the sort_key argument, which determines the sort order of the returned list. Depending on the specified sort order, the edge of the object may return different cursor values โ€‹โ€‹(since the server requires different information in each case to determine the next object). However, a thorough reading of https://facebook.imtqy.com/relay/docs/guides-mutations.html#range-add suggests that this is not allowed; should mutations returning a newly created edge return a single cursor that can be universally applied to all possible lists in which this edge can appear? How does facebook solve this problem?

+5
source share
2 answers

I had the same problem. So, I decided to write an npm package to solve this problem.

You can use the fast-relay-pagination npm package to sort, reverse and forward pagination and filter the Mongoose model or MongoDB object.

This package improves graphql-relay lazy loading using Mongoose or MongoDB search and restriction. As you definitely know, graphql-relay's connectionFromArray extracts all the data and performs data slicing, which is inefficient for large volumes.


You can see an example below:

 ... import { fetchConnectionFromArray } from 'fast-relay-pagination' ... export default{ type: orderConnection.connectionType, args: { ...connectionArgs, orderFieldName: { type: GraphQLString, }, sortType: { type: GraphQLInt, }, }, resolve: needAdmin(async (_, args) => { let orderFieldName = args.orderFieldName || '_id' let sortType = args.sortType || -1 let after = args.after let before = args.before let filter = args.filter let first = args.first let last = args.last return fetchConnectionFromArray({ dataPromiseFunc: SampleModel.find.bind(SampleModel), // required filter, // optional (for using filter on model collection) - for example => {username: 'test'} after, //optiona before, // optional first, //optional last, // optional orderFieldName, // optional sortType, // optional }) }), } 
+3
source

Yes, the cursor should contain enough information to enable the selection of the next page from this point (including restrictions such as sorting and filtering), but regardless of whether you perform this, depends on the specific implementation.

As for mutations, in your getConfigs() implementation, you can specify rangeBehaviors for each call. So, if you have a sorted view, you can add or add depending on the sort order. This behavior indicates what Relay will do on the client side to update its repository. You are still responsible for ensuring that the code that your GraphQL server receives all the information it needs (via input variables) correctly executes the actual mutation.

+2
source

Source: https://habr.com/ru/post/1241240/


All Articles