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 }) }), }
source share