@@ -4,13 +4,13 @@ import { execute, ExecutionResult, parse } from 'graphql'
4
4
import AutoPaginationTransform from '../src'
5
5
6
6
describe ( 'Auto Pagination' , ( ) => {
7
- const users = new Array ( 20 ) . fill ( { } ) . map ( ( _ , i ) => ( { id : ( i + 1 ) . toString ( ) , name : `User ${ i + 1 } ` } ) )
8
- const LIMIT = 3
7
+ const users = new Array ( 20000 ) . fill ( { } ) . map ( ( _ , i ) => ( { id : ( i + 1 ) . toString ( ) , name : `User ${ i + 1 } ` } ) )
8
+ const usersOdd = users . filter ( ( _ , i ) => i % 2 === 1 )
9
9
const schema = makeExecutableSchema ( {
10
10
typeDefs : /* GraphQL */ `
11
11
type Query {
12
12
_meta: Meta
13
- users(first: Int = ${ LIMIT } , skip: Int = 0): [User!]!
13
+ users(first: Int = ${ 1000 } , skip: Int = 0, odd: Boolean, where: WhereInput ): [User!]!
14
14
}
15
15
type User {
16
16
id: ID!
@@ -22,14 +22,27 @@ describe('Auto Pagination', () => {
22
22
type Block {
23
23
number: Int
24
24
}
25
+ input WhereInput {
26
+ id_gte: ID
27
+ }
25
28
` ,
26
29
resolvers : {
27
30
Query : {
28
- users : ( _ , { first = LIMIT , skip = 0 } ) => {
29
- if ( first > LIMIT ) {
30
- throw new Error ( `You cannot request more than ${ LIMIT } users; you requested ${ first } ` )
31
+ users : ( _ , { first = 1000 , skip = 0 , odd, where } ) => {
32
+ if ( first > 1000 ) {
33
+ throw new Error ( `You cannot request more than 1000 users; you requested ${ first } ` )
34
+ }
35
+ if ( skip > 5000 ) {
36
+ throw new Error ( `You cannot skip more than 5000 users; you requested ${ skip } ` )
37
+ }
38
+ let usersSlice = users
39
+ if ( odd ) {
40
+ usersSlice = usersOdd
41
+ }
42
+ if ( where ?. id_gte ) {
43
+ usersSlice = users . slice ( where . id_gte )
31
44
}
32
- return users . slice ( skip , skip + first )
45
+ return usersSlice . slice ( skip , skip + first )
33
46
} ,
34
47
_meta : ( ) => ( {
35
48
block : {
@@ -41,18 +54,12 @@ describe('Auto Pagination', () => {
41
54
} )
42
55
const wrappedSchema = wrapSchema ( {
43
56
schema,
44
- transforms : [
45
- new AutoPaginationTransform ( {
46
- config : {
47
- limitOfRecords : LIMIT ,
48
- } ,
49
- } ) ,
50
- ] ,
57
+ transforms : [ new AutoPaginationTransform ( ) ] ,
51
58
} )
52
59
it ( 'should give correct numbers of results if first arg are higher than given limit' , async ( ) => {
53
60
const query = /* GraphQL */ `
54
61
query {
55
- users(first: 10 ) {
62
+ users(first: 2000 ) {
56
63
id
57
64
name
58
65
}
@@ -62,13 +69,13 @@ describe('Auto Pagination', () => {
62
69
schema : wrappedSchema ,
63
70
document : parse ( query ) ,
64
71
} )
65
- expect ( result . data ?. users ) . toHaveLength ( 10 )
66
- expect ( result . data ?. users ) . toEqual ( users . slice ( 0 , 10 ) )
72
+ expect ( result . data ?. users ) . toHaveLength ( 2000 )
73
+ expect ( result . data ?. users ) . toEqual ( users . slice ( 0 , 2000 ) )
67
74
} )
68
75
it ( 'should respect skip argument' , async ( ) => {
69
76
const query = /* GraphQL */ `
70
77
query {
71
- users(first: 10 , skip: 1) {
78
+ users(first: 2000 , skip: 1) {
72
79
id
73
80
name
74
81
}
@@ -78,8 +85,8 @@ describe('Auto Pagination', () => {
78
85
schema : wrappedSchema ,
79
86
document : parse ( query ) ,
80
87
} )
81
- expect ( result . data ?. users ) . toHaveLength ( 10 )
82
- expect ( result . data ?. users ) . toEqual ( users . slice ( 1 , 11 ) )
88
+ expect ( result . data ?. users ) . toHaveLength ( 2000 )
89
+ expect ( result . data ?. users ) . toEqual ( users . slice ( 1 , 2001 ) )
83
90
} )
84
91
it ( 'should work with the values under the limit' , async ( ) => {
85
92
const query = /* GraphQL */ `
@@ -105,7 +112,7 @@ describe('Auto Pagination', () => {
105
112
number
106
113
}
107
114
}
108
- users(first: 10 ) {
115
+ users(first: 2000 ) {
109
116
id
110
117
name
111
118
}
@@ -116,7 +123,39 @@ describe('Auto Pagination', () => {
116
123
document : parse ( query ) ,
117
124
} )
118
125
expect ( result . data ?. _meta ?. block ?. number ) . toBeDefined ( )
119
- expect ( result . data ?. users ) . toHaveLength ( 10 )
120
- expect ( result . data ?. users ) . toEqual ( users . slice ( 0 , 10 ) )
126
+ expect ( result . data ?. users ) . toHaveLength ( 2000 )
127
+ expect ( result . data ?. users ) . toEqual ( users . slice ( 0 , 2000 ) )
128
+ } )
129
+ it ( 'should respect other arguments' , async ( ) => {
130
+ const query = /* GraphQL */ `
131
+ query {
132
+ users(first: 2000, odd: true) {
133
+ id
134
+ name
135
+ }
136
+ }
137
+ `
138
+ const result : ExecutionResult < any > = await execute ( {
139
+ schema : wrappedSchema ,
140
+ document : parse ( query ) ,
141
+ } )
142
+ expect ( result . data ?. users ) . toHaveLength ( 2000 )
143
+ expect ( result . data ?. users ) . toEqual ( usersOdd . slice ( 0 , 2000 ) )
144
+ } )
145
+ it ( 'should make queries serially if skip limit reaches the limit' , async ( ) => {
146
+ const query = /* GraphQL */ `
147
+ query {
148
+ users(first: 15000) {
149
+ id
150
+ name
151
+ }
152
+ }
153
+ `
154
+ const result : ExecutionResult < any > = await execute ( {
155
+ schema : wrappedSchema ,
156
+ document : parse ( query ) ,
157
+ } )
158
+ expect ( result . data ?. users ) . toHaveLength ( 15000 )
159
+ expect ( result . data ?. users ) . toEqual ( users . slice ( 0 , 15000 ) )
121
160
} )
122
161
} )
0 commit comments