2
2
/* eslint-disable no-param-reassign, no-use-before-define */
3
3
4
4
import { Resolver , TypeComposer } from 'graphql-compose' ;
5
- import type {
6
- ResolveParams ,
7
- PaginationResolveParams ,
8
- GraphQLPaginationType ,
9
- ComposeWithPaginationOpts ,
10
- } from './definition' ;
5
+ import type { ResolveParams , ProjectionType } from 'graphql-compose' ;
6
+ import type { GraphQLResolveInfo } from 'graphql-compose/lib/graphql' ;
7
+ import type { ComposeWithPaginationOpts } from './composeWithPagination' ;
11
8
import preparePaginationType from './types/paginationType' ;
12
9
13
- const defaultPerPage = 20 ;
10
+ const DEFAULT_PER_PAGE = 20 ;
11
+
12
+ export type PaginationResolveParams < TSource , TContext > = {
13
+ source : TSource ,
14
+ args : {
15
+ page ?: ?number ,
16
+ perPage ?: ?number ,
17
+ sort ?: any ,
18
+ filter ?: { [ fieldName : string ] : any } ,
19
+ [ argName : string ] : any ,
20
+ } ,
21
+ context : TContext ,
22
+ info : GraphQLResolveInfo ,
23
+ projection : $Shape < ProjectionType > ,
24
+ [ opt : string ] : any ,
25
+ } ;
26
+
27
+ export type PaginationType = { |
28
+ count : number ,
29
+ items : any [ ] ,
30
+ pageInfo : PaginationInfoType ,
31
+ | } ;
32
+
33
+ export type PaginationInfoType = { |
34
+ currentPage : number ,
35
+ perPage : number ,
36
+ itemCount : number ,
37
+ pageCount : number ,
38
+ hasPreviousPage : boolean ,
39
+ hasNextPage : boolean ,
40
+ | } ;
14
41
15
42
export function preparePaginationResolver < TSource , TContext > (
16
43
typeComposer : TypeComposer ,
@@ -78,7 +105,7 @@ export function preparePaginationResolver<TSource, TContext>(
78
105
perPage : {
79
106
type : 'Int' ,
80
107
description : '' ,
81
- defaultValue : opts . perPage || defaultPerPage ,
108
+ defaultValue : opts . perPage || DEFAULT_PER_PAGE ,
82
109
} ,
83
110
...additionalArgs ,
84
111
} ,
@@ -95,7 +122,7 @@ export function preparePaginationResolver<TSource, TContext>(
95
122
if ( page <= 0 ) {
96
123
throw new Error ( 'Argument `page` should be positive number.' ) ;
97
124
}
98
- const perPage = parseInt ( args . perPage , 10 ) || opts . perPage || defaultPerPage ;
125
+ const perPage = parseInt ( args . perPage , 10 ) || opts . perPage || DEFAULT_PER_PAGE ;
99
126
if ( perPage <= 0 ) {
100
127
throw new Error ( 'Argument `perPage` should be positive number.' ) ;
101
128
}
@@ -147,7 +174,7 @@ export function preparePaginationResolver<TSource, TContext>(
147
174
}
148
175
149
176
return Promise . all ( [ findManyPromise , countPromise ] ) . then ( ( [ items , count ] ) => {
150
- const result : GraphQLPaginationType = {
177
+ const result : PaginationType = {
151
178
count,
152
179
items : items . length > limit ? items . slice ( 0 , limit ) : items ,
153
180
pageInfo : {
0 commit comments