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

add support for defer and stream directives #2319

Merged
merged 6 commits into from
Oct 28, 2020
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
8 changes: 4 additions & 4 deletions benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,9 @@ function sampleModule(modulePath) {

clock(7, module.measure); // warm up
global.gc();
process.nextTick(() => {
process.nextTick(async () => {
const memBaseline = process.memoryUsage().heapUsed;
const clocked = clock(module.count, module.measure);
const clocked = await clock(module.count, module.measure);
process.send({
name: module.name,
clocked: clocked / module.count,
Expand All @@ -357,10 +357,10 @@ function sampleModule(modulePath) {
});

// Clocks the time taken to execute a test per cycle (secs).
function clock(count, fn) {
async function clock(count, fn) {
const start = process.hrtime.bigint();
for (let i = 0; i < count; ++i) {
fn();
await fn();
}
return Number(process.hrtime.bigint() - start);
}
Expand Down
28 changes: 28 additions & 0 deletions benchmark/list-async-benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const { parse } = require('graphql/language/parser.js');
const { execute } = require('graphql/execution/execute.js');
const { buildSchema } = require('graphql/utilities/buildASTSchema.js');

const schema = buildSchema('type Query { listField: [String] }');
const document = parse('{ listField }');

function listField() {
const results = [];
for (let index = 0; index < 100000; index++) {
results.push(Promise.resolve(index));
}
return results;
}

module.exports = {
name: 'Execute Asynchronous List Field',
count: 10,
async measure() {
await execute({
schema,
document,
rootValue: { listField },
});
},
};
26 changes: 26 additions & 0 deletions benchmark/list-asyncIterable-benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const { parse } = require('graphql/language/parser.js');
const { execute } = require('graphql/execution/execute.js');
const { buildSchema } = require('graphql/utilities/buildASTSchema.js');

const schema = buildSchema('type Query { listField: [String] }');
const document = parse('{ listField }');

async function* listField() {
for (let index = 0; index < 100000; index++) {
yield index;
}
}

module.exports = {
name: 'Execute Async Iterable List Field',
count: 10,
async measure() {
await execute({
schema,
document,
rootValue: { listField },
});
},
};
28 changes: 28 additions & 0 deletions benchmark/list-sync-benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const { parse } = require('graphql/language/parser.js');
const { execute } = require('graphql/execution/execute.js');
const { buildSchema } = require('graphql/utilities/buildASTSchema.js');

const schema = buildSchema('type Query { listField: [String] }');
const document = parse('{ listField }');

function listField() {
const results = [];
for (let index = 0; index < 100000; index++) {
results.push(index);
}
return results;
}

module.exports = {
name: 'Execute Synchronous List Field',
count: 10,
async measure() {
await execute({
schema,
document,
rootValue: { listField },
});
},
};
1 change: 1 addition & 0 deletions src/__tests__/starWarsIntrospection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('Star Wars Introspection Tests', () => {
{ name: 'Droid' },
{ name: 'Query' },
{ name: 'Boolean' },
{ name: 'Int' },
{ name: '__Schema' },
{ name: '__Type' },
{ name: '__TypeKind' },
Expand Down
270 changes: 270 additions & 0 deletions src/execution/__tests__/defer-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import isAsyncIterable from '../../jsutils/isAsyncIterable';
import { parse } from '../../language/parser';

import { GraphQLID, GraphQLString } from '../../type/scalars';
import { GraphQLSchema } from '../../type/schema';
import { GraphQLObjectType, GraphQLList } from '../../type/definition';

import { execute } from '../execute';

const friendType = new GraphQLObjectType({
fields: {
id: { type: GraphQLID },
name: { type: GraphQLString },
},
name: 'Friend',
});

const friends = [
{ name: 'Han', id: 2 },
{ name: 'Leia', id: 3 },
{ name: 'C-3PO', id: 4 },
];

const heroType = new GraphQLObjectType({
fields: {
id: { type: GraphQLID },
name: { type: GraphQLString },
errorField: {
type: GraphQLString,
resolve: () => {
throw new Error('bad');
},
},
friends: {
type: new GraphQLList(friendType),
resolve: () => friends,
},
},
name: 'Hero',
});

const hero = { name: 'Luke', id: 1 };

const query = new GraphQLObjectType({
fields: {
hero: {
type: heroType,
resolve: () => hero,
},
},
name: 'Query',
});

async function complete(document) {
const schema = new GraphQLSchema({ query });

const result = await execute({
schema,
document,
rootValue: {},
});

if (isAsyncIterable(result)) {
const results = [];
for await (const patch of result) {
results.push(patch);
}
return results;
}
return result;
}

describe('Execute: defer directive', () => {
it('Can defer fragments containing scalar types', async () => {
const document = parse(`
query HeroNameQuery {
hero {
id
...NameFragment @defer
}
}
fragment NameFragment on Hero {
id
name
}
`);
const result = await complete(document);

expect(result).to.deep.equal([
{
data: {
hero: {
id: '1',
},
},
hasNext: true,
},
{
data: {
id: '1',
name: 'Luke',
},
path: ['hero'],
hasNext: false,
},
]);
});
it('Can disable defer using if argument', async () => {
const document = parse(`
query HeroNameQuery {
hero {
id
...NameFragment @defer(if: false)
}
}
fragment NameFragment on Hero {
name
}
`);
const result = await complete(document);

expect(result).to.deep.equal({
data: {
hero: {
id: '1',
name: 'Luke',
},
},
});
});
it('Can defer fragments containing on the top level Query field', async () => {
const document = parse(`
query HeroNameQuery {
...QueryFragment @defer(label: "DeferQuery")
}
fragment QueryFragment on Query {
hero {
id
}
}
`);
const result = await complete(document);

expect(result).to.deep.equal([
{
data: {},
hasNext: true,
},
{
data: {
hero: {
id: '1',
},
},
path: [],
label: 'DeferQuery',
hasNext: false,
},
]);
});
it('Can defer a fragment within an already deferred fragment', async () => {
const document = parse(`
query HeroNameQuery {
hero {
id
...TopFragment @defer(label: "DeferTop")
}
}
fragment TopFragment on Hero {
name
...NestedFragment @defer(label: "DeferNested")
}
fragment NestedFragment on Hero {
friends {
name
}
}
`);
const result = await complete(document);

expect(result).to.deep.equal([
{
data: {
hero: {
id: '1',
},
},
hasNext: true,
},
{
data: {
friends: [{ name: 'Han' }, { name: 'Leia' }, { name: 'C-3PO' }],
},
path: ['hero'],
label: 'DeferNested',
hasNext: true,
},
{
data: {
name: 'Luke',
},
path: ['hero'],
label: 'DeferTop',
hasNext: false,
},
]);
});
it('Can defer an inline fragment', async () => {
const document = parse(`
query HeroNameQuery {
hero {
id
... on Hero @defer(label: "InlineDeferred") {
name
}
}
}
`);
const result = await complete(document);

expect(result).to.deep.equal([
{
data: { hero: { id: '1' } },
hasNext: true,
},
{
data: { name: 'Luke' },
path: ['hero'],
label: 'InlineDeferred',
hasNext: false,
},
]);
});
it('Handles errors thrown in deferred fragments', async () => {
const document = parse(`
query HeroNameQuery {
hero {
id
...NameFragment @defer
}
}
fragment NameFragment on Hero {
errorField
}
`);
const result = await complete(document);

expect(result).to.deep.equal([
{
data: { hero: { id: '1' } },
hasNext: true,
},
{
data: { errorField: null },
path: ['hero'],
errors: [
{
message: 'bad',
locations: [{ line: 9, column: 9 }],
path: ['hero', 'errorField'],
},
],
hasNext: false,
},
]);
});
});
Loading