Skip to content

Commit 61be526

Browse files
committed
refactor: introduce completeIterableValue (#4052)
refactoring that will streamline when we introduce two versions of this function to optimize the loop when not streaming depends on #4051
1 parent d46098b commit 61be526

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/execution/execute.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,12 +897,34 @@ function completeListValue(
897897
);
898898
}
899899

900+
return completeIterableValue(
901+
exeContext,
902+
itemType,
903+
fieldGroup,
904+
info,
905+
path,
906+
result,
907+
);
908+
}
909+
910+
function completeIterableValue(
911+
exeContext: ExecutionContext,
912+
itemType: GraphQLOutputType,
913+
fieldGroup: FieldGroup,
914+
info: GraphQLResolveInfo,
915+
path: Path,
916+
items: Iterable<unknown>,
917+
): PromiseOrValue<ReadonlyArray<unknown>> {
900918
// This is specified as a simple map, however we're optimizing the path
901919
// where the list contains no Promises by avoiding creating another Promise.
902920
let containsPromise = false;
903921
const completedResults: Array<unknown> = [];
904922
let index = 0;
905-
for (const item of result) {
923+
const iterator = items[Symbol.iterator]();
924+
let iteration = iterator.next();
925+
while (!iteration.done) {
926+
const item = iteration.value;
927+
906928
// No need to modify the info object containing the path,
907929
// since from here on it is not ever accessed by resolver functions.
908930
const itemPath = addPath(path, index, undefined);
@@ -934,6 +956,7 @@ function completeListValue(
934956
}
935957

936958
index++;
959+
iteration = iterator.next();
937960
}
938961

939962
return containsPromise ? Promise.all(completedResults) : completedResults;

0 commit comments

Comments
 (0)