|
1 | 1 | /* @flow */
|
2 | 2 | /* eslint-disable arrow-body-style */
|
3 | 3 |
|
4 |
| -import { upperFirst, type ObjectTypeComposer, type SchemaComposer } from 'graphql-compose'; |
| 4 | +import { upperFirst, ObjectTypeComposer, type SchemaComposer } from 'graphql-compose'; |
| 5 | + |
| 6 | +// PaginationInfo should be global |
| 7 | +const PaginationInfoTC = ObjectTypeComposer.createTemp(` |
| 8 | +# Information about pagination. |
| 9 | +type PaginationInfo { |
| 10 | + # Current page number |
| 11 | + currentPage: Int! |
| 12 | + |
| 13 | + # Number of items per page |
| 14 | + perPage: Int! |
| 15 | + |
| 16 | + # Total number of pages |
| 17 | + pageCount: Int |
| 18 | + |
| 19 | + # Total number of items |
| 20 | + itemCount: Int |
| 21 | + |
| 22 | + # When paginating forwards, are there more items? |
| 23 | + hasNextPage: Boolean |
| 24 | + |
| 25 | + # When paginating backwards, are there more items? |
| 26 | + hasPreviousPage: Boolean |
| 27 | +} |
| 28 | +`); |
5 | 29 |
|
6 | 30 | export function preparePaginationInfoTC<TContext>(
|
7 |
| - schemaComposer: SchemaComposer<TContext> |
| 31 | + sc: SchemaComposer<TContext> |
8 | 32 | ): ObjectTypeComposer<any, TContext> {
|
9 |
| - return schemaComposer.getOrCreateOTC('PaginationInfo', tc => { |
10 |
| - tc.setDescription('Information about pagination.'); |
11 |
| - tc.addFields({ |
12 |
| - currentPage: { |
13 |
| - type: 'Int!', |
14 |
| - description: 'Current page number', |
15 |
| - }, |
16 |
| - perPage: { |
17 |
| - type: 'Int!', |
18 |
| - description: 'Number of items per page', |
19 |
| - }, |
20 |
| - pageCount: { |
21 |
| - type: 'Int', |
22 |
| - description: 'Total number of pages', |
23 |
| - }, |
24 |
| - itemCount: { |
25 |
| - type: 'Int', |
26 |
| - description: 'Total number of items', |
27 |
| - }, |
28 |
| - hasNextPage: { |
29 |
| - type: 'Boolean', |
30 |
| - description: 'When paginating forwards, are there more items?', |
31 |
| - }, |
32 |
| - hasPreviousPage: { |
33 |
| - type: 'Boolean', |
34 |
| - description: 'When paginating backwards, are there more items?', |
35 |
| - }, |
36 |
| - }); |
37 |
| - }); |
| 33 | + // Pagination Info can be overrided via SchemaComposer registry |
| 34 | + if (sc.hasInstance('PaginationInfo', ObjectTypeComposer)) { |
| 35 | + return (sc.get('PaginationInfo'): any); |
| 36 | + } |
| 37 | + sc.set('PaginationInfo', (PaginationInfoTC: any)); |
| 38 | + return (PaginationInfoTC: any); |
38 | 39 | }
|
39 | 40 |
|
40 | 41 | export function preparePaginationTC<TSource, TContext>(
|
|
0 commit comments