@@ -165,11 +165,13 @@ export interface ExecutionContext {
165
165
validatedExecutionArgs : ValidatedExecutionArgs ;
166
166
errors : Array < GraphQLError > | undefined ;
167
167
canceller : Canceller ;
168
+ completed : boolean ;
168
169
cancellableStreams : Set < CancellableStreamRecord > | undefined ;
169
170
}
170
171
171
172
interface IncrementalContext {
172
173
errors : Array < GraphQLError > | undefined ;
174
+ completed : boolean ;
173
175
deferUsageSet ?: DeferUsageSet | undefined ;
174
176
}
175
177
@@ -316,6 +318,7 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
316
318
validatedExecutionArgs,
317
319
errors : undefined ,
318
320
canceller : new Canceller ( validatedExecutionArgs . abortSignal ) ,
321
+ completed : false ,
319
322
cancellableStreams : undefined ,
320
323
} ;
321
324
try {
@@ -366,8 +369,12 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
366
369
367
370
if ( isPromise ( graphqlWrappedResult ) ) {
368
371
return graphqlWrappedResult . then (
369
- ( resolved ) => buildDataResponse ( exeContext , resolved ) ,
372
+ ( resolved ) => {
373
+ exeContext . completed = true ;
374
+ return buildDataResponse ( exeContext , resolved ) ;
375
+ } ,
370
376
( error : unknown ) => {
377
+ exeContext . completed = true ;
371
378
exeContext . canceller . unsubscribe ( ) ;
372
379
return {
373
380
data : null ,
@@ -376,8 +383,10 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
376
383
} ,
377
384
) ;
378
385
}
386
+ exeContext . completed = true ;
379
387
return buildDataResponse ( exeContext , graphqlWrappedResult ) ;
380
388
} catch ( error ) {
389
+ exeContext . completed = true ;
381
390
exeContext . canceller . unsubscribe ( ) ;
382
391
return { data : null , errors : withError ( exeContext . errors , error ) } ;
383
392
}
@@ -1754,6 +1763,10 @@ function completeObjectValue(
1754
1763
incrementalContext : IncrementalContext | undefined ,
1755
1764
deferMap : ReadonlyMap < DeferUsage , DeferredFragmentRecord > | undefined ,
1756
1765
) : PromiseOrValue < GraphQLWrappedResult < ObjMap < unknown > > > {
1766
+ if ( ( incrementalContext ?? exeContext ) . completed ) {
1767
+ throw new Error ( 'Completed, aborting.' ) ;
1768
+ }
1769
+
1757
1770
// If there is an isTypeOf predicate function, call it with the
1758
1771
// current result. If isTypeOf returns false, then raise an error rather
1759
1772
// than continuing execution.
@@ -2271,6 +2284,7 @@ function collectExecutionGroups(
2271
2284
groupedFieldSet ,
2272
2285
{
2273
2286
errors : undefined ,
2287
+ completed : false ,
2274
2288
deferUsageSet,
2275
2289
} ,
2276
2290
deferMap ,
@@ -2330,6 +2344,7 @@ function executeExecutionGroup(
2330
2344
deferMap ,
2331
2345
) ;
2332
2346
} catch ( error ) {
2347
+ incrementalContext . completed = true ;
2333
2348
return {
2334
2349
pendingExecutionGroup,
2335
2350
path : pathToArray ( path ) ,
@@ -2339,21 +2354,27 @@ function executeExecutionGroup(
2339
2354
2340
2355
if ( isPromise ( result ) ) {
2341
2356
return result . then (
2342
- ( resolved ) =>
2343
- buildCompletedExecutionGroup (
2357
+ ( resolved ) => {
2358
+ incrementalContext . completed = true ;
2359
+ return buildCompletedExecutionGroup (
2344
2360
incrementalContext . errors ,
2345
2361
pendingExecutionGroup ,
2346
2362
path ,
2347
2363
resolved ,
2348
- ) ,
2349
- ( error : unknown ) => ( {
2350
- pendingExecutionGroup,
2351
- path : pathToArray ( path ) ,
2352
- errors : withError ( incrementalContext . errors , error as GraphQLError ) ,
2353
- } ) ,
2364
+ ) ;
2365
+ } ,
2366
+ ( error : unknown ) => {
2367
+ incrementalContext . completed = true ;
2368
+ return {
2369
+ pendingExecutionGroup,
2370
+ path : pathToArray ( path ) ,
2371
+ errors : withError ( incrementalContext . errors , error as GraphQLError ) ,
2372
+ } ;
2373
+ } ,
2354
2374
) ;
2355
2375
}
2356
2376
2377
+ incrementalContext . completed = true ;
2357
2378
return buildCompletedExecutionGroup (
2358
2379
incrementalContext . errors ,
2359
2380
pendingExecutionGroup ,
@@ -2408,7 +2429,7 @@ function buildSyncStreamItemQueue(
2408
2429
initialPath ,
2409
2430
initialItem ,
2410
2431
exeContext ,
2411
- { errors : undefined } ,
2432
+ { errors : undefined , completed : false } ,
2412
2433
fieldDetailsList ,
2413
2434
info ,
2414
2435
itemType ,
@@ -2439,7 +2460,7 @@ function buildSyncStreamItemQueue(
2439
2460
itemPath ,
2440
2461
value ,
2441
2462
exeContext ,
2442
- { errors : undefined } ,
2463
+ { errors : undefined , completed : false } ,
2443
2464
fieldDetailsList ,
2444
2465
info ,
2445
2466
itemType ,
@@ -2531,7 +2552,7 @@ async function getNextAsyncStreamItemResult(
2531
2552
itemPath ,
2532
2553
iteration . value ,
2533
2554
exeContext ,
2534
- { errors : undefined } ,
2555
+ { errors : undefined , completed : false } ,
2535
2556
fieldDetailsList ,
2536
2557
info ,
2537
2558
itemType ,
@@ -2578,11 +2599,16 @@ function completeStreamItem(
2578
2599
incrementalContext ,
2579
2600
new Map ( ) ,
2580
2601
) . then (
2581
- ( resolvedItem ) =>
2582
- buildStreamItemResult ( incrementalContext . errors , resolvedItem ) ,
2583
- ( error : unknown ) => ( {
2584
- errors : withError ( incrementalContext . errors , error as GraphQLError ) ,
2585
- } ) ,
2602
+ ( resolvedItem ) => {
2603
+ incrementalContext . completed = true ;
2604
+ return buildStreamItemResult ( incrementalContext . errors , resolvedItem ) ;
2605
+ } ,
2606
+ ( error : unknown ) => {
2607
+ incrementalContext . completed = true ;
2608
+ return {
2609
+ errors : withError ( incrementalContext . errors , error as GraphQLError ) ,
2610
+ } ;
2611
+ } ,
2586
2612
) ;
2587
2613
}
2588
2614
@@ -2611,6 +2637,7 @@ function completeStreamItem(
2611
2637
result = { rawResult : null , incrementalDataRecords : undefined } ;
2612
2638
}
2613
2639
} catch ( error ) {
2640
+ incrementalContext . completed = true ;
2614
2641
return {
2615
2642
errors : withError ( incrementalContext . errors , error ) ,
2616
2643
} ;
@@ -2630,14 +2657,20 @@ function completeStreamItem(
2630
2657
return { rawResult : null , incrementalDataRecords : undefined } ;
2631
2658
} )
2632
2659
. then (
2633
- ( resolvedItem ) =>
2634
- buildStreamItemResult ( incrementalContext . errors , resolvedItem ) ,
2635
- ( error : unknown ) => ( {
2636
- errors : withError ( incrementalContext . errors , error as GraphQLError ) ,
2637
- } ) ,
2660
+ ( resolvedItem ) => {
2661
+ incrementalContext . completed = true ;
2662
+ return buildStreamItemResult ( incrementalContext . errors , resolvedItem ) ;
2663
+ } ,
2664
+ ( error : unknown ) => {
2665
+ incrementalContext . completed = true ;
2666
+ return {
2667
+ errors : withError ( incrementalContext . errors , error as GraphQLError ) ,
2668
+ } ;
2669
+ } ,
2638
2670
) ;
2639
2671
}
2640
2672
2673
+ incrementalContext . completed = true ;
2641
2674
return buildStreamItemResult ( incrementalContext . errors , result ) ;
2642
2675
}
2643
2676
0 commit comments