Skip to content

Commit ef36972

Browse files
committed
do not use undefined for empty errors array
1 parent 02d302e commit ef36972

File tree

3 files changed

+26
-39
lines changed

3 files changed

+26
-39
lines changed

src/execution/IncrementalPublisher.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
export function buildIncrementalResponse(
3333
context: IncrementalPublisherContext,
3434
result: ObjMap<unknown>,
35-
errors: ReadonlyArray<GraphQLError> | undefined,
35+
errors: ReadonlyArray<GraphQLError>,
3636
incrementalDataRecords: ReadonlyArray<IncrementalDataRecord>,
3737
): ExperimentalIncrementalExecutionResults {
3838
const incrementalPublisher = new IncrementalPublisher(context);
@@ -73,7 +73,7 @@ class IncrementalPublisher {
7373

7474
buildResponse(
7575
data: ObjMap<unknown>,
76-
errors: ReadonlyArray<GraphQLError> | undefined,
76+
errors: ReadonlyArray<GraphQLError>,
7777
incrementalDataRecords: ReadonlyArray<IncrementalDataRecord>,
7878
): ExperimentalIncrementalExecutionResults {
7979
const newRootNodes = this._incrementalGraph.getNewRootNodes(
@@ -82,10 +82,9 @@ class IncrementalPublisher {
8282

8383
const pending = this._toPendingResults(newRootNodes);
8484

85-
const initialResult: InitialIncrementalExecutionResult =
86-
errors === undefined
87-
? { data, pending, hasNext: true }
88-
: { errors, data, pending, hasNext: true };
85+
const initialResult: InitialIncrementalExecutionResult = errors.length
86+
? { errors, data, pending, hasNext: true }
87+
: { data, pending, hasNext: true };
8988

9089
return {
9190
initialResult,

src/execution/execute.ts

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,15 @@ export interface ValidatedExecutionArgs {
168168

169169
export interface ExecutionContext {
170170
validatedExecutionArgs: ValidatedExecutionArgs;
171-
errors: Array<GraphQLError> | undefined;
171+
errors: Array<GraphQLError>;
172172
abortSignalListener: AbortSignalListener | undefined;
173173
completed: boolean;
174174
cancellableStreams: Set<CancellableStreamRecord> | undefined;
175175
errorPropagation: boolean;
176176
}
177177

178178
interface IncrementalContext {
179-
errors: Array<GraphQLError> | undefined;
179+
errors: Array<GraphQLError>;
180180
completed: boolean;
181181
deferUsageSet?: DeferUsageSet | undefined;
182182
}
@@ -337,7 +337,7 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
337337
const abortSignal = validatedExecutionArgs.abortSignal;
338338
const exeContext: ExecutionContext = {
339339
validatedExecutionArgs,
340-
errors: undefined,
340+
errors: [],
341341
abortSignalListener: abortSignal
342342
? new AbortSignalListener(abortSignal)
343343
: undefined,
@@ -394,7 +394,7 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
394394
exeContext.abortSignalListener?.disconnect();
395395
return {
396396
data: null,
397-
errors: withError(exeContext.errors, error as GraphQLError),
397+
errors: [...exeContext.errors, error as GraphQLError],
398398
};
399399
},
400400
);
@@ -406,17 +406,10 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
406406
// TODO: add test case for synchronous null bubbling to root with cancellation
407407
/* c8 ignore next */
408408
exeContext.abortSignalListener?.disconnect();
409-
return { data: null, errors: withError(exeContext.errors, error) };
409+
return { data: null, errors: [...exeContext.errors, error] };
410410
}
411411
}
412412

413-
function withError(
414-
errors: Array<GraphQLError> | undefined,
415-
error: GraphQLError,
416-
): ReadonlyArray<GraphQLError> {
417-
return errors === undefined ? [error] : [...errors, error];
418-
}
419-
420413
function buildDataResponse(
421414
exeContext: ExecutionContext,
422415
graphqlWrappedResult: GraphQLWrappedResult<ObjMap<unknown>>,
@@ -425,7 +418,7 @@ function buildDataResponse(
425418
const errors = exeContext.errors;
426419
if (incrementalDataRecords === undefined) {
427420
exeContext.abortSignalListener?.disconnect();
428-
return errors !== undefined ? { errors, data } : { data };
421+
return errors.length ? { errors, data } : { data };
429422
}
430423

431424
return buildIncrementalResponse(
@@ -1005,12 +998,7 @@ function handleFieldError(
1005998
// Otherwise, error protection is applied, logging the error and resolving
1006999
// a null value for this field if one is encountered.
10071000
const context = incrementalContext ?? exeContext;
1008-
let errors = context.errors;
1009-
if (errors === undefined) {
1010-
errors = [];
1011-
context.errors = errors;
1012-
}
1013-
errors.push(error);
1001+
context.errors.push(error);
10141002
}
10151003

10161004
/**
@@ -2397,7 +2385,7 @@ function collectExecutionGroups(
23972385
path,
23982386
groupedFieldSet,
23992387
{
2400-
errors: undefined,
2388+
errors: [],
24012389
completed: false,
24022390
deferUsageSet,
24032391
},
@@ -2462,7 +2450,7 @@ function executeExecutionGroup(
24622450
return {
24632451
pendingExecutionGroup,
24642452
path: pathToArray(path),
2465-
errors: withError(incrementalContext.errors, error),
2453+
errors: [...incrementalContext.errors, error],
24662454
};
24672455
}
24682456

@@ -2482,7 +2470,7 @@ function executeExecutionGroup(
24822470
return {
24832471
pendingExecutionGroup,
24842472
path: pathToArray(path),
2485-
errors: withError(incrementalContext.errors, error as GraphQLError),
2473+
errors: [...incrementalContext.errors, error as GraphQLError],
24862474
};
24872475
},
24882476
);
@@ -2498,7 +2486,7 @@ function executeExecutionGroup(
24982486
}
24992487

25002488
function buildCompletedExecutionGroup(
2501-
errors: ReadonlyArray<GraphQLError> | undefined,
2489+
errors: ReadonlyArray<GraphQLError>,
25022490
pendingExecutionGroup: PendingExecutionGroup,
25032491
path: Path | undefined,
25042492
result: GraphQLWrappedResult<ObjMap<unknown>>,
@@ -2507,7 +2495,7 @@ function buildCompletedExecutionGroup(
25072495
return {
25082496
pendingExecutionGroup,
25092497
path: pathToArray(path),
2510-
result: errors === undefined ? { data } : { data, errors },
2498+
result: errors.length ? { errors, data } : { data },
25112499
incrementalDataRecords,
25122500
};
25132501
}
@@ -2543,7 +2531,7 @@ function buildSyncStreamItemQueue(
25432531
initialPath,
25442532
initialItem,
25452533
exeContext,
2546-
{ errors: undefined, completed: false },
2534+
{ errors: [], completed: false },
25472535
fieldDetailsList,
25482536
info,
25492537
itemType,
@@ -2560,7 +2548,7 @@ function buildSyncStreamItemQueue(
25602548
/* c8 ignore next 6 */
25612549
if (currentStreamItem instanceof BoxedPromiseOrValue) {
25622550
const result = currentStreamItem.value;
2563-
if (!isPromise(result) && result.errors !== undefined) {
2551+
if (!isPromise(result) && result.item === undefined) {
25642552
break;
25652553
}
25662554
}
@@ -2574,7 +2562,7 @@ function buildSyncStreamItemQueue(
25742562
itemPath,
25752563
value,
25762564
exeContext,
2577-
{ errors: undefined, completed: false },
2565+
{ errors: [], completed: false },
25782566
fieldDetailsList,
25792567
info,
25802568
itemType,
@@ -2666,7 +2654,7 @@ async function getNextAsyncStreamItemResult(
26662654
itemPath,
26672655
iteration.value,
26682656
exeContext,
2669-
{ errors: undefined, completed: false },
2657+
{ errors: [], completed: false },
26702658
fieldDetailsList,
26712659
info,
26722660
itemType,
@@ -2724,7 +2712,7 @@ function completeStreamItem(
27242712
(error: unknown) => {
27252713
incrementalContext.completed = true;
27262714
return {
2727-
errors: withError(incrementalContext.errors, error as GraphQLError),
2715+
errors: [...incrementalContext.errors, error as GraphQLError],
27282716
};
27292717
},
27302718
);
@@ -2757,7 +2745,7 @@ function completeStreamItem(
27572745
} catch (error) {
27582746
incrementalContext.completed = true;
27592747
return {
2760-
errors: withError(incrementalContext.errors, error),
2748+
errors: [...incrementalContext.errors, error],
27612749
};
27622750
}
27632751

@@ -2782,7 +2770,7 @@ function completeStreamItem(
27822770
(error: unknown) => {
27832771
incrementalContext.completed = true;
27842772
return {
2785-
errors: withError(incrementalContext.errors, error as GraphQLError),
2773+
errors: [...incrementalContext.errors, error as GraphQLError],
27862774
};
27872775
},
27882776
);
@@ -2793,7 +2781,7 @@ function completeStreamItem(
27932781
}
27942782

27952783
function buildStreamItemResult(
2796-
errors: ReadonlyArray<GraphQLError> | undefined,
2784+
errors: ReadonlyArray<GraphQLError>,
27972785
result: GraphQLWrappedResult<unknown>,
27982786
): StreamItemResult {
27992787
const { rawResult: item, incrementalDataRecords } = result;

src/execution/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ export function isDeferredFragmentRecord(
248248
export interface StreamItemResult {
249249
item?: unknown;
250250
incrementalDataRecords?: ReadonlyArray<IncrementalDataRecord> | undefined;
251-
errors?: ReadonlyArray<GraphQLError> | undefined;
251+
errors?: ReadonlyArray<GraphQLError>;
252252
}
253253

254254
export type StreamItemRecord = ThunkIncrementalResult<StreamItemResult>;

0 commit comments

Comments
 (0)