Skip to content

fix(incremental): abort async iteration when stream errors or stream is filtered #3815

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

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
177 changes: 156 additions & 21 deletions src/execution/__tests__/stream-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { assert } from 'chai';
import { describe, it } from 'mocha';

import { expectJSON } from '../../__testUtils__/expectJSON.js';
import { resolveOnNextTick } from '../../__testUtils__/resolveOnNextTick.js';

import type { PromiseOrValue } from '../../jsutils/PromiseOrValue.js';

Expand Down Expand Up @@ -1134,7 +1135,7 @@ describe('Execute: stream directive', () => {
},
]);
});
it('Handles async errors thrown by completeValue after initialCount is reached from async iterable for a non-nullable list', async () => {
it('Handles async errors thrown by completeValue after initialCount is reached from async generator for a non-nullable list', async () => {
const document = parse(`
query {
nonNullFriendList @stream(initialCount: 1) {
Expand Down Expand Up @@ -1174,9 +1175,152 @@ describe('Execute: stream directive', () => {
],
},
],
hasNext: false,
},
]);
});
it('Handles async errors thrown by completeValue after initialCount is reached from async iterable for a non-nullable list when the async iterable does not provide a return method) ', async () => {
const document = parse(`
query {
nonNullFriendList @stream(initialCount: 1) {
nonNullName
}
}
`);
let count = 0;
const result = await complete(document, {
nonNullFriendList: {
[Symbol.asyncIterator]: () => ({
next: async () => {
switch (count++) {
case 0:
return Promise.resolve({
done: false,
value: { nonNullName: friends[0].name },
});
case 1:
return Promise.resolve({
done: false,
value: {
nonNullName: () => Promise.reject(new Error('Oops')),
},
});
case 2:
return Promise.resolve({
done: false,
value: { nonNullName: friends[1].name },
});
// Not reached
/* c8 ignore next 5 */
case 3:
return Promise.resolve({
done: false,
value: { nonNullName: friends[2].name },
});
}
},
}),
},
});
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('Handles async errors thrown by completeValue after initialCount is reached from async iterable for a non-nullable list when the async iterable provides concurrent next/return methods and has a slow return ', async () => {
const document = parse(`
query {
nonNullFriendList @stream(initialCount: 1) {
nonNullName
}
}
`);
let count = 0;
let returned = false;
const result = await complete(document, {
nonNullFriendList: {
[Symbol.asyncIterator]: () => ({
next: async () => {
/* c8 ignore next 3 */
if (returned) {
return Promise.resolve({ done: true });
}
switch (count++) {
case 0:
return Promise.resolve({
done: false,
value: { nonNullName: friends[0].name },
});
case 1:
return Promise.resolve({
done: false,
value: {
nonNullName: () => Promise.reject(new Error('Oops')),
},
});
case 2:
return Promise.resolve({
done: false,
value: { nonNullName: friends[1].name },
});
// Not reached
/* c8 ignore next 5 */
case 3:
return Promise.resolve({
done: false,
value: { nonNullName: friends[2].name },
});
}
},
return: async () => {
await resolveOnNextTick();
returned = true;
return { done: true };
},
}),
},
});
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,
},
]);
Expand All @@ -1200,25 +1344,19 @@ describe('Execute: stream directive', () => {
} /* c8 ignore stop */,
},
});
expectJSON(result).toDeepEqual([
{
errors: [
{
message:
'Cannot return null for non-nullable field NestedObject.nonNullScalarField.',
locations: [{ line: 4, column: 11 }],
path: ['nestedObject', 'nonNullScalarField'],
},
],
data: {
nestedObject: null,
expectJSON(result).toDeepEqual({
errors: [
{
message:
'Cannot return null for non-nullable field NestedObject.nonNullScalarField.',
locations: [{ line: 4, column: 11 }],
path: ['nestedObject', 'nonNullScalarField'],
},
hasNext: true,
},
{
hasNext: false,
],
data: {
nestedObject: null,
},
]);
});
});
it('Filters payloads that are nulled by a later synchronous error', async () => {
const document = parse(`
Expand Down Expand Up @@ -1359,9 +1497,6 @@ describe('Execute: stream directive', () => {
],
},
],
hasNext: true,
},
{
hasNext: false,
},
]);
Expand Down
Loading