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

remove extra ticks from executeStreamField #3798

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
60 changes: 50 additions & 10 deletions src/execution/__tests__/stream-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,56 @@ describe('Execute: stream directive', () => {
},
]);
});
it('Can stream a field that returns a list of promises with nested promises', async () => {
const document = parse(`
query {
friendList @stream(initialCount: 2) {
name
id
}
}
`);
const result = await complete(document, {
friendList: () =>
friends.map((f) =>
Promise.resolve({
name: Promise.resolve(f.name),
id: Promise.resolve(f.id),
}),
),
});
expectJSON(result).toDeepEqual([
{
data: {
friendList: [
{
name: 'Luke',
id: '1',
},
{
name: 'Han',
id: '2',
},
],
},
hasNext: true,
},
{
incremental: [
{
items: [
{
name: 'Leia',
id: '3',
},
],
path: ['friendList', 2],
},
],
hasNext: false,
},
]);
});
it('Handles rejections in a field that returns a list of promises before initialCount is reached', async () => {
const document = parse(`
query {
Expand Down Expand Up @@ -531,11 +581,6 @@ describe('Execute: stream directive', () => {
},
],
},
],
hasNext: true,
},
{
incremental: [
{
items: [{ name: 'Leia', id: '3' }],
path: ['friendList', 2],
Expand Down Expand Up @@ -984,11 +1029,6 @@ describe('Execute: stream directive', () => {
},
],
},
],
hasNext: true,
},
{
incremental: [
{
items: [{ nonNullName: 'Han' }],
path: ['friendList', 2],
Expand Down
132 changes: 94 additions & 38 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,78 @@ function executeDeferredFragment(
asyncPayloadRecord.addData(promiseOrData);
}

async function completedItemsFromPromisedItem(
exeContext: ExecutionContext,
itemType: GraphQLOutputType,
fieldNodes: ReadonlyArray<FieldNode>,
info: GraphQLResolveInfo,
path: Path,
itemPath: Path,
item: Promise<unknown>,
asyncPayloadRecord: AsyncPayloadRecord,
): Promise<[unknown] | null> {
try {
try {
const resolved = await item;
let completedItem = completeValue(
exeContext,
itemType,
fieldNodes,
info,
itemPath,
resolved,
asyncPayloadRecord,
);
if (isPromise(completedItem)) {
completedItem = await completedItem;
}
return [completedItem];
} catch (rawError) {
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
const handledError = handleFieldError(
error,
itemType,
asyncPayloadRecord.errors,
);
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
return [handledError];
}
} catch (error) {
asyncPayloadRecord.errors.push(error);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
return null;
}
}

async function completedItemsFromPromisedCompletedItem(
exeContext: ExecutionContext,
itemType: GraphQLOutputType,
fieldNodes: ReadonlyArray<FieldNode>,
path: Path,
itemPath: Path,
completedItem: Promise<unknown>,
asyncPayloadRecord: AsyncPayloadRecord,
): Promise<[unknown] | null> {
try {
try {
return [await completedItem];
} catch (rawError) {
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
const handledError = handleFieldError(
error,
itemType,
asyncPayloadRecord.errors,
);
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
return [handledError];
}
} catch (error) {
asyncPayloadRecord.errors.push(error);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
return null;
}
}

function executeStreamField(
path: Path,
itemPath: Path,
Expand All @@ -1816,24 +1888,18 @@ function executeStreamField(
exeContext,
});
if (isPromise(item)) {
const completedItems = completePromisedValue(
exeContext,
itemType,
fieldNodes,
info,
itemPath,
item,
asyncPayloadRecord,
).then(
(value) => [value],
(error) => {
asyncPayloadRecord.errors.push(error);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
return null;
},
asyncPayloadRecord.addItems(
completedItemsFromPromisedItem(
exeContext,
itemType,
fieldNodes,
info,
path,
itemPath,
item,
asyncPayloadRecord,
),
);

asyncPayloadRecord.addItems(completedItems);
return asyncPayloadRecord;
}

Expand Down Expand Up @@ -1866,27 +1932,17 @@ function executeStreamField(
}

if (isPromise(completedItem)) {
const completedItems = completedItem
.then(undefined, (rawError) => {
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
const handledError = handleFieldError(
error,
itemType,
asyncPayloadRecord.errors,
);
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
return handledError;
})
.then(
(value) => [value],
(error) => {
asyncPayloadRecord.errors.push(error);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
return null;
},
);

asyncPayloadRecord.addItems(completedItems);
asyncPayloadRecord.addItems(
completedItemsFromPromisedCompletedItem(
exeContext,
itemType,
fieldNodes,
path,
itemPath,
completedItem,
asyncPayloadRecord,
),
);
return asyncPayloadRecord;
}

Expand Down