@@ -377,9 +377,8 @@ export interface StreamItemResult {
377377export class Executor {
378378 validatedExecutionArgs : ValidatedExecutionArgs ;
379379 deferUsageSet ?: DeferUsageSet | undefined ;
380- onExternalAbort : ( ( ) => void ) | undefined ;
381380 finished : boolean ;
382- abortControllers : Set < AbortController > ;
381+ resolverAbortControllers : Set < AbortController > ;
383382 collectedErrors : CollectedErrors ;
384383 groups : Array < DeliveryGroup > ;
385384 tasks : Array < ExecutionGroup > ;
@@ -403,9 +402,8 @@ export class Executor {
403402 ) {
404403 this . validatedExecutionArgs = validatedExecutionArgs ;
405404 this . deferUsageSet = deferUsageSet ;
406- this . onExternalAbort = undefined ;
407405 this . finished = false ;
408- this . abortControllers = new Set ( ) ;
406+ this . resolverAbortControllers = new Set ( ) ;
409407 this . collectedErrors = new CollectedErrors ( ) ;
410408 this . groups = [ ] ;
411409 this . tasks = [ ] ;
@@ -439,17 +437,22 @@ export class Executor {
439437 > {
440438 const validatedExecutionArgs = this . validatedExecutionArgs ;
441439 const externalAbortSignal = validatedExecutionArgs . externalAbortSignal ;
440+ let removeAbortListener : ( ( ) => void ) | undefined ;
442441 if ( externalAbortSignal ) {
443442 if ( externalAbortSignal . aborted ) {
444443 throw new Error ( externalAbortSignal . reason ) ;
445444 }
446- const onExternalAbort = ( ) => {
447- this . cancel ( externalAbortSignal . reason ) ;
448- } ;
445+ const onExternalAbort = ( ) => this . cancel ( externalAbortSignal . reason ) ;
446+ removeAbortListener = ( ) =>
447+ externalAbortSignal . removeEventListener ( 'abort' , onExternalAbort ) ;
449448 externalAbortSignal . addEventListener ( 'abort' , onExternalAbort ) ;
450- this . onExternalAbort = onExternalAbort ;
451449 }
452450
451+ const onFinish = ( ) => {
452+ removeAbortListener ?.( ) ;
453+ this . finish ( ) ;
454+ } ;
455+
453456 try {
454457 const {
455458 schema,
@@ -490,11 +493,11 @@ export class Executor {
490493 if ( isPromise ( result ) ) {
491494 const promise = result . then (
492495 ( data ) => {
493- this . finish ( ) ;
496+ onFinish ( ) ;
494497 return this . buildResponse ( data ) ;
495498 } ,
496499 ( error : unknown ) => {
497- this . finish ( ) ;
500+ onFinish ( ) ;
498501 this . collectedErrors . add ( error as GraphQLError , undefined ) ;
499502 return this . buildResponse ( null ) ;
500503 } ,
@@ -503,9 +506,11 @@ export class Executor {
503506 ? cancellablePromise ( promise , externalAbortSignal )
504507 : promise ;
505508 }
509+ onFinish ( ) ;
506510 return this . buildResponse ( result ) ;
507511 } catch ( error ) {
508512 this . collectedErrors . add ( error as GraphQLError , undefined ) ;
513+ onFinish ( ) ;
509514 return this . buildResponse ( null ) ;
510515 }
511516 }
@@ -523,24 +528,20 @@ export class Executor {
523528 }
524529
525530 finish ( reason ?: unknown ) : void {
526- if ( this . finished ) {
527- return ;
531+ if ( ! this . finished ) {
532+ this . finished = true ;
533+ this . triggerResolverAbortSignals ( reason ) ;
528534 }
529- this . finished = true ;
530- const { abortControllers, onExternalAbort } = this ;
535+ }
536+
537+ triggerResolverAbortSignals ( reason ?: unknown ) : void {
538+ const { resolverAbortControllers } = this ;
531539 const finishReason =
532540 reason ?? new Error ( 'Execution has already completed.' ) ;
533- for ( const abortController of abortControllers ) {
541+ for ( const abortController of resolverAbortControllers ) {
534542 abortController . abort ( finishReason ) ;
535543 }
536- if ( onExternalAbort ) {
537- this . validatedExecutionArgs . externalAbortSignal ?. removeEventListener (
538- 'abort' ,
539- onExternalAbort ,
540- ) ;
541- }
542544 }
543-
544545 /**
545546 * Given a completed execution context and data, build the `{ errors, data }`
546547 * response defined by the "Response" section of the GraphQL specification.
@@ -788,11 +789,11 @@ export class Executor {
788789 throw new Error ( 'Execution has already completed.' ) ;
789790 }
790791 const abortController = new AbortController ( ) ;
791- this . abortControllers . add ( abortController ) ;
792+ this . resolverAbortControllers . add ( abortController ) ;
792793 return {
793794 abortSignal : abortController . signal ,
794795 unregister : ( ) => {
795- this . abortControllers . delete ( abortController ) ;
796+ this . resolverAbortControllers . delete ( abortController ) ;
796797 } ,
797798 } ;
798799 } ,
0 commit comments