Skip to content

Commit 0b0f809

Browse files
committed
Add readme
1 parent a673cdc commit 0b0f809

File tree

1 file changed

+57
-5
lines changed

1 file changed

+57
-5
lines changed

README.md

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,73 @@
11
graphql-compose-connection
22
======================
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).
410

511
[CHANGELOG](https://github.com/nodkz/graphql-compose-connection/blob/master/CHANGELOG.md)
612

713
Example
814
=======
15+
```js
16+
import composeWithConnection from 'graphql-compose-connection';
17+
import userTypeComposer from './user.js';
18+
const OPERATORS_FIELDNAME = '_operators';
919

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">
1059

1160
Requirements
1261
============
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.
1365

14-
15-
Compatible plugins
16-
==================
66+
Used in plugins
67+
===============
68+
[graphql-compose-mongoose](https://github.com/nodkz/graphql-compose-mongoose)
1769

1870

1971
License
2072
=======
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

Comments
 (0)