Skip to content

Commit b294582

Browse files
committed
move handleNext to completeAsyncIterableValue
1 parent 8dfcc48 commit b294582

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
@@ -919,55 +919,45 @@ function completeValue(
919919
}
920920

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

973963
/**
@@ -982,14 +972,21 @@ function completeListValue(
982972
path: Path,
983973
result: mixed,
984974
): PromiseOrValue<$ReadOnlyArray<mixed>> {
975+
const itemType = returnType.ofType;
976+
985977
if (isAsyncIterable(result)) {
986-
return completeAsyncIterableValue(
978+
const iteratorMethod = result[SYMBOL_ASYNC_ITERATOR];
979+
const iterator = iteratorMethod.call(result);
980+
981+
return completeAsyncIteratorValue(
987982
exeContext,
988-
returnType,
983+
itemType,
989984
fieldNodes,
990985
info,
991986
path,
992-
result,
987+
0,
988+
[],
989+
iterator,
993990
);
994991
}
995992

@@ -1001,7 +998,6 @@ function completeListValue(
1001998

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

0 commit comments

Comments
 (0)