Skip to content

incremental delivery without branching #3862

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 5 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
1,205 changes: 1,200 additions & 5 deletions src/execution/__tests__/defer-test.ts

Large diffs are not rendered by default.

52 changes: 38 additions & 14 deletions src/execution/__tests__/executor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { expectJSON } from '../../__testUtils__/expectJSON.js';
import { resolveOnNextTick } from '../../__testUtils__/resolveOnNextTick.js';

import { inspect } from '../../jsutils/inspect.js';
import type { Path } from '../../jsutils/Path.js';

import { Kind } from '../../language/kinds.js';
import { parse } from '../../language/parser.js';

import type { GraphQLResolveInfo } from '../../type/definition.js';
import {
GraphQLInterfaceType,
GraphQLList,
Expand Down Expand Up @@ -191,7 +193,7 @@ describe('Execute: Handles basic execution tasks', () => {
});

it('provides info about current execution state', () => {
let resolvedInfo;
let resolvedInfo: GraphQLResolveInfo | undefined;
const testType = new GraphQLObjectType({
name: 'Test',
fields: {
Expand Down Expand Up @@ -239,13 +241,22 @@ describe('Execute: Handles basic execution tasks', () => {
const field = operation.selectionSet.selections[0];
expect(resolvedInfo).to.deep.include({
fieldNodes: [field],
path: { prev: undefined, key: 'result', typename: 'Test' },
variableValues: { var: 'abc' },
});

expect(resolvedInfo?.path).to.deep.include({
prev: undefined,
key: 'result',
});

expect(resolvedInfo?.path.info).to.deep.include({
parentType: testType,
fieldName: 'test',
});
});

it('populates path correctly with complex types', () => {
let path;
let path: Path<unknown> | undefined;
const someObject = new GraphQLObjectType({
name: 'SomeObject',
fields: {
Expand Down Expand Up @@ -288,18 +299,31 @@ describe('Execute: Handles basic execution tasks', () => {

executeSync({ schema, document, rootValue });

expect(path).to.deep.equal({
expect(path).to.deep.include({
key: 'l2',
typename: 'SomeObject',
prev: {
key: 0,
typename: undefined,
prev: {
key: 'l1',
typename: 'SomeQuery',
prev: undefined,
},
},
});

expect(path?.info).to.deep.include({
parentType: someObject,
fieldName: 'test',
});

expect(path?.prev).to.deep.include({
key: 0,
});

expect(path?.prev?.info).to.deep.include({
parentType: testType,
fieldName: 'test',
});

expect(path?.prev?.prev).to.deep.include({
key: 'l1',
});

expect(path?.prev?.prev?.info).to.deep.include({
parentType: testType,
fieldName: 'test',
});
});

Expand Down
227 changes: 221 additions & 6 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 @@ -1181,6 +1182,158 @@ describe('Execute: stream directive', () => {
},
]);
});
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: true,
},
{
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: true,
},
{
hasNext: false,
},
]);
});
it('Filters payloads that are nulled', async () => {
const document = parse(`
query {
Expand Down Expand Up @@ -1359,9 +1512,6 @@ describe('Execute: stream directive', () => {
],
},
],
hasNext: true,
},
{
hasNext: false,
},
]);
Expand Down Expand Up @@ -1421,10 +1571,11 @@ describe('Execute: stream directive', () => {
const iterable = {
[Symbol.asyncIterator]: () => ({
next: () => {
/* c8 ignore start */
if (requested) {
// Ignores further errors when filtered.
// stream is filtered, next is not called, and so this is not reached.
return Promise.reject(new Error('Oops'));
}
} /* c8 ignore stop */
requested = true;
const friend = friends[0];
return Promise.resolve({
Expand Down Expand Up @@ -1563,6 +1714,70 @@ describe('Execute: stream directive', () => {
},
]);
});
it('Handles overlapping deferred and non-deferred streams', async () => {
const document = parse(`
query {
nestedObject {
nestedFriendList @stream(initialCount: 0) {
id
}
}
nestedObject {
... @defer {
nestedFriendList @stream(initialCount: 0) {
id
name
}
}
}
}
`);
const result = await complete(document, {
nestedObject: {
async *nestedFriendList() {
yield await Promise.resolve(friends[0]);
yield await Promise.resolve(friends[1]);
},
},
});
expectJSON(result).toDeepEqual([
{
data: {
nestedObject: {
nestedFriendList: [],
},
},
hasNext: true,
},
{
incremental: [
{
data: {
nestedFriendList: [],
},
path: ['nestedObject'],
},
{
items: [{ id: '1', name: 'Luke' }],
path: ['nestedObject', 'nestedFriendList', 0],
},
],
hasNext: true,
},
{
incremental: [
{
items: [{ id: '2', name: 'Han' }],
path: ['nestedObject', 'nestedFriendList', 1],
},
],
hasNext: true,
},
{
hasNext: false,
},
]);
});
it('Returns payloads in correct order when parent deferred fragment resolves slower than stream', async () => {
const [slowFieldPromise, resolveSlowField] = createResolvablePromise();
const document = parse(`
Expand Down
Loading