Skip to content

Commit e0cc0e7

Browse files
committed
extract completeListItemValue from completeListValue
1 parent 80325b5 commit e0cc0e7

File tree

1 file changed

+83
-46
lines changed

1 file changed

+83
-46
lines changed

src/execution/execute.ts

Lines changed: 83 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ function completeListValue(
10921092
// where the list contains no Promises by avoiding creating another Promise.
10931093
let containsPromise = false;
10941094
let previousAsyncPayloadRecord = asyncPayloadRecord;
1095-
const completedResults = [];
1095+
const completedResults: Array<unknown> = [];
10961096
let index = 0;
10971097
for (const item of result) {
10981098
// No need to modify the info object containing the path,
@@ -1119,61 +1119,98 @@ function completeListValue(
11191119
continue;
11201120
}
11211121

1122-
try {
1123-
let completedItem;
1124-
if (isPromise(item)) {
1125-
completedItem = item.then((resolved) =>
1126-
completeValue(
1127-
exeContext,
1128-
itemType,
1129-
fieldNodes,
1130-
info,
1131-
itemPath,
1132-
resolved,
1133-
asyncPayloadRecord,
1134-
),
1135-
);
1136-
} else {
1137-
completedItem = completeValue(
1122+
if (
1123+
completeListItemValue(
1124+
item,
1125+
completedResults,
1126+
errors,
1127+
exeContext,
1128+
itemType,
1129+
fieldNodes,
1130+
info,
1131+
itemPath,
1132+
asyncPayloadRecord,
1133+
)
1134+
) {
1135+
containsPromise = true;
1136+
}
1137+
1138+
index++;
1139+
}
1140+
1141+
return containsPromise ? Promise.all(completedResults) : completedResults;
1142+
}
1143+
1144+
/**
1145+
* Complete a list item value by adding it to the completed results.
1146+
*
1147+
* Returns true if the value is a Promise.
1148+
*/
1149+
function completeListItemValue(
1150+
item: unknown,
1151+
completedResults: Array<unknown>,
1152+
errors: Array<GraphQLError>,
1153+
exeContext: ExecutionContext,
1154+
itemType: GraphQLOutputType,
1155+
fieldNodes: ReadonlyArray<FieldNode>,
1156+
info: GraphQLResolveInfo,
1157+
itemPath: Path,
1158+
asyncPayloadRecord?: AsyncPayloadRecord,
1159+
): boolean {
1160+
try {
1161+
let completedItem;
1162+
if (isPromise(item)) {
1163+
completedItem = item.then((resolved) =>
1164+
completeValue(
11381165
exeContext,
11391166
itemType,
11401167
fieldNodes,
11411168
info,
11421169
itemPath,
1143-
item,
1170+
resolved,
11441171
asyncPayloadRecord,
1145-
);
1146-
}
1172+
),
1173+
);
1174+
} else {
1175+
completedItem = completeValue(
1176+
exeContext,
1177+
itemType,
1178+
fieldNodes,
1179+
info,
1180+
itemPath,
1181+
item,
1182+
asyncPayloadRecord,
1183+
);
1184+
}
11471185

1148-
if (isPromise(completedItem)) {
1149-
containsPromise = true;
1150-
// Note: we don't rely on a `catch` method, but we do expect "thenable"
1151-
// to take a second callback for the error case.
1152-
completedResults.push(
1153-
completedItem.then(undefined, (rawError) => {
1154-
const error = locatedError(
1155-
rawError,
1156-
fieldNodes,
1157-
pathToArray(itemPath),
1158-
);
1159-
const handledError = handleFieldError(error, itemType, errors);
1160-
filterSubsequentPayloads(exeContext, itemPath);
1161-
return handledError;
1162-
}),
1163-
);
1164-
} else {
1165-
completedResults.push(completedItem);
1166-
}
1167-
} catch (rawError) {
1168-
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1169-
const handledError = handleFieldError(error, itemType, errors);
1170-
filterSubsequentPayloads(exeContext, itemPath);
1171-
completedResults.push(handledError);
1186+
if (isPromise(completedItem)) {
1187+
// Note: we don't rely on a `catch` method, but we do expect "thenable"
1188+
// to take a second callback for the error case.
1189+
completedResults.push(
1190+
completedItem.then(undefined, (rawError) => {
1191+
const error = locatedError(
1192+
rawError,
1193+
fieldNodes,
1194+
pathToArray(itemPath),
1195+
);
1196+
const handledError = handleFieldError(error, itemType, errors);
1197+
filterSubsequentPayloads(exeContext, itemPath);
1198+
return handledError;
1199+
}),
1200+
);
1201+
1202+
return true;
11721203
}
1173-
index++;
1204+
1205+
completedResults.push(completedItem);
1206+
} catch (rawError) {
1207+
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1208+
const handledError = handleFieldError(error, itemType, errors);
1209+
filterSubsequentPayloads(exeContext, itemPath);
1210+
completedResults.push(handledError);
11741211
}
11751212

1176-
return containsPromise ? Promise.all(completedResults) : completedResults;
1213+
return false;
11771214
}
11781215

11791216
/**

0 commit comments

Comments
 (0)