Skip to content

Commit 78d7a21

Browse files
committed
introduce addError instead of handleFieldError
addError calls locatedError and then adds the error as previously in handleFieldError, throwing if the type is non-null. As opposed to handleFieldError, it does not return null, so as to require the calling function to be more explicit about the new value.
1 parent 307c5e5 commit 78d7a21

File tree

1 file changed

+52
-43
lines changed

1 file changed

+52
-43
lines changed

src/execution/execute.ts

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,6 @@ function executeField(
687687
path: Path,
688688
asyncPayloadRecord?: AsyncPayloadRecord,
689689
): PromiseOrValue<unknown> {
690-
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
691690
const fieldName = fieldNodes[0].name.value;
692691
const fieldDef = exeContext.schema.getField(parentType, fieldName);
693692
if (!fieldDef) {
@@ -749,18 +748,18 @@ function executeField(
749748
// Note: we don't rely on a `catch` method, but we do expect "thenable"
750749
// to take a second callback for the error case.
751750
return completed.then(undefined, (rawError) => {
752-
const error = locatedError(rawError, fieldNodes, pathToArray(path));
753-
const handledError = handleFieldError(error, returnType, errors);
751+
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
752+
addError(rawError, fieldNodes, returnType, path, errors);
754753
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
755-
return handledError;
754+
return null;
756755
});
757756
}
758757
return completed;
759758
} catch (rawError) {
760-
const error = locatedError(rawError, fieldNodes, pathToArray(path));
761-
const handledError = handleFieldError(error, returnType, errors);
759+
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
760+
addError(rawError, fieldNodes, returnType, path, errors);
762761
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
763-
return handledError;
762+
return null;
764763
}
765764
}
766765

@@ -791,11 +790,15 @@ export function buildResolveInfo(
791790
};
792791
}
793792

794-
function handleFieldError(
795-
error: GraphQLError,
793+
function addError(
794+
rawError: unknown,
795+
fieldNodes: ReadonlyArray<FieldNode>,
796796
returnType: GraphQLOutputType,
797+
path: Path,
797798
errors: Array<GraphQLError>,
798-
): null {
799+
): void {
800+
const error = locatedError(rawError, fieldNodes, pathToArray(path));
801+
799802
// If the field type is non-nullable, then it is resolved without any
800803
// protection from errors, however it still properly locates the error.
801804
if (isNonNullType(returnType)) {
@@ -805,7 +808,6 @@ function handleFieldError(
805808
// Otherwise, error protection is applied, logging the error and resolving
806809
// a null value for this field if one is encountered.
807810
errors.push(error);
808-
return null;
809811
}
810812

811813
/**
@@ -947,10 +949,9 @@ async function completePromisedValue(
947949
return completed;
948950
} catch (rawError) {
949951
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
950-
const error = locatedError(rawError, fieldNodes, pathToArray(path));
951-
const handledError = handleFieldError(error, returnType, errors);
952+
addError(rawError, fieldNodes, returnType, path, errors);
952953
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
953-
return handledError;
954+
return null;
954955
}
955956
}
956957

@@ -1057,8 +1058,8 @@ async function completeAsyncIteratorValue(
10571058
// eslint-disable-next-line no-await-in-loop
10581059
iteration = await iterator.next();
10591060
} catch (rawError) {
1060-
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1061-
completedResults.push(handleFieldError(error, itemType, errors));
1061+
addError(rawError, fieldNodes, itemType, itemPath, errors);
1062+
completedResults.push(null);
10621063
break;
10631064
}
10641065

@@ -1225,14 +1226,9 @@ function completeListItemValue(
12251226
// to take a second callback for the error case.
12261227
completedResults.push(
12271228
completedItem.then(undefined, (rawError) => {
1228-
const error = locatedError(
1229-
rawError,
1230-
fieldNodes,
1231-
pathToArray(itemPath),
1232-
);
1233-
const handledError = handleFieldError(error, itemType, errors);
1229+
addError(rawError, fieldNodes, itemType, itemPath, errors);
12341230
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
1235-
return handledError;
1231+
return null;
12361232
}),
12371233
);
12381234

@@ -1241,10 +1237,9 @@ function completeListItemValue(
12411237

12421238
completedResults.push(completedItem);
12431239
} catch (rawError) {
1244-
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1245-
const handledError = handleFieldError(error, itemType, errors);
1240+
addError(rawError, fieldNodes, itemType, itemPath, errors);
12461241
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
1247-
completedResults.push(handledError);
1242+
completedResults.push(null);
12481243
}
12491244

12501245
return false;
@@ -1858,12 +1853,14 @@ function executeStreamField(
18581853
asyncPayloadRecord,
18591854
);
18601855
} catch (rawError) {
1861-
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1862-
completedItem = handleFieldError(
1863-
error,
1856+
addError(
1857+
rawError,
1858+
fieldNodes,
18641859
itemType,
1860+
itemPath,
18651861
asyncPayloadRecord.errors,
18661862
);
1863+
completedItem = null;
18671864
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
18681865
}
18691866
} catch (error) {
@@ -1876,14 +1873,15 @@ function executeStreamField(
18761873
if (isPromise(completedItem)) {
18771874
const completedItems = completedItem
18781875
.then(undefined, (rawError) => {
1879-
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1880-
const handledError = handleFieldError(
1881-
error,
1876+
addError(
1877+
rawError,
1878+
fieldNodes,
18821879
itemType,
1880+
itemPath,
18831881
asyncPayloadRecord.errors,
18841882
);
18851883
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
1886-
return handledError;
1884+
return null;
18871885
})
18881886
.then(
18891887
(value) => [value],
@@ -1915,10 +1913,15 @@ async function executeStreamIteratorItem(
19151913
try {
19161914
iteration = await iterator.next();
19171915
} catch (rawError) {
1918-
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1919-
const value = handleFieldError(error, itemType, asyncPayloadRecord.errors);
1916+
addError(
1917+
rawError,
1918+
fieldNodes,
1919+
itemType,
1920+
itemPath,
1921+
asyncPayloadRecord.errors,
1922+
);
19201923
// don't continue if iterator throws
1921-
return { done: true, value };
1924+
return { done: true, value: null };
19221925
}
19231926

19241927
const { done, value: item } = iteration;
@@ -1942,22 +1945,28 @@ async function executeStreamIteratorItem(
19421945

19431946
if (isPromise(completedItem)) {
19441947
completedItem = completedItem.then(undefined, (rawError) => {
1945-
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1946-
const handledError = handleFieldError(
1947-
error,
1948+
addError(
1949+
rawError,
1950+
fieldNodes,
19481951
itemType,
1952+
itemPath,
19491953
asyncPayloadRecord.errors,
19501954
);
19511955
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
1952-
return handledError;
1956+
return null;
19531957
});
19541958
}
19551959
return { done: false, value: completedItem };
19561960
} catch (rawError) {
1957-
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1958-
const value = handleFieldError(error, itemType, asyncPayloadRecord.errors);
1961+
addError(
1962+
rawError,
1963+
fieldNodes,
1964+
itemType,
1965+
itemPath,
1966+
asyncPayloadRecord.errors,
1967+
);
19591968
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
1960-
return { done: false, value };
1969+
return { done: false, value: null };
19611970
}
19621971
}
19631972

0 commit comments

Comments
 (0)