Skip to content

Commit 99bb0ac

Browse files
committed
BUG: Ensure building AST schema does not exclude @Skip and @include
`buildASTSchema` used to not regard directives in 0.4.x, just always including only `@skip` and `@include`. Since 0.5.0 included the ability to use directives in the experimental schema language, existing use of this tool found no defined directives and therefore excluded these two built-ins. This fixes the issue by implicitly adding these built-in directives if they were not explicitly defined.
1 parent 2ac41f6 commit 99bb0ac

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/utilities/__tests__/buildASTSchema-test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,41 @@ type Hello {
6161
expect(output).to.equal(body);
6262
});
6363

64+
65+
it('Maintains @skip & @include', () => {
66+
const body = `
67+
schema {
68+
query: Hello
69+
}
70+
71+
type Hello {
72+
str: String
73+
}
74+
`;
75+
const schema = buildASTSchema(parse(body));
76+
expect(schema.getDirectives().length).to.equal(2);
77+
expect(schema.getDirective('skip')).to.not.equal(undefined);
78+
expect(schema.getDirective('include')).to.not.equal(undefined);
79+
});
80+
81+
it('Adding directives maintains @skip & @include', () => {
82+
const body = `
83+
schema {
84+
query: Hello
85+
}
86+
87+
directive @foo(arg: Int) on FIELD
88+
89+
type Hello {
90+
str: String
91+
}
92+
`;
93+
const schema = buildASTSchema(parse(body));
94+
expect(schema.getDirectives().length).to.equal(3);
95+
expect(schema.getDirective('skip')).to.not.equal(undefined);
96+
expect(schema.getDirective('include')).to.not.equal(undefined);
97+
});
98+
6499
it('Type modifiers', () => {
65100
const body = `
66101
schema {

src/utilities/buildASTSchema.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import { valueFromAST } from './valueFromAST';
1616
import {
1717
LIST_TYPE,
1818
NON_NULL_TYPE,
19-
} from '../language/kinds';
20-
21-
import {
2219
DOCUMENT,
2320
SCHEMA_DEFINITION,
2421
SCALAR_TYPE_DEFINITION,
@@ -63,7 +60,11 @@ import {
6360
GraphQLNonNull,
6461
} from '../type';
6562

66-
import { GraphQLDirective } from '../type/directives';
63+
import {
64+
GraphQLDirective,
65+
GraphQLSkipDirective,
66+
GraphQLIncludeDirective,
67+
} from '../type/directives';
6768

6869
import {
6970
__Schema,
@@ -223,6 +224,15 @@ export function buildASTSchema(ast: Document): GraphQLSchema {
223224

224225
const directives = directiveDefs.map(getDirective);
225226

227+
// If skip and include were not explicitly declared, add them.
228+
if (!directives.some(directive => directive.name === 'skip')) {
229+
directives.push(GraphQLSkipDirective);
230+
}
231+
232+
if (!directives.some(directive => directive.name === 'include')) {
233+
directives.push(GraphQLIncludeDirective);
234+
}
235+
226236
return new GraphQLSchema({
227237
query: getObjectType(astMap[queryTypeName]),
228238
mutation: mutationTypeName ? getObjectType(astMap[mutationTypeName]) : null,

0 commit comments

Comments
 (0)