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

fix(incrementalDelivery): fix null bubbling with async iterables #3760

Merged
merged 2 commits into from
Oct 27, 2022
Merged
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
130 changes: 110 additions & 20 deletions src/execution/__tests__/stream-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert } from 'chai';
import { assert, expect } from 'chai';
import { describe, it } from 'mocha';

import { expectJSON } from '../../__testUtils__/expectJSON.js';
Expand Down Expand Up @@ -851,6 +851,57 @@ describe('Execute: stream directive', () => {
]);
});
it('Handles async errors thrown by completeValue after initialCount is reached', async () => {
const document = parse(`
query {
friendList @stream(initialCount: 1) {
nonNullName
}
}
`);
const result = await complete(document, {
friendList: () => [
Promise.resolve({ nonNullName: friends[0].name }),
Promise.resolve({
nonNullName: () => Promise.reject(new Error('Oops')),
}),
Promise.resolve({ nonNullName: friends[1].name }),
],
});
expectJSON(result).toDeepEqual([
{
data: {
friendList: [{ nonNullName: 'Luke' }],
},
hasNext: true,
},
{
incremental: [
{
items: [null],
path: ['friendList', 1],
errors: [
{
message: 'Oops',
locations: [{ line: 4, column: 11 }],
path: ['friendList', 1, 'nonNullName'],
},
],
},
],
hasNext: true,
},
{
incremental: [
{
items: [{ nonNullName: 'Han' }],
path: ['friendList', 2],
},
],
hasNext: false,
},
]);
});
it('Handles async errors thrown by completeValue after initialCount is reached for a non-nullable list', async () => {
const document = parse(`
query {
nonNullFriendList @stream(initialCount: 1) {
Expand Down Expand Up @@ -946,6 +997,50 @@ describe('Execute: stream directive', () => {
},
]);
});
it('Handles async errors thrown by completeValue after initialCount is reached from async iterable for a non-nullable list', async () => {
const document = parse(`
query {
nonNullFriendList @stream(initialCount: 1) {
nonNullName
}
}
`);
const result = await complete(document, {
async *nonNullFriendList() {
yield await Promise.resolve({ nonNullName: friends[0].name });
yield await Promise.resolve({
nonNullName: () => Promise.reject(new Error('Oops')),
});
yield await Promise.resolve({
nonNullName: friends[1].name,
}); /* c8 ignore start */
} /* c8 ignore stop */,
});
expectJSON(result).toDeepEqual([
{
data: {
nonNullFriendList: [{ nonNullName: 'Luke' }],
},
hasNext: true,
},
{
incremental: [
{
items: null,
path: ['nonNullFriendList', 1],
errors: [
{
message: 'Oops',
locations: [{ line: 4, column: 11 }],
path: ['nonNullFriendList', 1, 'nonNullName'],
},
],
},
],
hasNext: false,
},
]);
});
it('Filters payloads that are nulled', async () => {
const document = parse(`
query {
Expand All @@ -961,8 +1056,8 @@ describe('Execute: stream directive', () => {
nestedObject: {
nonNullScalarField: () => Promise.resolve(null),
async *nestedFriendList() {
yield await Promise.resolve(friends[0]);
},
yield await Promise.resolve(friends[0]); /* c8 ignore start */
} /* c8 ignore stop */,
},
});
expectJSON(result).toDeepEqual({
Expand Down Expand Up @@ -1061,9 +1156,6 @@ describe('Execute: stream directive', () => {
path: ['nestedObject', 'nestedFriendList', 0],
},
],
hasNext: true,
},
{
hasNext: false,
},
]);
Expand All @@ -1088,8 +1180,8 @@ describe('Execute: stream directive', () => {
deeperNestedObject: {
nonNullScalarField: () => Promise.resolve(null),
async *deeperNestedFriendList() {
yield await Promise.resolve(friends[0]);
},
yield await Promise.resolve(friends[0]); /* c8 ignore start */
} /* c8 ignore stop */,
},
},
});
Expand Down Expand Up @@ -1176,14 +1268,17 @@ describe('Execute: stream directive', () => {

it('Returns iterator and ignores errors when stream payloads are filtered', async () => {
let returned = false;
let index = 0;
let requested = false;
const iterable = {
[Symbol.asyncIterator]: () => ({
next: () => {
const friend = friends[index++];
if (!friend) {
return Promise.resolve({ done: true, value: undefined });
if (requested) {
/* c8 ignore next 3 */
// Not reached, iterator should end immediately.
expect.fail('Not reached');
}
requested = true;
const friend = friends[0];
return Promise.resolve({
done: false,
value: {
Expand Down Expand Up @@ -1261,17 +1356,12 @@ describe('Execute: stream directive', () => {
],
},
],
hasNext: true,
hasNext: false,
},
});
const result3 = await iterator.next();
expectJSON(result3).toDeepEqual({
done: false,
value: { hasNext: false },
});

const result4 = await iterator.next();
expectJSON(result4).toDeepEqual({ done: true, value: undefined });
const result3 = await iterator.next();
expectJSON(result3).toDeepEqual({ done: true, value: undefined });

assert(returned);
});
Expand Down
63 changes: 36 additions & 27 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2026,43 +2026,52 @@ async function executeStreamIterator(
exeContext,
});

const dataPromise = executeStreamIteratorItem(
iterator,
exeContext,
fieldNodes,
info,
itemType,
asyncPayloadRecord,
itemPath,
);

asyncPayloadRecord.addItems(
dataPromise
.then(({ value }) => value)
.then(
(value) => [value],
(err) => {
asyncPayloadRecord.errors.push(err);
return null;
},
),
);
let iteration;
try {
// eslint-disable-next-line no-await-in-loop
const { done } = await dataPromise;
if (done) {
break;
}
} catch (err) {
// entire stream has errored and bubbled upwards
iteration = await executeStreamIteratorItem(
iterator,
exeContext,
fieldNodes,
info,
itemType,
asyncPayloadRecord,
itemPath,
);
} catch (error) {
asyncPayloadRecord.errors.push(error);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
asyncPayloadRecord.addItems(null);
// entire stream has errored and bubbled upwards
if (iterator?.return) {
iterator.return().catch(() => {
// ignore errors
});
}
return;
}

const { done, value: completedItem } = iteration;

let completedItems: PromiseOrValue<Array<unknown> | null>;
if (isPromise(completedItem)) {
completedItems = completedItem.then(
(value) => [value],
(error) => {
asyncPayloadRecord.errors.push(error);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
return null;
},
);
} else {
completedItems = [completedItem];
}

asyncPayloadRecord.addItems(completedItems);

if (done) {
break;
}
previousAsyncPayloadRecord = asyncPayloadRecord;
index++;
}
Expand Down