Install
yarn add mongo-relay-connection graphql graphql-relay
graphql
and graphql-relay
are required as peer dependencies.
Overview
To assist building a Relay Connection type from a mongoose schema. It supports dynamic collection. The order could be based on a field that is not necessarily unique. And existing schema need not be changed at all.
It is based on the Relay pagination algorithm. But as including a value for both first and last is confusing, the last is ignored if both are given.
# | after | first | before | last | remarks | support |
---|---|---|---|---|---|---|
1 | returns all | ✓ | ||||
2 | ✓ | ✓ | ||||
3 | ✓ | ✓ | ||||
4 | ✓ | ✓ | ✓ | |||
5 | ✓ | ✓ | ||||
6 | ✓ | ✓ | same as #5 | ✗ | ||
7 | ✓ | ✓ | ✓ | |||
8 | ✓ | ✓ | ✓ | same as #7 | ✗ | |
9 | ✓ | ✓ | ||||
10 | ✓ | ✓ | ✓ | |||
11 | ✓ | ✓ | ✓ | |||
12 | ✓ | ✓ | ✓ | ✓ | ||
13 | ✓ | ✓ | ✓ | |||
14 | ✓ | ✓ | ✓ | same as #13 | ✗ | |
15 | ✓ | ✓ | ✓ | ✓ | ||
16 | ✓ | ✓ | ✓ | ✓ | same as #15 | ✗ |
Usage
Suppose you want to do cursor based pagination over a collection:
// models/product.jsconst ProductSchema =name: Stringtype: Stringprice: Number
First create a corresponding GraphQLObjectType:
// types/product.jsconst Product =name: 'Product'fields:id: type: GraphQLIDname: type: GraphQLStringtype: type: GraphQLStringprice: type: GraphQLInt
Then create your query by defining the type, args, and resolve function. Here all the food product is selected and sorted by price descendingly:
const foodTypes ="Bacon""Cheese""Chicken""Chips""Fish""Pizza""Salad""Sausages""Tuna"const RootQuery =name: 'RootQuery'fields:allFoodProducts:type:args: mrArgs{const query =type: $in: foodTypesconst opts =cursorField: 'price'direction: -1return}
Boom, you're done! No third step. All the hard work of resolving is done for you.
Limitation
It is based on sorting on a single given field (default is _id). If the field is not unique, it is compounded with _id as the secondary sort. So it could only be sorted in one given dimension.
License
MIT