forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/defer-stream' into executor-de…
…fer-stream
- Loading branch information
Showing
24 changed files
with
4,818 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
]); | ||
}); | ||
}); |
Oops, something went wrong.