22/* eslint-disable no-param-reassign, no-use-before-define */
33
44import { 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' ;
118import preparePaginationType from './types/paginationType' ;
129
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+ | } ;
1441
1542export function preparePaginationResolver < TSource , TContext > (
1643 typeComposer : TypeComposer ,
@@ -78,7 +105,7 @@ export function preparePaginationResolver<TSource, TContext>(
78105 perPage : {
79106 type : 'Int' ,
80107 description : '' ,
81- defaultValue : opts . perPage || defaultPerPage ,
108+ defaultValue : opts . perPage || DEFAULT_PER_PAGE ,
82109 } ,
83110 ...additionalArgs ,
84111 } ,
@@ -95,7 +122,7 @@ export function preparePaginationResolver<TSource, TContext>(
95122 if ( page <= 0 ) {
96123 throw new Error ( 'Argument `page` should be positive number.' ) ;
97124 }
98- const perPage = parseInt ( args . perPage , 10 ) || opts . perPage || defaultPerPage ;
125+ const perPage = parseInt ( args . perPage , 10 ) || opts . perPage || DEFAULT_PER_PAGE ;
99126 if ( perPage <= 0 ) {
100127 throw new Error ( 'Argument `perPage` should be positive number.' ) ;
101128 }
@@ -147,7 +174,7 @@ export function preparePaginationResolver<TSource, TContext>(
147174 }
148175
149176 return Promise . all ( [ findManyPromise , countPromise ] ) . then ( ( [ items , count ] ) => {
150- const result : GraphQLPaginationType = {
177+ const result : PaginationType = {
151178 count,
152179 items : items . length > limit ? items . slice ( 0 , limit ) : items ,
153180 pageInfo : {
0 commit comments