|
1 | 1 | graphql-compose-connection
|
2 | 2 | ======================
|
3 |
| -This is a plugin for [graphql-compose](https://github.com/nodkz/graphql-compose), which adds to the GraphQL types `connection` resolvers. |
| 3 | +This is a plugin for [graphql-compose](https://github.com/nodkz/graphql-compose) family, which adds to the GraphQL types `connection` resolvers. |
| 4 | + |
| 5 | +This package completely follows to Relay Cursor Connections Specification (https://facebook.github.io/relay/graphql/connections.htm). |
| 6 | + |
| 7 | +Besides standard connection arguments `first`, `last`, `before` and `after`, also added great arguments: |
| 8 | +* `filter` arg - for filtering records |
| 9 | +* `sort` arg - for sorting records. Build in mechanism allows sort by any unique indexes (not only by id). Also supported compound sorting (by several fields). |
4 | 10 |
|
5 | 11 | [CHANGELOG](https://github.com/nodkz/graphql-compose-connection/blob/master/CHANGELOG.md)
|
6 | 12 |
|
7 | 13 | Example
|
8 | 14 | =======
|
| 15 | +```js |
| 16 | +import composeWithConnection from 'graphql-compose-connection'; |
| 17 | +import userTypeComposer from './user.js'; |
| 18 | +const OPERATORS_FIELDNAME = '_operators'; |
9 | 19 |
|
| 20 | +composeWithConnection(userTypeComposer, { |
| 21 | + findResolverName: 'findMany', |
| 22 | + countResolverName: 'count', |
| 23 | + sort: { |
| 24 | + _ID_DESC: { |
| 25 | + uniqueFields: ['_id'], |
| 26 | + sortValue: { _id: -1 }, |
| 27 | + directionFilter: (cursorData, filter, isBefore) => { |
| 28 | + filter[OPERATORS_FIELDNAME] = filter[OPERATORS_FIELDNAME] || {}; |
| 29 | + filter[OPERATORS_FIELDNAME]._id = filter[OPERATORS_FIELDNAME]._id || {}; |
| 30 | + if (isBefore) { |
| 31 | + filter[OPERATORS_FIELDNAME]._id.gt = cursorData._id; |
| 32 | + } else { |
| 33 | + filter[OPERATORS_FIELDNAME]._id.lt = cursorData._id; |
| 34 | + } |
| 35 | + return filter; |
| 36 | + }, |
| 37 | + }, |
| 38 | + AGE_ID_ASC: { |
| 39 | + uniqueFields: ['age', '_id'], |
| 40 | + sortValue: { age: 1, _id: -1 }, |
| 41 | + directionFilter: (cursorData, filter, isBefore) => { |
| 42 | + filter[OPERATORS_FIELDNAME] = filter[OPERATORS_FIELDNAME] || {}; |
| 43 | + filter[OPERATORS_FIELDNAME]._id = filter[OPERATORS_FIELDNAME]._id || {}; |
| 44 | + filter[OPERATORS_FIELDNAME].age = filter[OPERATORS_FIELDNAME].age || {}; |
| 45 | + if (isBefore) { |
| 46 | + filter[OPERATORS_FIELDNAME].age.lt = cursorData.age; |
| 47 | + filter[OPERATORS_FIELDNAME]._id.gt = cursorData._id; |
| 48 | + } else { |
| 49 | + filter[OPERATORS_FIELDNAME].age.gt = cursorData.age; |
| 50 | + filter[OPERATORS_FIELDNAME]._id.lt = cursorData._id; |
| 51 | + } |
| 52 | + return filter; |
| 53 | + }, |
| 54 | + } |
| 55 | + }, |
| 56 | +}) |
| 57 | +``` |
| 58 | +<img width="946" alt="screen shot 2016-07-14 at 15 30 24" src="https://cloud.githubusercontent.com/assets/1946920/16834977/1e0b6760-49d8-11e6-833f-cae5ffa68717.png"> |
10 | 59 |
|
11 | 60 | Requirements
|
12 | 61 | ============
|
| 62 | +Types should have following resolvers: |
| 63 | +* `count` - for counting records |
| 64 | +* `findMany` - for filtering records. Also required that this resolver supports search with operators (lt, gt), which used in `directionFilter` option. Resolver `findMany` should have `filter` argument, which will be copied to connection. |
13 | 65 |
|
14 |
| - |
15 |
| -Compatible plugins |
16 |
| -================== |
| 66 | +Used in plugins |
| 67 | +=============== |
| 68 | +[graphql-compose-mongoose](https://github.com/nodkz/graphql-compose-mongoose) |
17 | 69 |
|
18 | 70 |
|
19 | 71 | License
|
20 | 72 | =======
|
21 |
| -[MIT](https://github.com/nodkz/graphql-compose-relay/blob/master/LICENSE.md) |
| 73 | +[MIT](https://github.com/nodkz/graphql-compose-connection/blob/master/LICENSE.md) |
0 commit comments