@@ -98,25 +98,37 @@ describe('connectionResolver', () => {
98
98
} ) ;
99
99
} ) ;
100
100
101
- describe ( 'call of findMany resolver ' , ( ) => {
101
+ describe ( 'call of resolvers ' , ( ) => {
102
102
let spyResolveParams ;
103
103
let mockedConnectionResolver ;
104
+ let findManyResolverCalled ;
105
+ let countResolverCalled ;
104
106
105
107
beforeEach ( ( ) => {
108
+ findManyResolverCalled = false ;
109
+ countResolverCalled = false ;
106
110
const mockedFindMany = userTypeComposer . getResolver ( 'findMany' )
107
111
. wrapResolve ( ( next ) => ( resolveParams ) => {
112
+ findManyResolverCalled = true ;
113
+ spyResolveParams = resolveParams ;
114
+ return next ( resolveParams ) ;
115
+ } ) ;
116
+ const mockedCount = userTypeComposer . getResolver ( 'findMany' )
117
+ . wrapResolve ( ( next ) => ( resolveParams ) => {
118
+ countResolverCalled = true ;
108
119
spyResolveParams = resolveParams ;
109
120
return next ( resolveParams ) ;
110
121
} ) ;
111
122
userTypeComposer . setResolver ( 'mockedFindMany' , mockedFindMany ) ;
123
+ userTypeComposer . setResolver ( 'mockedCount' , mockedCount ) ;
112
124
mockedConnectionResolver = prepareConnectionResolver ( userTypeComposer , {
113
- countResolverName : 'count ' ,
125
+ countResolverName : 'mockedCount ' ,
114
126
findResolverName : 'mockedFindMany' ,
115
127
sort : sortOptions ,
116
128
} ) ;
117
129
} ) ;
118
130
119
- it ( 'should pass args.sort' , async ( ) => {
131
+ it ( 'should pass to findMany args.sort' , async ( ) => {
120
132
const result = await mockedConnectionResolver . resolve ( {
121
133
args : {
122
134
sort : { name : 1 } ,
@@ -126,7 +138,7 @@ describe('connectionResolver', () => {
126
138
expect ( spyResolveParams ) . have . deep . property ( 'args.sort.name' , 1 ) ;
127
139
} ) ;
128
140
129
- it ( 'should pass projection edges.node on top level' , async ( ) => {
141
+ it ( 'should pass to findMany projection edges.node on top level' , async ( ) => {
130
142
const result = await mockedConnectionResolver . resolve ( {
131
143
args : { } ,
132
144
projection : {
@@ -142,7 +154,7 @@ describe('connectionResolver', () => {
142
154
expect ( spyResolveParams ) . have . deep . property ( 'projection.age' , true ) ;
143
155
} ) ;
144
156
145
- it ( 'should pass custom projections to top level' , async ( ) => {
157
+ it ( 'should pass to findMany custom projections to top level' , async ( ) => {
146
158
const result = await mockedConnectionResolver . resolve ( {
147
159
args : { } ,
148
160
projection : {
@@ -153,6 +165,93 @@ describe('connectionResolver', () => {
153
165
expect ( spyResolveParams ) . have . deep . property ( 'projection.score' )
154
166
. to . deep . equal ( { $meta : 'textScore' } ) ;
155
167
} ) ;
168
+
169
+ it ( 'should call count but not findMany when only count is projected' , async ( ) => {
170
+ const result = await mockedConnectionResolver . resolve ( {
171
+ args : { } ,
172
+ projection : {
173
+ count : true
174
+ } ,
175
+ } ) ;
176
+ expect ( countResolverCalled , 'count resolver called' ) . to . be . true ;
177
+ expect ( findManyResolverCalled , 'findMany resolver called' ) . to . be . false ;
178
+ } ) ;
179
+
180
+ it ( 'should call count and findMany resolver when not only count is projected' , async ( ) => {
181
+ const result = await mockedConnectionResolver . resolve ( {
182
+ args : { } ,
183
+ projection : {
184
+ count : true ,
185
+ edges : {
186
+ node : {
187
+ name : true ,
188
+ age : true
189
+ }
190
+ }
191
+ } ,
192
+ } ) ;
193
+ expect ( countResolverCalled , 'count resolver called' ) . to . be . true ;
194
+ expect ( findManyResolverCalled , 'findMany resolver called' ) . to . be . true ;
195
+ } ) ;
196
+
197
+ it ( 'should call findMany and not count when arbitrary top level fields are projected without count' , async ( ) => {
198
+ const result = await mockedConnectionResolver . resolve ( {
199
+ args : { } ,
200
+ projection : {
201
+ name : true ,
202
+ age : true
203
+ } ,
204
+ } ) ;
205
+ expect ( countResolverCalled , 'count resolver called' ) . to . be . false ;
206
+ expect ( findManyResolverCalled , 'findMany resolver called' ) . to . be . true ;
207
+ } ) ;
208
+
209
+ it ( 'should call findMany and count when arbitrary top level fields are projected with count' , async ( ) => {
210
+ const result = await mockedConnectionResolver . resolve ( {
211
+ args : { } ,
212
+ projection : {
213
+ count : true ,
214
+ name : true ,
215
+ age : true
216
+ } ,
217
+ } ) ;
218
+ expect ( countResolverCalled , 'count resolver called' ) . to . be . true ;
219
+ expect ( findManyResolverCalled , 'findMany resolver called' ) . to . be . true ;
220
+ } ) ;
221
+
222
+ it ( 'should call count and findMany resolver when last arg is used but not first arg' , async ( ) => {
223
+ const result = await mockedConnectionResolver . resolve ( {
224
+ args : {
225
+ last : 1
226
+ } ,
227
+ projection : {
228
+ edges : {
229
+ node : {
230
+ name : true ,
231
+ age : true
232
+ }
233
+ }
234
+ } ,
235
+ } ) ;
236
+ expect ( countResolverCalled , 'count resolver called' ) . to . be . true ;
237
+ expect ( findManyResolverCalled , 'findMany resolver called' ) . to . be . true ;
238
+ } ) ;
239
+
240
+ it ( 'should call findMany but not count resolver when first arg is used' , async ( ) => {
241
+ const result = await mockedConnectionResolver . resolve ( {
242
+ args : { first : 1 } ,
243
+ projection : {
244
+ edges : {
245
+ node : {
246
+ name : true ,
247
+ age : true
248
+ }
249
+ }
250
+ } ,
251
+ } ) ;
252
+ expect ( countResolverCalled , 'count resolver called' ) . to . be . false ;
253
+ expect ( findManyResolverCalled , 'findMany resolver called' ) . to . be . true ;
254
+ } ) ;
156
255
} ) ;
157
256
158
257
describe ( 'prepareRawQuery()' , ( ) => {
@@ -453,7 +552,14 @@ describe('connectionResolver', () => {
453
552
first : 5 ,
454
553
last : 3 ,
455
554
} ,
456
- projection : { count : 1 } ,
555
+ projection : {
556
+ count : true ,
557
+ edges : {
558
+ node : {
559
+ name : true
560
+ }
561
+ }
562
+ } ,
457
563
} ) ;
458
564
expect ( result ) . deep . property ( 'edges' ) . to . have . length ( 3 ) ;
459
565
expect ( result ) . deep . property ( 'edges.0.node.id' ) . equals ( 8 ) ;
@@ -503,7 +609,14 @@ describe('connectionResolver', () => {
503
609
sort : sortOptions . ID_ASC . value ,
504
610
first : 100 ,
505
611
} ,
506
- projection : { count : 1 } ,
612
+ projection : {
613
+ count : true ,
614
+ edges : {
615
+ node : {
616
+ name : true
617
+ }
618
+ }
619
+ } ,
507
620
} ) ;
508
621
expect ( result ) . deep . property ( 'edges' ) . to . have . length ( 8 ) ;
509
622
expect ( result ) . deep . property ( 'edges.0.node' )
@@ -612,7 +725,14 @@ describe('connectionResolver', () => {
612
725
first : 5 ,
613
726
last : 3 ,
614
727
} ,
615
- projection : { count : 1 } ,
728
+ projection : {
729
+ count : true ,
730
+ edges : {
731
+ node : {
732
+ id : true
733
+ }
734
+ }
735
+ } ,
616
736
} ) ;
617
737
expect ( result ) . deep . property ( 'edges' ) . to . have . length ( 3 ) ;
618
738
expect ( result ) . deep . property ( 'edges.0.node.id' ) . equals ( 8 ) ;
0 commit comments