Skip to content

Commit 44768a8

Browse files
committed
BUG: Ensure building AST schema does not exclude @Skip and @include (#380)
`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 88cf354 commit 44768a8

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

src/utilities/__tests__/buildASTSchema-test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import { describe, it } from 'mocha';
1212
import { parse } from '../../language';
1313
import { printSchema } from '../schemaPrinter';
1414
import { buildASTSchema } from '../buildASTSchema';
15+
import {
16+
GraphQLSkipDirective,
17+
GraphQLIncludeDirective,
18+
} from '../../type/directives';
1519

1620
/**
1721
* This function does a full cycle of going from a
@@ -61,6 +65,62 @@ type Hello {
6165
expect(output).to.equal(body);
6266
});
6367

68+
69+
it('Maintains @skip & @include', () => {
70+
const body = `
71+
schema {
72+
query: Hello
73+
}
74+
75+
type Hello {
76+
str: String
77+
}
78+
`;
79+
const schema = buildASTSchema(parse(body));
80+
expect(schema.getDirectives().length).to.equal(2);
81+
expect(schema.getDirective('skip')).to.equal(GraphQLSkipDirective);
82+
expect(schema.getDirective('include')).to.equal(GraphQLIncludeDirective);
83+
});
84+
85+
it('Overriding directives excludes built-ins', () => {
86+
const body = `
87+
schema {
88+
query: Hello
89+
}
90+
91+
directive @skip on FIELD
92+
directive @include on FIELD
93+
94+
type Hello {
95+
str: String
96+
}
97+
`;
98+
const schema = buildASTSchema(parse(body));
99+
expect(schema.getDirectives().length).to.equal(2);
100+
expect(schema.getDirective('skip')).to.not.equal(GraphQLSkipDirective);
101+
expect(
102+
schema.getDirective('include')
103+
).to.not.equal(GraphQLIncludeDirective);
104+
});
105+
106+
it('Adding directives maintains @skip & @include', () => {
107+
const body = `
108+
schema {
109+
query: Hello
110+
}
111+
112+
directive @foo(arg: Int) on FIELD
113+
114+
type Hello {
115+
str: String
116+
}
117+
`;
118+
const schema = buildASTSchema(parse(body));
119+
expect(schema.getDirectives().length).to.equal(3);
120+
expect(schema.getDirective('skip')).to.not.equal(undefined);
121+
expect(schema.getDirective('include')).to.not.equal(undefined);
122+
});
123+
64124
it('Type modifiers', () => {
65125
const body = `
66126
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)