Skip to content

Commit 80325b5

Browse files
authored
incrementalDelivery: refactoring and streamlining (#3728)
1 parent d1c83e0 commit 80325b5

File tree

1 file changed

+39
-48
lines changed

1 file changed

+39
-48
lines changed

src/execution/execute.ts

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ async function completeAsyncIteratorValue(
996996
break;
997997
}
998998

999-
const fieldPath = addPath(path, index, undefined);
999+
const itemPath = addPath(path, index, undefined);
10001000
try {
10011001
// eslint-disable-next-line no-await-in-loop
10021002
const { value, done } = await iterator.next();
@@ -1011,7 +1011,7 @@ async function completeAsyncIteratorValue(
10111011
itemType,
10121012
fieldNodes,
10131013
info,
1014-
fieldPath,
1014+
itemPath,
10151015
value,
10161016
asyncPayloadRecord,
10171017
);
@@ -1024,10 +1024,10 @@ async function completeAsyncIteratorValue(
10241024
const error = locatedError(
10251025
rawError,
10261026
fieldNodes,
1027-
pathToArray(fieldPath),
1027+
pathToArray(itemPath),
10281028
);
10291029
const handledError = handleFieldError(error, itemType, errors);
1030-
filterSubsequentPayloads(exeContext, fieldPath);
1030+
filterSubsequentPayloads(exeContext, itemPath);
10311031
return handledError;
10321032
}),
10331033
);
@@ -1036,18 +1036,13 @@ async function completeAsyncIteratorValue(
10361036
}
10371037
} catch (rawError) {
10381038
completedResults.push(null);
1039-
const error = locatedError(
1040-
rawError,
1041-
fieldNodes,
1042-
pathToArray(fieldPath),
1043-
);
1044-
filterSubsequentPayloads(exeContext, fieldPath);
1039+
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1040+
filterSubsequentPayloads(exeContext, itemPath);
10451041
handleFieldError(error, itemType, errors);
10461042
}
10471043
} catch (rawError) {
1048-
completedResults.push(null);
1049-
const error = locatedError(rawError, fieldNodes, pathToArray(fieldPath));
1050-
handleFieldError(error, itemType, errors);
1044+
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1045+
completedResults.push(handleFieldError(error, itemType, errors));
10511046
break;
10521047
}
10531048
index += 1;
@@ -1103,28 +1098,29 @@ function completeListValue(
11031098
// No need to modify the info object containing the path,
11041099
// since from here on it is not ever accessed by resolver functions.
11051100
const itemPath = addPath(path, index, undefined);
1101+
1102+
if (
1103+
stream &&
1104+
typeof stream.initialCount === 'number' &&
1105+
index >= stream.initialCount
1106+
) {
1107+
previousAsyncPayloadRecord = executeStreamField(
1108+
path,
1109+
itemPath,
1110+
item,
1111+
exeContext,
1112+
fieldNodes,
1113+
info,
1114+
itemType,
1115+
stream.label,
1116+
previousAsyncPayloadRecord,
1117+
);
1118+
index++;
1119+
continue;
1120+
}
1121+
11061122
try {
11071123
let completedItem;
1108-
1109-
if (
1110-
stream &&
1111-
typeof stream.initialCount === 'number' &&
1112-
index >= stream.initialCount
1113-
) {
1114-
previousAsyncPayloadRecord = executeStreamField(
1115-
path,
1116-
itemPath,
1117-
item,
1118-
exeContext,
1119-
fieldNodes,
1120-
info,
1121-
itemType,
1122-
stream.label,
1123-
previousAsyncPayloadRecord,
1124-
);
1125-
index++;
1126-
continue;
1127-
}
11281124
if (isPromise(item)) {
11291125
completedItem = item.then((resolved) =>
11301126
completeValue(
@@ -1939,7 +1935,7 @@ async function executeStreamIteratorItem(
19391935
info: GraphQLResolveInfo,
19401936
itemType: GraphQLOutputType,
19411937
asyncPayloadRecord: StreamRecord,
1942-
fieldPath: Path,
1938+
itemPath: Path,
19431939
): Promise<IteratorResult<unknown>> {
19441940
let item;
19451941
try {
@@ -1950,9 +1946,8 @@ async function executeStreamIteratorItem(
19501946
}
19511947
item = value;
19521948
} catch (rawError) {
1953-
const error = locatedError(rawError, fieldNodes, pathToArray(fieldPath));
1949+
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
19541950
const value = handleFieldError(error, itemType, asyncPayloadRecord.errors);
1955-
filterSubsequentPayloads(exeContext, fieldPath, asyncPayloadRecord);
19561951
// don't continue if iterator throws
19571952
return { done: true, value };
19581953
}
@@ -1963,32 +1958,28 @@ async function executeStreamIteratorItem(
19631958
itemType,
19641959
fieldNodes,
19651960
info,
1966-
fieldPath,
1961+
itemPath,
19671962
item,
19681963
asyncPayloadRecord,
19691964
);
19701965

19711966
if (isPromise(completedItem)) {
19721967
completedItem = completedItem.then(undefined, (rawError) => {
1973-
const error = locatedError(
1974-
rawError,
1975-
fieldNodes,
1976-
pathToArray(fieldPath),
1977-
);
1968+
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
19781969
const handledError = handleFieldError(
19791970
error,
19801971
itemType,
19811972
asyncPayloadRecord.errors,
19821973
);
1983-
filterSubsequentPayloads(exeContext, fieldPath, asyncPayloadRecord);
1974+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
19841975
return handledError;
19851976
});
19861977
}
19871978
return { done: false, value: completedItem };
19881979
} catch (rawError) {
1989-
const error = locatedError(rawError, fieldNodes, pathToArray(fieldPath));
1980+
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
19901981
const value = handleFieldError(error, itemType, asyncPayloadRecord.errors);
1991-
filterSubsequentPayloads(exeContext, fieldPath, asyncPayloadRecord);
1982+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
19921983
return { done: false, value };
19931984
}
19941985
}
@@ -2008,10 +1999,10 @@ async function executeStreamIterator(
20081999
let previousAsyncPayloadRecord = parentContext ?? undefined;
20092000
// eslint-disable-next-line no-constant-condition
20102001
while (true) {
2011-
const fieldPath = addPath(path, index, undefined);
2002+
const itemPath = addPath(path, index, undefined);
20122003
const asyncPayloadRecord = new StreamRecord({
20132004
label,
2014-
path: fieldPath,
2005+
path: itemPath,
20152006
parentContext: previousAsyncPayloadRecord,
20162007
iterator,
20172008
exeContext,
@@ -2024,7 +2015,7 @@ async function executeStreamIterator(
20242015
info,
20252016
itemType,
20262017
asyncPayloadRecord,
2027-
fieldPath,
2018+
itemPath,
20282019
);
20292020

20302021
asyncPayloadRecord.addItems(

0 commit comments

Comments
 (0)