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

feat(incremental): merge deferred fragments at the same level #3820

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
425 changes: 405 additions & 20 deletions src/execution/__tests__/defer-test.ts

Large diffs are not rendered by default.

43 changes: 27 additions & 16 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 All @@ -213,7 +215,8 @@ describe('Execute: Handles basic execution tasks', () => {

expect(resolvedInfo).to.have.all.keys(
'fieldName',
'fieldNodes',
'fieldGroup',
'deferDepth',
'returnType',
'parentType',
'path',
Expand All @@ -236,16 +239,22 @@ describe('Execute: Handles basic execution tasks', () => {
operation,
});

const field = operation.selectionSet.selections[0];
const fieldNode = operation.selectionSet.selections[0];
expect(resolvedInfo).to.deep.include({
fieldNodes: [field],
path: { prev: undefined, key: 'result', typename: 'Test' },
fieldGroup: [{ fieldNode, depth: 0, deferDepth: undefined }],
deferDepth: undefined,
variableValues: { var: 'abc' },
});

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

it('populates path correctly with complex types', () => {
let path;
let path: Path | undefined;
const someObject = new GraphQLObjectType({
name: 'SomeObject',
fields: {
Expand Down Expand Up @@ -288,18 +297,20 @@ 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?.prev).to.deep.include({
key: 0,
typename: undefined,
});

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

Expand Down
6 changes: 2 additions & 4 deletions src/execution/__tests__/mutations-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ describe('Execute: Handles mutation execution ordering', () => {
const document = parse(`
mutation M {
first: promiseToChangeTheNumber(newNumber: 1) {
...DeferFragment @defer(label: "defer-label")
...DeferFragment @defer
},
second: immediatelyChangeTheNumber(newNumber: 2) {
theNumber
Expand Down Expand Up @@ -242,7 +242,6 @@ describe('Execute: Handles mutation execution ordering', () => {
{
incremental: [
{
label: 'defer-label',
path: ['first'],
data: {
promiseToGetTheNumber: 2,
Expand Down Expand Up @@ -281,7 +280,7 @@ describe('Execute: Handles mutation execution ordering', () => {
it('Mutation with @defer is not executed serially', async () => {
const document = parse(`
mutation M {
...MutationFragment @defer(label: "defer-label")
...MutationFragment @defer
second: immediatelyChangeTheNumber(newNumber: 2) {
theNumber
}
Expand Down Expand Up @@ -317,7 +316,6 @@ describe('Execute: Handles mutation execution ordering', () => {
{
incremental: [
{
label: 'defer-label',
path: [],
data: {
first: {
Expand Down
20 changes: 5 additions & 15 deletions src/execution/__tests__/stream-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,7 @@ describe('Execute: stream directive', () => {
});
});
it('Returns label from stream directive', async () => {
const document = parse(
'{ scalarList @stream(initialCount: 1, label: "scalar-stream") }',
);
const document = parse('{ scalarList @stream(initialCount: 1) }');
const result = await complete(document, {
scalarList: () => ['apple', 'banana', 'coconut'],
});
Expand All @@ -228,7 +226,6 @@ describe('Execute: stream directive', () => {
{
items: ['banana'],
path: ['scalarList', 1],
label: 'scalar-stream',
},
],
hasNext: true,
Expand All @@ -238,7 +235,6 @@ describe('Execute: stream directive', () => {
{
items: ['coconut'],
path: ['scalarList', 2],
label: 'scalar-stream',
},
],
hasNext: false,
Expand Down Expand Up @@ -1661,8 +1657,8 @@ describe('Execute: stream directive', () => {

const document = parse(`
query {
friendList @stream(initialCount: 1, label:"stream-label") {
...NameFragment @defer(label: "DeferName") @defer(label: "DeferName")
friendList @stream(initialCount: 1) {
...NameFragment @defer
id
}
}
Expand Down Expand Up @@ -1705,12 +1701,10 @@ describe('Execute: stream directive', () => {
{
data: { name: 'Luke' },
path: ['friendList', 0],
label: 'DeferName',
},
{
items: [{ id: '2' }],
path: ['friendList', 1],
label: 'stream-label',
},
],
hasNext: true,
Expand All @@ -1727,7 +1721,6 @@ describe('Execute: stream directive', () => {
{
data: { name: 'Han' },
path: ['friendList', 1],
label: 'DeferName',
},
],
hasNext: false,
Expand All @@ -1747,8 +1740,8 @@ describe('Execute: stream directive', () => {

const document = parse(`
query {
friendList @stream(initialCount: 1, label:"stream-label") {
...NameFragment @defer(label: "DeferName") @defer(label: "DeferName")
friendList @stream(initialCount: 1) {
...NameFragment @defer
id
}
}
Expand Down Expand Up @@ -1791,12 +1784,10 @@ describe('Execute: stream directive', () => {
{
data: { name: 'Luke' },
path: ['friendList', 0],
label: 'DeferName',
},
{
items: [{ id: '2' }],
path: ['friendList', 1],
label: 'stream-label',
},
],
hasNext: true,
Expand All @@ -1811,7 +1802,6 @@ describe('Execute: stream directive', () => {
{
data: { name: 'Han' },
path: ['friendList', 1],
label: 'DeferName',
},
],
hasNext: true,
Expand Down
2 changes: 1 addition & 1 deletion src/execution/__tests__/sync-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('Execute: synchronously when possible', () => {
it('throws if encountering async iterable execution', () => {
const doc = `
query Example {
...deferFrag @defer(label: "deferLabel")
...deferFrag @defer
}
fragment deferFrag on Query {
syncField
Expand Down
Loading