@@ -135,11 +135,16 @@ const query = new GraphQLObjectType({
135
135
136
136
const schema = new GraphQLSchema ( { query } ) ;
137
137
138
- async function complete ( document : DocumentNode , rootValue : unknown = { hero } ) {
138
+ async function complete (
139
+ document : DocumentNode ,
140
+ rootValue : unknown = { hero } ,
141
+ enableEarlyExecution = false ,
142
+ ) {
139
143
const result = await experimentalExecuteIncrementally ( {
140
144
schema,
141
145
document,
142
146
rootValue,
147
+ enableEarlyExecution,
143
148
} ) ;
144
149
145
150
if ( 'initialResult' in result ) {
@@ -247,6 +252,118 @@ describe('Execute: defer directive', () => {
247
252
} ,
248
253
] ) ;
249
254
} ) ;
255
+ it ( 'Does not execute deferred fragments early when not specified' , async ( ) => {
256
+ const document = parse ( `
257
+ query HeroNameQuery {
258
+ hero {
259
+ id
260
+ ...NameFragment @defer
261
+ }
262
+ }
263
+ fragment NameFragment on Hero {
264
+ name
265
+ }
266
+ ` ) ;
267
+ const order : Array < string > = [ ] ;
268
+ const result = await complete ( document , {
269
+ hero : {
270
+ ...hero ,
271
+ id : async ( ) => {
272
+ await resolveOnNextTick ( ) ;
273
+ await resolveOnNextTick ( ) ;
274
+ order . push ( 'slow-id' ) ;
275
+ return hero . id ;
276
+ } ,
277
+ name : ( ) => {
278
+ order . push ( 'fast-name' ) ;
279
+ return hero . name ;
280
+ } ,
281
+ } ,
282
+ } ) ;
283
+
284
+ expectJSON ( result ) . toDeepEqual ( [
285
+ {
286
+ data : {
287
+ hero : {
288
+ id : '1' ,
289
+ } ,
290
+ } ,
291
+ pending : [ { id : '0' , path : [ 'hero' ] } ] ,
292
+ hasNext : true ,
293
+ } ,
294
+ {
295
+ incremental : [
296
+ {
297
+ data : {
298
+ name : 'Luke' ,
299
+ } ,
300
+ id : '0' ,
301
+ } ,
302
+ ] ,
303
+ completed : [ { id : '0' } ] ,
304
+ hasNext : false ,
305
+ } ,
306
+ ] ) ;
307
+ expect ( order ) . to . deep . equal ( [ 'slow-id' , 'fast-name' ] ) ;
308
+ } ) ;
309
+ it ( 'Does execute deferred fragments early when specified' , async ( ) => {
310
+ const document = parse ( `
311
+ query HeroNameQuery {
312
+ hero {
313
+ id
314
+ ...NameFragment @defer
315
+ }
316
+ }
317
+ fragment NameFragment on Hero {
318
+ name
319
+ }
320
+ ` ) ;
321
+ const order : Array < string > = [ ] ;
322
+ const result = await complete (
323
+ document ,
324
+ {
325
+ hero : {
326
+ ...hero ,
327
+ id : async ( ) => {
328
+ await resolveOnNextTick ( ) ;
329
+ await resolveOnNextTick ( ) ;
330
+ order . push ( 'slow-id' ) ;
331
+ return hero . id ;
332
+ } ,
333
+ name : ( ) => {
334
+ order . push ( 'fast-name' ) ;
335
+ return hero . name ;
336
+ } ,
337
+ } ,
338
+ } ,
339
+ true ,
340
+ ) ;
341
+
342
+ expectJSON ( result ) . toDeepEqual ( [
343
+ {
344
+ data : {
345
+ hero : {
346
+ id : '1' ,
347
+ } ,
348
+ } ,
349
+ pending : [ { id : '0' , path : [ 'hero' ] } ] ,
350
+ hasNext : true ,
351
+ } ,
352
+ {
353
+ incremental : [
354
+ {
355
+ data : {
356
+ name : 'Luke' ,
357
+ } ,
358
+ id : '0' ,
359
+ } ,
360
+ ] ,
361
+ completed : [ { id : '0' } ] ,
362
+ hasNext : false ,
363
+ } ,
364
+ ] ) ;
365
+ expect ( order ) . to . deep . equal ( [ 'fast-name' , 'slow-id' ] ) ;
366
+ } ) ;
250
367
it ( 'Can defer fragments on the top level Query field' , async ( ) => {
251
368
const document = parse ( `
252
369
query HeroNameQuery {
@@ -1492,20 +1609,24 @@ describe('Execute: defer directive', () => {
1492
1609
}
1493
1610
}
1494
1611
` ) ;
1495
- const result = await complete ( document , {
1496
- a : {
1497
- b : {
1498
- c : {
1499
- d : 'd' ,
1500
- nonNullErrorField : async ( ) => {
1501
- await resolveOnNextTick ( ) ;
1502
- return null ;
1612
+ const result = await complete (
1613
+ document ,
1614
+ {
1615
+ a : {
1616
+ b : {
1617
+ c : {
1618
+ d : 'd' ,
1619
+ nonNullErrorField : async ( ) => {
1620
+ await resolveOnNextTick ( ) ;
1621
+ return null ;
1622
+ } ,
1503
1623
} ,
1504
1624
} ,
1625
+ someField : 'someField' ,
1505
1626
} ,
1506
- someField : 'someField' ,
1507
1627
} ,
1508
- } ) ;
1628
+ true ,
1629
+ ) ;
1509
1630
expectJSON ( result ) . toDeepEqual ( [
1510
1631
{
1511
1632
data : {
@@ -1564,12 +1685,16 @@ describe('Execute: defer directive', () => {
1564
1685
}
1565
1686
}
1566
1687
` ) ;
1567
- const result = await complete ( document , {
1568
- hero : {
1569
- ...hero ,
1570
- nonNullName : ( ) => null ,
1688
+ const result = await complete (
1689
+ document ,
1690
+ {
1691
+ hero : {
1692
+ ...hero ,
1693
+ nonNullName : ( ) => null ,
1694
+ } ,
1571
1695
} ,
1572
- } ) ;
1696
+ true ,
1697
+ ) ;
1573
1698
expectJSON ( result ) . toDeepEqual ( {
1574
1699
data : {
1575
1700
hero : null ,
@@ -1596,12 +1721,16 @@ describe('Execute: defer directive', () => {
1596
1721
}
1597
1722
}
1598
1723
` ) ;
1599
- const result = await complete ( document , {
1600
- hero : {
1601
- ...hero ,
1602
- nonNullName : ( ) => null ,
1724
+ const result = await complete (
1725
+ document ,
1726
+ {
1727
+ hero : {
1728
+ ...hero ,
1729
+ nonNullName : ( ) => null ,
1730
+ } ,
1603
1731
} ,
1604
- } ) ;
1732
+ true ,
1733
+ ) ;
1605
1734
expectJSON ( result ) . toDeepEqual ( [
1606
1735
{
1607
1736
data : { } ,
0 commit comments