Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/defer-stream' into executor-de…
Browse files Browse the repository at this point in the history
…fer-stream
  • Loading branch information
yaacovCR committed Oct 26, 2021
2 parents 5dd52c5 + a09e09c commit 0a6bf12
Show file tree
Hide file tree
Showing 24 changed files with 4,818 additions and 99 deletions.
276 changes: 276 additions & 0 deletions src/execution/__tests__/defer-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import type { DocumentNode } from 'graphql';
import {
GraphQLID,
GraphQLList,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
parse,
} from 'graphql';

import { isAsyncIterable } from '../../jsutils/isAsyncIterable';

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

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: DocumentNode) {
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);

expectJSON(result).toDeepEqual([
{
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

0 comments on commit 0a6bf12

Please sign in to comment.