Skip to content

Commit ff5f744

Browse files
committed
Add parsing and validation tests
1 parent 6e65e8e commit ff5f744

File tree

7 files changed

+88
-19
lines changed

7 files changed

+88
-19
lines changed

src/language/__tests__/schema-parser-test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,66 @@ extend type Hello {
263263
);
264264
});
265265

266+
it('Schema extension', () => {
267+
const body = `
268+
extend schema {
269+
mutation: Mutation
270+
}`;
271+
const doc = parse(body);
272+
const expected = {
273+
kind: 'Document',
274+
definitions: [
275+
{
276+
kind: 'SchemaExtension',
277+
directives: [],
278+
operationTypes: [
279+
{
280+
kind: 'OperationTypeDefinition',
281+
operation: 'mutation',
282+
type: typeNode('Mutation', { start: 41, end: 49 }),
283+
loc: { start: 31, end: 49 },
284+
},
285+
],
286+
loc: { start: 7, end: 57 },
287+
},
288+
],
289+
loc: { start: 0, end: 57 },
290+
};
291+
expect(printJson(doc)).to.equal(printJson(expected));
292+
});
293+
294+
it('Schema extension with only directives', () => {
295+
const body = 'extend schema @directive';
296+
const doc = parse(body);
297+
const expected = {
298+
kind: 'Document',
299+
definitions: [
300+
{
301+
kind: 'SchemaExtension',
302+
directives: [
303+
{
304+
kind: 'Directive',
305+
name: nameNode('directive', { start: 15, end: 24 }),
306+
arguments: [],
307+
loc: { start: 14, end: 24 },
308+
},
309+
],
310+
operationTypes: [],
311+
loc: { start: 0, end: 24 },
312+
},
313+
],
314+
loc: { start: 0, end: 24 },
315+
};
316+
expect(printJson(doc)).to.equal(printJson(expected));
317+
});
318+
319+
it('Schema extension without anything throws', () => {
320+
expectSyntaxError('extend schema', 'Unexpected <EOF>', {
321+
line: 1,
322+
column: 14,
323+
});
324+
});
325+
266326
it('Simple non-null type', () => {
267327
const body = `
268328
type Hello {

src/language/ast.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,11 +503,11 @@ export type InputObjectTypeDefinitionNode = {
503503

504504
export type DirectiveDefinitionNode = {
505505
+kind: 'DirectiveDefinition',
506-
+loc ?: Location,
507-
+description ?: StringValueNode,
506+
+loc?: Location,
507+
+description?: StringValueNode,
508508
+name: NameNode,
509-
+arguments ?: $ReadOnlyArray < InputValueDefinitionNode >,
510-
+locations: $ReadOnlyArray < NameNode >,
509+
+arguments?: $ReadOnlyArray<InputValueDefinitionNode>,
510+
+locations: $ReadOnlyArray<NameNode>,
511511
};
512512

513513
// Type System Extensions

src/language/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import type {
5252
EnumTypeDefinitionNode,
5353
EnumValueDefinitionNode,
5454
InputObjectTypeDefinitionNode,
55+
DirectiveDefinitionNode,
5556
TypeSystemExtensionNode,
5657
SchemaExtensionNode,
5758
ScalarTypeExtensionNode,
@@ -60,7 +61,6 @@ import type {
6061
UnionTypeExtensionNode,
6162
EnumTypeExtensionNode,
6263
InputObjectTypeExtensionNode,
63-
DirectiveDefinitionNode,
6464
} from './ast';
6565

6666
import { Kind } from './kinds';

src/language/printer.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ const printDocASTReducer = {
175175
join(['input', name, join(directives, ' '), block(fields)], ' '),
176176
),
177177

178+
DirectiveDefinition: addDescription(
179+
({ name, arguments: args, locations }) =>
180+
'directive @' +
181+
name +
182+
(args.every(arg => arg.indexOf('\n') === -1)
183+
? wrap('(', join(args, ', '), ')')
184+
: wrap('(\n', indent(join(args, '\n')), '\n)')) +
185+
' on ' +
186+
join(locations, ' | '),
187+
),
188+
178189
SchemaExtension: ({ directives, operationTypes }) =>
179190
join(['extend schema', join(directives, ' '), block(operationTypes)], ' '),
180191

@@ -212,17 +223,6 @@ const printDocASTReducer = {
212223

213224
InputObjectTypeExtension: ({ name, directives, fields }) =>
214225
join(['extend input', name, join(directives, ' '), block(fields)], ' '),
215-
216-
DirectiveDefinition: addDescription(
217-
({ name, arguments: args, locations }) =>
218-
'directive @' +
219-
name +
220-
(args.every(arg => arg.indexOf('\n') === -1)
221-
? wrap('(', join(args, ', '), ')')
222-
: wrap('(\n', indent(join(args, '\n')), '\n)')) +
223-
' on ' +
224-
join(locations, ' | '),
225-
),
226226
};
227227

228228
function addDescription(cb) {

src/language/visitor.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ export const QueryDocumentKeys = {
9999
NonNullType: ['type'],
100100

101101
SchemaDefinition: ['directives', 'operationTypes'],
102-
SchemaExtension: ['directives', 'operationTypes'],
103102
OperationTypeDefinition: ['type'],
104103

105104
ScalarTypeDefinition: ['description', 'name', 'directives'],
@@ -124,14 +123,16 @@ export const QueryDocumentKeys = {
124123
EnumValueDefinition: ['description', 'name', 'directives'],
125124
InputObjectTypeDefinition: ['description', 'name', 'directives', 'fields'],
126125

126+
DirectiveDefinition: ['description', 'name', 'arguments', 'locations'],
127+
128+
SchemaExtension: ['directives', 'operationTypes'],
129+
127130
ScalarTypeExtension: ['name', 'directives'],
128131
ObjectTypeExtension: ['name', 'interfaces', 'directives', 'fields'],
129132
InterfaceTypeExtension: ['name', 'directives', 'fields'],
130133
UnionTypeExtension: ['name', 'directives', 'types'],
131134
EnumTypeExtension: ['name', 'directives', 'values'],
132135
InputObjectTypeExtension: ['name', 'directives', 'fields'],
133-
134-
DirectiveDefinition: ['description', 'name', 'arguments', 'locations'],
135136
};
136137

137138
export const BREAK = {};

src/validation/__tests__/ExecutableDefinitions-test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,13 @@ describe('Validate: Executable definitions', () => {
8787
type Query {
8888
test: String
8989
}
90+
91+
extend schema @directive
9092
`,
9193
[
9294
nonExecutableDefinition('schema', 2, 7),
9395
nonExecutableDefinition('Query', 6, 7),
96+
nonExecutableDefinition('schema', 10, 7),
9497
],
9598
);
9699
});

src/validation/__tests__/KnownDirectives-test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ describe('Validate: Known directives', () => {
178178
schema @onSchema {
179179
query: MyQuery
180180
}
181+
182+
extend schema @onSchema
181183
`,
182184
);
183185
});
@@ -209,6 +211,8 @@ describe('Validate: Known directives', () => {
209211
schema @onObject {
210212
query: MyQuery
211213
}
214+
215+
extend schema @onObject
212216
`,
213217
[
214218
misplacedDirective('onInterface', 'OBJECT', 2, 43),
@@ -249,6 +253,7 @@ describe('Validate: Known directives', () => {
249253
24,
250254
),
251255
misplacedDirective('onObject', 'SCHEMA', 22, 16),
256+
misplacedDirective('onObject', 'SCHEMA', 26, 23),
252257
],
253258
);
254259
});

0 commit comments

Comments
 (0)