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

incremental: avoid double loop with stream from sync iterables #4076

Merged
merged 1 commit into from
May 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 20 additions & 21 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2163,12 +2163,12 @@ function firstSyncStreamItems(
info: GraphQLResolveInfo,
itemType: GraphQLOutputType,
): StreamItemsRecord {
const path = streamRecord.path;
const initialPath = addPath(path, initialIndex, undefined);

const firstStreamItems: StreamItemsRecord = {
return {
streamRecord,
result: Promise.resolve().then(() => {
const path = streamRecord.path;
const initialPath = addPath(path, initialIndex, undefined);

let result = completeStreamItems(
streamRecord,
initialPath,
Expand All @@ -2179,7 +2179,8 @@ function firstSyncStreamItems(
info,
itemType,
);
const results = [result];
const firstStreamItems = { result };
let currentStreamItems = firstStreamItems;
let currentIndex = initialIndex;
let iteration = iterator.next();
let erroredSynchronously = false;
Expand All @@ -2201,31 +2202,29 @@ function firstSyncStreamItems(
info,
itemType,
);
results.push(result);

const nextStreamItems: StreamItemsRecord = { streamRecord, result };
currentStreamItems.result = prependNextStreamItems(
currentStreamItems.result,
nextStreamItems,
);
currentStreamItems = nextStreamItems;

iteration = iterator.next();
}

currentIndex = results.length - 1;
// If a non-reconcilable stream items result was encountered, then the stream terminates in error.
// Otherwise, add a stream terminator.
let currentResult = erroredSynchronously
? results[currentIndex]
: prependNextStreamItems(results[currentIndex], {
streamRecord,
result: { streamRecord },
});

while (currentIndex-- > 0) {
currentResult = prependNextStreamItems(results[currentIndex], {
streamRecord,
result: currentResult,
});
if (!erroredSynchronously) {
currentStreamItems.result = prependNextStreamItems(
currentStreamItems.result,
{ streamRecord, result: { streamRecord } },
);
}

return currentResult;
return firstStreamItems.result;
}),
};
return firstStreamItems;
}

function prependNextStreamItems(
Expand Down