Skip to content

Commit 9baa40f

Browse files
committed
feat(Pagination): Add pagination resolver
close #46
1 parent 35a9a70 commit 9baa40f

File tree

6 files changed

+681
-559
lines changed

6 files changed

+681
-559
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ This is a plugin for [graphql-compose](https://github.com/nodkz/graphql-compose)
1414
Installation
1515
============
1616
```
17-
npm install graphql graphql-compose graphql-compose-connection mongoose graphql-compose-mongoose --save
17+
npm install graphql graphql-compose graphql-compose-connection graphql-compose-pagination mongoose graphql-compose-mongoose --save
1818
```
19-
Modules `graphql`, `graphql-compose`, `graphql-compose-connection`, `mongoose` are in `peerDependencies`, so should be installed explicitly in your app. They have global objects and should not have ability to be installed as submodule.
19+
Modules `graphql`, `graphql-compose`, `graphql-compose-connection`, `graphql-compose-pagination`, `mongoose` are in `peerDependencies`, so should be installed explicitly in your app. They have global objects and should not have ability to be installed as submodule.
2020

2121
Example
2222
=======
@@ -76,8 +76,9 @@ GQC.rootQuery().addFields({
7676
userByIds: UserTC.getResolver('findByIds'),
7777
userOne: UserTC.getResolver('findOne'),
7878
userMany: UserTC.getResolver('findMany'),
79-
userTotal: UserTC.getResolver('count'),
79+
userCount: UserTC.getResolver('count'),
8080
userConnection: UserTC.getResolver('connection'),
81+
userPagination: UserTC.getResolver('pagination'),
8182
});
8283

8384
GQC.rootMutation().addFields({
@@ -314,7 +315,10 @@ export type typeConverterResolversOpts = {
314315
uniqueFields: string[],
315316
sortValue: mixed,
316317
directionFilter: (<T>(filterArg: T, cursorData: CursorDataType, isBefore: boolean) => T),
317-
};
318+
},
319+
pagination?: false | {
320+
perPage?: number,
321+
},
318322
};
319323
```
320324

@@ -362,6 +366,8 @@ Besides standard connection arguments `first`, `last`, `before` and `after`, als
362366

363367
This plugin completely follows to [Relay Cursor Connections Specification](https://facebook.github.io/relay/graphql/connections.htm).
364368

369+
### [graphql-compose-pagination](https://github.com/nodkz/graphql-compose-pagination)
370+
This plugin adds `pagination` resolver.
365371

366372
License
367373
=======

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"peerDependencies": {
3030
"graphql-compose": ">=1.20.3 || >=2.0.0",
3131
"graphql-compose-connection": ">=2.2.1",
32+
"graphql-compose-pagination": ">=1.0.0",
3233
"mongoose": ">=4.0.0"
3334
},
3435
"devDependencies": {
@@ -50,6 +51,7 @@
5051
"graphql": "^0.10.3",
5152
"graphql-compose": "^2.0.0",
5253
"graphql-compose-connection": "^2.2.2",
54+
"graphql-compose-pagination": "^1.0.0",
5355
"jest": "^20.0.4",
5456
"mongodb-memory-server": "^1.3.4",
5557
"mongoose": "^4.10.7",

src/__tests__/composeWithMongoose-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,30 @@ describe('composeWithMongoose ->', () => {
163163
});
164164
});
165165

166+
describe('3rd party resolvers', () => {
167+
describe('graphql-compose-connection', () => {
168+
it('should add `connection` resolver by default', () => {
169+
const tc2 = composeWithMongoose(UserModel);
170+
expect(tc2.getResolver('connection')).toBeDefined();
171+
});
172+
});
173+
174+
describe('graphql-compose-pagination', () => {
175+
it('should add `pagination` resolver by default', () => {
176+
const tc2 = composeWithMongoose(UserModel);
177+
expect(tc2.getResolver('pagination')).toBeDefined();
178+
});
179+
180+
it('should add `pagination` resolver with `perPage` option', () => {
181+
const tc2 = composeWithMongoose(UserModel, { resolvers: { pagination: { perPage: 333 } } });
182+
const resolver = tc2.getResolver('pagination');
183+
expect(resolver).toBeDefined();
184+
// $FlowFixMe
185+
expect(resolver.getArg('perPage').defaultValue).toBe(333);
186+
});
187+
});
188+
});
189+
166190
describe('complex situations', () => {
167191
it('required input fields, should be passed down to resolvers', () => {
168192
const typeComposer = composeWithMongoose(UserModel, {

src/composeWithMongoose.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import { TypeComposer, InputTypeComposer } from 'graphql-compose';
55
import composeWithConnection from 'graphql-compose-connection';
6+
import composeWithPagination from 'graphql-compose-pagination';
67
import { convertModelToGraphQL } from './fieldsConverter';
78
import * as resolvers from './resolvers';
89
import { getUniqueIndexes, extendByReversedIndexes } from './utils/getIndexesFromModel';
@@ -122,6 +123,15 @@ export function createResolvers(
122123
if (!{}.hasOwnProperty.call(opts, 'connection') || opts.connection !== false) {
123124
prepareConnectionResolver(model, typeComposer, opts.connection ? opts.connection : {});
124125
}
126+
127+
if (!{}.hasOwnProperty.call(opts, 'pagination') || opts.pagination !== false) {
128+
const pOpts = opts.pagination || {};
129+
composeWithPagination(typeComposer, {
130+
findResolverName: 'findMany',
131+
countResolverName: 'count',
132+
...pOpts,
133+
});
134+
}
125135
}
126136

127137
export function prepareConnectionResolver(

src/definition.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import type { TypeComposer } from 'graphql-compose';
55
import type { ConnectionSortMapOpts as _ConnectionSortMapOpts} from 'graphql-compose-connection/lib/definition';
6+
67
export type ConnectionSortMapOpts = _ConnectionSortMapOpts;
78

89
export type ObjectMap = { [optName: string]: any };
@@ -185,6 +186,7 @@ export type TypeConverterResolversOpts = {
185186
filter?: FilterHelperArgsOpts | false,
186187
},
187188
connection?: ConnectionSortMapOpts | false,
189+
pagination?: { perPage?: number } | false,
188190
};
189191

190192
export type FilterHelperArgsOpts = {

0 commit comments

Comments
 (0)