Skip to content

Commit 9af3fac

Browse files
committed
perf: introduce completePromisedListItemValue (#4051)
depends on #4050
1 parent fd3c54a commit 9af3fac

1 file changed

Lines changed: 65 additions & 18 deletions

File tree

src/execution/execute.ts

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -793,9 +793,31 @@ async function completeAsyncIteratorValue(
793793
throw locatedError(rawError, fieldGroup, pathToArray(path));
794794
}
795795

796-
if (
796+
// TODO: add test case for stream returning done before initialCount
797+
/* c8 ignore next 3 */
798+
if (iteration.done) {
799+
break;
800+
}
801+
802+
const item = iteration.value;
803+
// TODO: add tests for stream backed by asyncIterator that returns a promise
804+
/* c8 ignore start */
805+
if (isPromise(item)) {
806+
completedResults.push(
807+
completePromisedListItemValue(
808+
item,
809+
exeContext,
810+
itemType,
811+
fieldGroup,
812+
info,
813+
itemPath,
814+
),
815+
);
816+
containsPromise = true;
817+
} else if (
818+
/* c8 ignore stop */
797819
completeListItemValue(
798-
iteration.value,
820+
item,
799821
completedResults,
800822
exeContext,
801823
itemType,
@@ -854,7 +876,19 @@ function completeListValue(
854876
// since from here on it is not ever accessed by resolver functions.
855877
const itemPath = addPath(path, index, undefined);
856878

857-
if (
879+
if (isPromise(item)) {
880+
completedResults.push(
881+
completePromisedListItemValue(
882+
item,
883+
exeContext,
884+
itemType,
885+
fieldGroup,
886+
info,
887+
itemPath,
888+
),
889+
);
890+
containsPromise = true;
891+
} else if (
858892
completeListItemValue(
859893
item,
860894
completedResults,
@@ -888,21 +922,6 @@ function completeListItemValue(
888922
info: GraphQLResolveInfo,
889923
itemPath: Path,
890924
): boolean {
891-
if (isPromise(item)) {
892-
completedResults.push(
893-
completePromisedValue(
894-
exeContext,
895-
itemType,
896-
fieldGroup,
897-
info,
898-
itemPath,
899-
item,
900-
),
901-
);
902-
903-
return true;
904-
}
905-
906925
try {
907926
const completedItem = completeValue(
908927
exeContext,
@@ -941,6 +960,34 @@ function completeListItemValue(
941960
return false;
942961
}
943962

963+
async function completePromisedListItemValue(
964+
item: unknown,
965+
exeContext: ExecutionContext,
966+
itemType: GraphQLOutputType,
967+
fieldGroup: FieldGroup,
968+
info: GraphQLResolveInfo,
969+
itemPath: Path,
970+
): Promise<unknown> {
971+
try {
972+
const resolved = await item;
973+
let completed = completeValue(
974+
exeContext,
975+
itemType,
976+
fieldGroup,
977+
info,
978+
itemPath,
979+
resolved,
980+
);
981+
if (isPromise(completed)) {
982+
completed = await completed;
983+
}
984+
return completed;
985+
} catch (rawError) {
986+
handleFieldError(rawError, exeContext, itemType, fieldGroup, itemPath);
987+
return null;
988+
}
989+
}
990+
944991
/**
945992
* Complete a Scalar or Enum by serializing to a valid value, returning
946993
* null if serialization is not possible.

0 commit comments

Comments
 (0)