Skip to content

Commit 3340bf4

Browse files
committed
move handleNext to completeAsyncIterableValue
1 parent a0e305f commit 3340bf4

File tree

1 file changed

+42
-46
lines changed

1 file changed

+42
-46
lines changed

src/execution/execute.js

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -917,55 +917,45 @@ function completeValue(
917917
}
918918

919919
/**
920-
* Complete a async iterable value by completing each item in the list with
921-
* the inner type
920+
* Complete a async iterator value by completing the result and calling
921+
* recursively until all the results are completed.
922922
*/
923-
function completeAsyncIterableValue(
923+
function completeAsyncIteratorValue(
924924
exeContext: ExecutionContext,
925-
returnType: GraphQLList<GraphQLOutputType>,
925+
itemType: GraphQLOutputType,
926926
fieldNodes: $ReadOnlyArray<FieldNode>,
927927
info: GraphQLResolveInfo,
928928
path: Path,
929-
result: AsyncIterable<mixed>,
929+
index: number,
930+
completedResults: Array<mixed>,
931+
iterator: AsyncIterator<mixed>,
930932
): Promise<$ReadOnlyArray<mixed>> {
931-
// $FlowFixMe
932-
const iteratorMethod = result[SYMBOL_ASYNC_ITERATOR];
933-
const iterator = iteratorMethod.call(result);
934-
935-
const completedResults = [];
936-
let index = 0;
937-
938-
const itemType = returnType.ofType;
939-
940-
function handleNext() {
941-
const fieldPath = addPath(path, index);
942-
return iterator.next().then(
943-
({ value, done }) => {
944-
if (done) {
945-
return completedResults;
946-
}
947-
completedResults.push(
948-
completeValue(
949-
exeContext,
950-
itemType,
951-
fieldNodes,
952-
info,
953-
fieldPath,
954-
value,
955-
),
956-
);
957-
index++;
958-
return handleNext();
959-
},
960-
(error) => {
961-
completedResults.push(null);
962-
handleFieldError(error, fieldNodes, fieldPath, itemType, exeContext);
933+
const fieldPath = addPath(path, index);
934+
return iterator.next().then(
935+
({ value, done }) => {
936+
if (done) {
963937
return completedResults;
964-
},
965-
);
966-
}
967-
968-
return handleNext();
938+
}
939+
completedResults.push(
940+
completeValue(exeContext, itemType, fieldNodes, info, fieldPath, value),
941+
);
942+
return completeAsyncIteratorValue(
943+
exeContext,
944+
itemType,
945+
fieldNodes,
946+
info,
947+
path,
948+
index + 1,
949+
completedResults,
950+
iterator,
951+
);
952+
},
953+
(error) => {
954+
completedResults.push(null);
955+
handleFieldError(error, fieldNodes, fieldPath, itemType, exeContext);
956+
return completedResults;
957+
},
958+
);
969959
}
970960

971961
/**
@@ -980,14 +970,21 @@ function completeListValue(
980970
path: Path,
981971
result: mixed,
982972
): PromiseOrValue<$ReadOnlyArray<mixed>> {
973+
const itemType = returnType.ofType;
974+
983975
if (isAsyncIterable(result)) {
984-
return completeAsyncIterableValue(
976+
const iteratorMethod = result[SYMBOL_ASYNC_ITERATOR];
977+
const iterator = iteratorMethod.call(result);
978+
979+
return completeAsyncIteratorValue(
985980
exeContext,
986-
returnType,
981+
itemType,
987982
fieldNodes,
988983
info,
989984
path,
990-
result,
985+
0,
986+
[],
987+
iterator,
991988
);
992989
}
993990

@@ -999,7 +996,6 @@ function completeListValue(
999996

1000997
// This is specified as a simple map, however we're optimizing the path
1001998
// where the list contains no Promises by avoiding creating another Promise.
1002-
const itemType = returnType.ofType;
1003999
let containsPromise = false;
10041000
const completedResults = arrayFrom(result, (item, index) => {
10051001
// No need to modify the info object containing the path,

0 commit comments

Comments
 (0)