Skip to content

Commit a537996

Browse files
committed
introduce handleRawError
to include filtering
1 parent 78d7a21 commit a537996

File tree

1 file changed

+66
-30
lines changed

1 file changed

+66
-30
lines changed

src/execution/execute.ts

Lines changed: 66 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -748,17 +748,27 @@ function executeField(
748748
// Note: we don't rely on a `catch` method, but we do expect "thenable"
749749
// to take a second callback for the error case.
750750
return completed.then(undefined, (rawError) => {
751-
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
752-
addError(rawError, fieldNodes, returnType, path, errors);
753-
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
751+
handleRawError(
752+
rawError,
753+
exeContext,
754+
fieldNodes,
755+
returnType,
756+
path,
757+
asyncPayloadRecord,
758+
);
754759
return null;
755760
});
756761
}
757762
return completed;
758763
} catch (rawError) {
759-
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
760-
addError(rawError, fieldNodes, returnType, path, errors);
761-
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
764+
handleRawError(
765+
rawError,
766+
exeContext,
767+
fieldNodes,
768+
returnType,
769+
path,
770+
asyncPayloadRecord,
771+
);
762772
return null;
763773
}
764774
}
@@ -790,6 +800,19 @@ export function buildResolveInfo(
790800
};
791801
}
792802

803+
function handleRawError(
804+
rawError: unknown,
805+
exeContext: ExecutionContext,
806+
fieldNodes: ReadonlyArray<FieldNode>,
807+
returnType: GraphQLOutputType,
808+
path: Path,
809+
asyncPayloadRecord: AsyncPayloadRecord | undefined,
810+
): void {
811+
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
812+
addError(rawError, fieldNodes, returnType, path, errors);
813+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
814+
}
815+
793816
function addError(
794817
rawError: unknown,
795818
fieldNodes: ReadonlyArray<FieldNode>,
@@ -948,9 +971,14 @@ async function completePromisedValue(
948971
}
949972
return completed;
950973
} catch (rawError) {
951-
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
952-
addError(rawError, fieldNodes, returnType, path, errors);
953-
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
974+
handleRawError(
975+
rawError,
976+
exeContext,
977+
fieldNodes,
978+
returnType,
979+
path,
980+
asyncPayloadRecord,
981+
);
954982
return null;
955983
}
956984
}
@@ -1025,7 +1053,6 @@ async function completeAsyncIteratorValue(
10251053
iterator: AsyncIterator<unknown>,
10261054
asyncPayloadRecord?: AsyncPayloadRecord,
10271055
): Promise<ReadonlyArray<unknown>> {
1028-
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
10291056
const stream = getStreamValues(exeContext, fieldNodes, path);
10301057
let containsPromise = false;
10311058
const completedResults: Array<unknown> = [];
@@ -1058,6 +1085,7 @@ async function completeAsyncIteratorValue(
10581085
// eslint-disable-next-line no-await-in-loop
10591086
iteration = await iterator.next();
10601087
} catch (rawError) {
1088+
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
10611089
addError(rawError, fieldNodes, itemType, itemPath, errors);
10621090
completedResults.push(null);
10631091
break;
@@ -1071,7 +1099,6 @@ async function completeAsyncIteratorValue(
10711099
completeListItemValue(
10721100
iteration.value,
10731101
completedResults,
1074-
errors,
10751102
exeContext,
10761103
itemType,
10771104
fieldNodes,
@@ -1101,7 +1128,6 @@ function completeListValue(
11011128
asyncPayloadRecord?: AsyncPayloadRecord,
11021129
): PromiseOrValue<ReadonlyArray<unknown>> {
11031130
const itemType = returnType.ofType;
1104-
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
11051131

11061132
if (isAsyncIterable(result)) {
11071133
const iterator = result[Symbol.asyncIterator]();
@@ -1160,7 +1186,6 @@ function completeListValue(
11601186
completeListItemValue(
11611187
item,
11621188
completedResults,
1163-
errors,
11641189
exeContext,
11651190
itemType,
11661191
fieldNodes,
@@ -1186,7 +1211,6 @@ function completeListValue(
11861211
function completeListItemValue(
11871212
item: unknown,
11881213
completedResults: Array<unknown>,
1189-
errors: Array<GraphQLError>,
11901214
exeContext: ExecutionContext,
11911215
itemType: GraphQLOutputType,
11921216
fieldNodes: ReadonlyArray<FieldNode>,
@@ -1226,8 +1250,14 @@ function completeListItemValue(
12261250
// to take a second callback for the error case.
12271251
completedResults.push(
12281252
completedItem.then(undefined, (rawError) => {
1229-
addError(rawError, fieldNodes, itemType, itemPath, errors);
1230-
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
1253+
handleRawError(
1254+
rawError,
1255+
exeContext,
1256+
fieldNodes,
1257+
itemType,
1258+
itemPath,
1259+
asyncPayloadRecord,
1260+
);
12311261
return null;
12321262
}),
12331263
);
@@ -1237,8 +1267,14 @@ function completeListItemValue(
12371267

12381268
completedResults.push(completedItem);
12391269
} catch (rawError) {
1240-
addError(rawError, fieldNodes, itemType, itemPath, errors);
1241-
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
1270+
handleRawError(
1271+
rawError,
1272+
exeContext,
1273+
fieldNodes,
1274+
itemType,
1275+
itemPath,
1276+
asyncPayloadRecord,
1277+
);
12421278
completedResults.push(null);
12431279
}
12441280

@@ -1853,15 +1889,15 @@ function executeStreamField(
18531889
asyncPayloadRecord,
18541890
);
18551891
} catch (rawError) {
1856-
addError(
1892+
handleRawError(
18571893
rawError,
1894+
exeContext,
18581895
fieldNodes,
18591896
itemType,
18601897
itemPath,
1861-
asyncPayloadRecord.errors,
1898+
asyncPayloadRecord,
18621899
);
18631900
completedItem = null;
1864-
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
18651901
}
18661902
} catch (error) {
18671903
asyncPayloadRecord.errors.push(error);
@@ -1873,14 +1909,14 @@ function executeStreamField(
18731909
if (isPromise(completedItem)) {
18741910
const completedItems = completedItem
18751911
.then(undefined, (rawError) => {
1876-
addError(
1912+
handleRawError(
18771913
rawError,
1914+
exeContext,
18781915
fieldNodes,
18791916
itemType,
18801917
itemPath,
1881-
asyncPayloadRecord.errors,
1918+
asyncPayloadRecord,
18821919
);
1883-
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
18841920
return null;
18851921
})
18861922
.then(
@@ -1945,27 +1981,27 @@ async function executeStreamIteratorItem(
19451981

19461982
if (isPromise(completedItem)) {
19471983
completedItem = completedItem.then(undefined, (rawError) => {
1948-
addError(
1984+
handleRawError(
19491985
rawError,
1986+
exeContext,
19501987
fieldNodes,
19511988
itemType,
19521989
itemPath,
1953-
asyncPayloadRecord.errors,
1990+
asyncPayloadRecord,
19541991
);
1955-
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
19561992
return null;
19571993
});
19581994
}
19591995
return { done: false, value: completedItem };
19601996
} catch (rawError) {
1961-
addError(
1997+
handleRawError(
19621998
rawError,
1999+
exeContext,
19632000
fieldNodes,
19642001
itemType,
19652002
itemPath,
1966-
asyncPayloadRecord.errors,
2003+
asyncPayloadRecord,
19672004
);
1968-
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
19692005
return { done: false, value: null };
19702006
}
19712007
}

0 commit comments

Comments
 (0)