Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incrementalDelivery: refactoring and streamlining #3728

Merged
merged 4 commits into from
Sep 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 39 additions & 48 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ async function completeAsyncIteratorValue(
break;
}

const fieldPath = addPath(path, index, undefined);
const itemPath = addPath(path, index, undefined);
try {
// eslint-disable-next-line no-await-in-loop
const { value, done } = await iterator.next();
Expand All @@ -1011,7 +1011,7 @@ async function completeAsyncIteratorValue(
itemType,
fieldNodes,
info,
fieldPath,
itemPath,
value,
asyncPayloadRecord,
);
Expand All @@ -1024,10 +1024,10 @@ async function completeAsyncIteratorValue(
const error = locatedError(
rawError,
fieldNodes,
pathToArray(fieldPath),
pathToArray(itemPath),
);
const handledError = handleFieldError(error, itemType, errors);
filterSubsequentPayloads(exeContext, fieldPath);
filterSubsequentPayloads(exeContext, itemPath);
return handledError;
}),
);
Expand All @@ -1036,18 +1036,13 @@ async function completeAsyncIteratorValue(
}
} catch (rawError) {
completedResults.push(null);
const error = locatedError(
rawError,
fieldNodes,
pathToArray(fieldPath),
);
filterSubsequentPayloads(exeContext, fieldPath);
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
filterSubsequentPayloads(exeContext, itemPath);
handleFieldError(error, itemType, errors);
}
} catch (rawError) {
completedResults.push(null);
const error = locatedError(rawError, fieldNodes, pathToArray(fieldPath));
handleFieldError(error, itemType, errors);
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
completedResults.push(handleFieldError(error, itemType, errors));
break;
}
index += 1;
Expand Down Expand Up @@ -1103,28 +1098,29 @@ function completeListValue(
// No need to modify the info object containing the path,
// since from here on it is not ever accessed by resolver functions.
const itemPath = addPath(path, index, undefined);

if (
stream &&
typeof stream.initialCount === 'number' &&
index >= stream.initialCount
) {
previousAsyncPayloadRecord = executeStreamField(
path,
itemPath,
item,
exeContext,
fieldNodes,
info,
itemType,
stream.label,
previousAsyncPayloadRecord,
);
index++;
continue;
}

try {
let completedItem;

if (
stream &&
typeof stream.initialCount === 'number' &&
index >= stream.initialCount
) {
previousAsyncPayloadRecord = executeStreamField(
path,
itemPath,
item,
exeContext,
fieldNodes,
info,
itemType,
stream.label,
previousAsyncPayloadRecord,
);
index++;
continue;
}
if (isPromise(item)) {
completedItem = item.then((resolved) =>
completeValue(
Expand Down Expand Up @@ -1939,7 +1935,7 @@ async function executeStreamIteratorItem(
info: GraphQLResolveInfo,
itemType: GraphQLOutputType,
asyncPayloadRecord: StreamRecord,
fieldPath: Path,
itemPath: Path,
): Promise<IteratorResult<unknown>> {
let item;
try {
Expand All @@ -1950,9 +1946,8 @@ async function executeStreamIteratorItem(
}
item = value;
} catch (rawError) {
const error = locatedError(rawError, fieldNodes, pathToArray(fieldPath));
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
const value = handleFieldError(error, itemType, asyncPayloadRecord.errors);
filterSubsequentPayloads(exeContext, fieldPath, asyncPayloadRecord);
// don't continue if iterator throws
return { done: true, value };
}
Expand All @@ -1963,32 +1958,28 @@ async function executeStreamIteratorItem(
itemType,
fieldNodes,
info,
fieldPath,
itemPath,
item,
asyncPayloadRecord,
);

if (isPromise(completedItem)) {
completedItem = completedItem.then(undefined, (rawError) => {
const error = locatedError(
rawError,
fieldNodes,
pathToArray(fieldPath),
);
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
const handledError = handleFieldError(
error,
itemType,
asyncPayloadRecord.errors,
);
filterSubsequentPayloads(exeContext, fieldPath, asyncPayloadRecord);
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
return handledError;
});
}
return { done: false, value: completedItem };
} catch (rawError) {
const error = locatedError(rawError, fieldNodes, pathToArray(fieldPath));
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
const value = handleFieldError(error, itemType, asyncPayloadRecord.errors);
filterSubsequentPayloads(exeContext, fieldPath, asyncPayloadRecord);
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
return { done: false, value };
}
}
Expand All @@ -2008,10 +1999,10 @@ async function executeStreamIterator(
let previousAsyncPayloadRecord = parentContext ?? undefined;
// eslint-disable-next-line no-constant-condition
while (true) {
const fieldPath = addPath(path, index, undefined);
const itemPath = addPath(path, index, undefined);
const asyncPayloadRecord = new StreamRecord({
label,
path: fieldPath,
path: itemPath,
parentContext: previousAsyncPayloadRecord,
iterator,
exeContext,
Expand All @@ -2024,7 +2015,7 @@ async function executeStreamIterator(
info,
itemType,
asyncPayloadRecord,
fieldPath,
itemPath,
);

asyncPayloadRecord.addItems(
Expand Down