@@ -165,11 +165,13 @@ export interface ExecutionContext {
165165 validatedExecutionArgs : ValidatedExecutionArgs ;
166166 errors : Array < GraphQLError > | undefined ;
167167 canceller : Canceller | undefined ;
168+ completed : boolean ;
168169 cancellableStreams : Set < CancellableStreamRecord > | undefined ;
169170}
170171
171172interface IncrementalContext {
172173 errors : Array < GraphQLError > | undefined ;
174+ completed : boolean ;
173175 deferUsageSet ?: DeferUsageSet | undefined ;
174176}
175177
@@ -317,6 +319,7 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
317319 validatedExecutionArgs,
318320 errors : undefined ,
319321 canceller : abortSignal ? new Canceller ( abortSignal ) : undefined ,
322+ completed : false ,
320323 cancellableStreams : undefined ,
321324 } ;
322325 try {
@@ -367,8 +370,12 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
367370
368371 if ( isPromise ( graphqlWrappedResult ) ) {
369372 return graphqlWrappedResult . then (
370- ( resolved ) => buildDataResponse ( exeContext , resolved ) ,
373+ ( resolved ) => {
374+ exeContext . completed = true ;
375+ return buildDataResponse ( exeContext , resolved ) ;
376+ } ,
371377 ( error : unknown ) => {
378+ exeContext . completed = true ;
372379 exeContext . canceller ?. unsubscribe ( ) ;
373380 return {
374381 data : null ,
@@ -377,8 +384,10 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
377384 } ,
378385 ) ;
379386 }
387+ exeContext . completed = true ;
380388 return buildDataResponse ( exeContext , graphqlWrappedResult ) ;
381389 } catch ( error ) {
390+ exeContext . completed = true ;
382391 exeContext . canceller ?. unsubscribe ( ) ;
383392 return { data : null , errors : withError ( exeContext . errors , error ) } ;
384393 }
@@ -1755,6 +1764,10 @@ function completeObjectValue(
17551764 incrementalContext : IncrementalContext | undefined ,
17561765 deferMap : ReadonlyMap < DeferUsage , DeferredFragmentRecord > | undefined ,
17571766) : PromiseOrValue < GraphQLWrappedResult < ObjMap < unknown > > > {
1767+ if ( ( incrementalContext ?? exeContext ) . completed ) {
1768+ throw new Error ( 'Completed, aborting.' ) ;
1769+ }
1770+
17581771 // If there is an isTypeOf predicate function, call it with the
17591772 // current result. If isTypeOf returns false, then raise an error rather
17601773 // than continuing execution.
@@ -2270,6 +2283,7 @@ function collectExecutionGroups(
22702283 groupedFieldSet ,
22712284 {
22722285 errors : undefined ,
2286+ completed : false ,
22732287 deferUsageSet,
22742288 } ,
22752289 deferMap ,
@@ -2329,6 +2343,7 @@ function executeExecutionGroup(
23292343 deferMap ,
23302344 ) ;
23312345 } catch ( error ) {
2346+ incrementalContext . completed = true ;
23322347 return {
23332348 pendingExecutionGroup,
23342349 path : pathToArray ( path ) ,
@@ -2338,21 +2353,27 @@ function executeExecutionGroup(
23382353
23392354 if ( isPromise ( result ) ) {
23402355 return result . then (
2341- ( resolved ) =>
2342- buildCompletedExecutionGroup (
2356+ ( resolved ) => {
2357+ incrementalContext . completed = true ;
2358+ return buildCompletedExecutionGroup (
23432359 incrementalContext . errors ,
23442360 pendingExecutionGroup ,
23452361 path ,
23462362 resolved ,
2347- ) ,
2348- ( error : unknown ) => ( {
2349- pendingExecutionGroup,
2350- path : pathToArray ( path ) ,
2351- errors : withError ( incrementalContext . errors , error as GraphQLError ) ,
2352- } ) ,
2363+ ) ;
2364+ } ,
2365+ ( error : unknown ) => {
2366+ incrementalContext . completed = true ;
2367+ return {
2368+ pendingExecutionGroup,
2369+ path : pathToArray ( path ) ,
2370+ errors : withError ( incrementalContext . errors , error as GraphQLError ) ,
2371+ } ;
2372+ } ,
23532373 ) ;
23542374 }
23552375
2376+ incrementalContext . completed = true ;
23562377 return buildCompletedExecutionGroup (
23572378 incrementalContext . errors ,
23582379 pendingExecutionGroup ,
@@ -2407,7 +2428,7 @@ function buildSyncStreamItemQueue(
24072428 initialPath ,
24082429 initialItem ,
24092430 exeContext ,
2410- { errors : undefined } ,
2431+ { errors : undefined , completed : false } ,
24112432 fieldDetailsList ,
24122433 info ,
24132434 itemType ,
@@ -2438,7 +2459,7 @@ function buildSyncStreamItemQueue(
24382459 itemPath ,
24392460 value ,
24402461 exeContext ,
2441- { errors : undefined } ,
2462+ { errors : undefined , completed : false } ,
24422463 fieldDetailsList ,
24432464 info ,
24442465 itemType ,
@@ -2530,7 +2551,7 @@ async function getNextAsyncStreamItemResult(
25302551 itemPath ,
25312552 iteration . value ,
25322553 exeContext ,
2533- { errors : undefined } ,
2554+ { errors : undefined , completed : false } ,
25342555 fieldDetailsList ,
25352556 info ,
25362557 itemType ,
@@ -2577,11 +2598,16 @@ function completeStreamItem(
25772598 incrementalContext ,
25782599 new Map ( ) ,
25792600 ) . then (
2580- ( resolvedItem ) =>
2581- buildStreamItemResult ( incrementalContext . errors , resolvedItem ) ,
2582- ( error : unknown ) => ( {
2583- errors : withError ( incrementalContext . errors , error as GraphQLError ) ,
2584- } ) ,
2601+ ( resolvedItem ) => {
2602+ incrementalContext . completed = true ;
2603+ return buildStreamItemResult ( incrementalContext . errors , resolvedItem ) ;
2604+ } ,
2605+ ( error : unknown ) => {
2606+ incrementalContext . completed = true ;
2607+ return {
2608+ errors : withError ( incrementalContext . errors , error as GraphQLError ) ,
2609+ } ;
2610+ } ,
25852611 ) ;
25862612 }
25872613
@@ -2610,6 +2636,7 @@ function completeStreamItem(
26102636 result = { rawResult : null , incrementalDataRecords : undefined } ;
26112637 }
26122638 } catch ( error ) {
2639+ incrementalContext . completed = true ;
26132640 return {
26142641 errors : withError ( incrementalContext . errors , error ) ,
26152642 } ;
@@ -2629,14 +2656,20 @@ function completeStreamItem(
26292656 return { rawResult : null , incrementalDataRecords : undefined } ;
26302657 } )
26312658 . then (
2632- ( resolvedItem ) =>
2633- buildStreamItemResult ( incrementalContext . errors , resolvedItem ) ,
2634- ( error : unknown ) => ( {
2635- errors : withError ( incrementalContext . errors , error as GraphQLError ) ,
2636- } ) ,
2659+ ( resolvedItem ) => {
2660+ incrementalContext . completed = true ;
2661+ return buildStreamItemResult ( incrementalContext . errors , resolvedItem ) ;
2662+ } ,
2663+ ( error : unknown ) => {
2664+ incrementalContext . completed = true ;
2665+ return {
2666+ errors : withError ( incrementalContext . errors , error as GraphQLError ) ,
2667+ } ;
2668+ } ,
26372669 ) ;
26382670 }
26392671
2672+ incrementalContext . completed = true ;
26402673 return buildStreamItemResult ( incrementalContext . errors , result ) ;
26412674}
26422675
0 commit comments