Skip to content

Commit 55f5643

Browse files
committed
fix: make PaginationInfo as a global type
It fixes the problem when merging several SchemaComposer instance.
1 parent 27b3c37 commit 55f5643

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

src/types/__tests__/preparePaginationType-test.js renamed to src/__tests__/preparePaginationType-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { ObjectTypeComposer } from 'graphql-compose';
44
import { GraphQLNonNull, getNamedType, GraphQLInt, GraphQLList } from 'graphql-compose/lib/graphql';
5-
import { UserTC } from '../../__mocks__/User';
5+
import { UserTC } from '../__mocks__/User';
66
import { preparePaginationTC, preparePaginationInfoTC } from '../preparePaginationType';
77

88
describe('preparePaginationTC()', () => {

src/paginationResolver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
ProjectionType,
99
} from 'graphql-compose';
1010
import type { GraphQLResolveInfo } from 'graphql-compose/lib/graphql';
11-
import { preparePaginationTC } from './types/preparePaginationType';
11+
import { preparePaginationTC } from './preparePaginationType';
1212

1313
export const DEFAULT_RESOLVER_NAME = 'pagination';
1414
export const DEFAULT_PER_PAGE = 20;

src/types/preparePaginationType.js renamed to src/preparePaginationType.js

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
/* @flow */
22
/* eslint-disable arrow-body-style */
33

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+
`);
529

630
export function preparePaginationInfoTC<TContext>(
7-
schemaComposer: SchemaComposer<TContext>
31+
sc: SchemaComposer<TContext>
832
): 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);
3839
}
3940

4041
export function preparePaginationTC<TSource, TContext>(

0 commit comments

Comments
 (0)