Skip to content

Commit f0ae2dc

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 ce41e44 commit f0ae2dc

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
@@ -866,12 +866,34 @@ function completeListValue(
866866
);
867867
}
868868

869+
return completeIterableValue(
870+
exeContext,
871+
itemType,
872+
fieldGroup,
873+
info,
874+
path,
875+
result,
876+
);
877+
}
878+
879+
function completeIterableValue(
880+
exeContext: ExecutionContext,
881+
itemType: GraphQLOutputType,
882+
fieldGroup: FieldGroup,
883+
info: GraphQLResolveInfo,
884+
path: Path,
885+
items: Iterable<unknown>,
886+
): PromiseOrValue<ReadonlyArray<unknown>> {
869887
// This is specified as a simple map, however we're optimizing the path
870888
// where the list contains no Promises by avoiding creating another Promise.
871889
let containsPromise = false;
872890
const completedResults: Array<unknown> = [];
873891
let index = 0;
874-
for (const item of result) {
892+
const iterator = items[Symbol.iterator]();
893+
let iteration = iterator.next();
894+
while (!iteration.done) {
895+
const item = iteration.value;
896+
875897
// No need to modify the info object containing the path,
876898
// since from here on it is not ever accessed by resolver functions.
877899
const itemPath = addPath(path, index, undefined);
@@ -903,6 +925,7 @@ function completeListValue(
903925
}
904926

905927
index++;
928+
iteration = iterator.next();
906929
}
907930

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

0 commit comments

Comments
 (0)