Skip to content

Commit 8fa2b9f

Browse files
committed
extract completeListItemValue from completeListValue
1 parent 41bc274 commit 8fa2b9f

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
@@ -1096,7 +1096,7 @@ function completeListValue(
10961096
// where the list contains no Promises by avoiding creating another Promise.
10971097
let containsPromise = false;
10981098
let previousAsyncPayloadRecord = asyncPayloadRecord;
1099-
const completedResults = [];
1099+
const completedResults: Array<unknown> = [];
11001100
let index = 0;
11011101
for (const item of result) {
11021102
// No need to modify the info object containing the path,
@@ -1123,61 +1123,98 @@ function completeListValue(
11231123
continue;
11241124
}
11251125

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

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

1180-
return containsPromise ? Promise.all(completedResults) : completedResults;
1217+
return false;
11811218
}
11821219

11831220
/**

0 commit comments

Comments
 (0)