Skip to content

Commit 3dd146d

Browse files
authored
refactor(execution): separate response construction from finish checks (#4673)
1 parent 37a6299 commit 3dd146d

3 files changed

Lines changed: 24 additions & 28 deletions

File tree

src/execution/Executor.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export class Executor<
234234
abortReason: unknown;
235235
sharedExecutionContext: SharedExecutionContext;
236236
collectedErrors: CollectedErrors;
237-
abortResultPromise: ((reason?: unknown) => void) | undefined;
237+
abortResultPromise: (() => void) | undefined;
238238
resolverAbortController: AbortController | undefined;
239239
getAbortSignal: () => AbortSignal | undefined;
240240
getAsyncHelpers: () => GraphQLResolveInfoHelpers;
@@ -285,6 +285,7 @@ export class Executor<
285285
removeExternalAbortListener?.();
286286
};
287287

288+
let result: PromiseOrValue<ObjMap<unknown>>;
288289
try {
289290
const {
290291
schema,
@@ -314,7 +315,7 @@ export class Executor<
314315
hideSuggestions,
315316
);
316317

317-
const result = this.executeCollectedRootFields(
318+
result = this.executeCollectedRootFields(
318319
operation.operation,
319320
rootType,
320321
rootValue,
@@ -336,26 +337,22 @@ export class Executor<
336337
);
337338
this.sharedExecutionContext.asyncWorkTracker.add(promise);
338339
const { promise: cancellablePromise, abort: abortResultPromise } =
339-
withCancellation(promise);
340-
this.abortResultPromise = abortResultPromise;
341-
if (this.aborted) {
340+
withCancellation(promise.then((resolved) => this.finish(resolved)));
341+
this.abortResultPromise = () => {
342342
abortResultPromise(this.abortReason);
343+
};
344+
if (this.aborted) {
345+
this.abortResultPromise();
343346
}
344347
return cancellablePromise;
345348
}
346349
maybeRemoveExternalAbortListener();
347-
return this.buildResponse(result);
348350
} catch (error) {
349351
maybeRemoveExternalAbortListener();
350352
this.collectedErrors.add(ensureGraphQLError(error), undefined);
351-
return this.buildResponse(null);
352-
}
353-
}
354-
355-
throwIfAborted(): void {
356-
if (this.aborted) {
357-
throw this.abortReason;
353+
return this.finish(this.buildResponse(null));
358354
}
355+
return this.finish(this.buildResponse(result));
359356
}
360357

361358
abort(reason?: unknown): void {
@@ -366,13 +363,16 @@ export class Executor<
366363
if (reason !== undefined) {
367364
this.abortReason = reason;
368365
}
369-
this.abortResultPromise?.(this.abortReason);
366+
this.abortResultPromise?.();
370367
this.resolverAbortController?.abort(this.abortReason);
371368
}
372369

373-
finish(): void {
374-
this.throwIfAborted();
370+
finish<T>(result: T): T {
371+
if (this.aborted) {
372+
throw this.abortReason;
373+
}
375374
this.aborted = true;
375+
return result;
376376
}
377377

378378
getFinishSharedExecution(): () => void {
@@ -403,10 +403,8 @@ export class Executor<
403403
data: ObjMap<unknown> | null,
404404
): ExecutionResult | TAlternativeInitialResponse {
405405
this.getFinishSharedExecution()();
406-
this.finish();
407406
const errors = this.collectedErrors.errors;
408-
const result = errors.length ? { errors, data } : { data };
409-
return result;
407+
return errors.length ? { errors, data } : { data };
410408
}
411409

412410
executeCollectedRootFields(

src/execution/incremental/IncrementalExecutor.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ export class IncrementalExecutor<
349349
return super.buildResponse(data);
350350
}
351351

352-
this.finish();
353352
const errors = this.collectedErrors.errors;
354353
invariant(data !== null);
355354
const incrementalPublisher = new IncrementalPublisher();
@@ -573,15 +572,14 @@ export class IncrementalExecutor<
573572
path: Path | undefined,
574573
result: ObjMap<unknown>,
575574
): ExecutionGroupResult {
576-
this.finish();
577575
const data = result;
578576
const errors = this.collectedErrors.errors;
579-
return {
577+
return this.finish({
580578
value: errors.length
581579
? { deliveryGroups, path: pathToArray(path), errors, data }
582580
: { deliveryGroups, path: pathToArray(path), data },
583581
work: this.getIncrementalWork(),
584-
};
582+
});
585583
}
586584

587585
getIncrementalWork(): IncrementalWork {
@@ -912,13 +910,14 @@ export class IncrementalExecutor<
912910
}
913911

914912
buildStreamItemResult(result: unknown): StreamItemResult {
915-
this.finish();
916913
const item = result;
917914
const errors = this.collectedErrors.errors;
918915
const work = this.getIncrementalWork();
919-
return errors.length > 0
920-
? { value: { item, errors }, work }
921-
: { value: { item }, work };
916+
return this.finish(
917+
errors.length > 0
918+
? { value: { item, errors }, work }
919+
: { value: { item }, work },
920+
);
922921
}
923922
}
924923

src/execution/legacyIncremental/BranchingIncrementalExecutor.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ export class BranchingIncrementalExecutor extends IncrementalExecutor<LegacyExpe
197197
return super.buildResponse(data);
198198
}
199199

200-
this.finish();
201200
const errors = this.collectedErrors.errors;
202201
invariant(data !== null);
203202
const incrementalPublisher = new BranchingIncrementalPublisher();

0 commit comments

Comments
 (0)