Skip to content

Commit

Permalink
Add parsing and validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leebyron committed Apr 19, 2018
1 parent 6e65e8e commit ff5f744
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 19 deletions.
60 changes: 60 additions & 0 deletions src/language/__tests__/schema-parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,66 @@ extend type Hello {
);
});

it('Schema extension', () => {
const body = `
extend schema {
mutation: Mutation
}`;
const doc = parse(body);
const expected = {
kind: 'Document',
definitions: [
{
kind: 'SchemaExtension',
directives: [],
operationTypes: [
{
kind: 'OperationTypeDefinition',
operation: 'mutation',
type: typeNode('Mutation', { start: 41, end: 49 }),
loc: { start: 31, end: 49 },
},
],
loc: { start: 7, end: 57 },
},
],
loc: { start: 0, end: 57 },
};
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Schema extension with only directives', () => {
const body = 'extend schema @directive';
const doc = parse(body);
const expected = {
kind: 'Document',
definitions: [
{
kind: 'SchemaExtension',
directives: [
{
kind: 'Directive',
name: nameNode('directive', { start: 15, end: 24 }),
arguments: [],
loc: { start: 14, end: 24 },
},
],
operationTypes: [],
loc: { start: 0, end: 24 },
},
],
loc: { start: 0, end: 24 },
};
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Schema extension without anything throws', () => {
expectSyntaxError('extend schema', 'Unexpected <EOF>', {
line: 1,
column: 14,
});
});

it('Simple non-null type', () => {
const body = `
type Hello {
Expand Down
8 changes: 4 additions & 4 deletions src/language/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,11 @@ export type InputObjectTypeDefinitionNode = {

export type DirectiveDefinitionNode = {
+kind: 'DirectiveDefinition',
+loc ?: Location,
+description ?: StringValueNode,
+loc?: Location,
+description?: StringValueNode,
+name: NameNode,
+arguments ?: $ReadOnlyArray < InputValueDefinitionNode >,
+locations: $ReadOnlyArray < NameNode >,
+arguments?: $ReadOnlyArray<InputValueDefinitionNode>,
+locations: $ReadOnlyArray<NameNode>,
};

// Type System Extensions
Expand Down
2 changes: 1 addition & 1 deletion src/language/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import type {
EnumTypeDefinitionNode,
EnumValueDefinitionNode,
InputObjectTypeDefinitionNode,
DirectiveDefinitionNode,
TypeSystemExtensionNode,
SchemaExtensionNode,
ScalarTypeExtensionNode,
Expand All @@ -60,7 +61,6 @@ import type {
UnionTypeExtensionNode,
EnumTypeExtensionNode,
InputObjectTypeExtensionNode,
DirectiveDefinitionNode,
} from './ast';

import { Kind } from './kinds';
Expand Down
22 changes: 11 additions & 11 deletions src/language/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@ const printDocASTReducer = {
join(['input', name, join(directives, ' '), block(fields)], ' '),
),

DirectiveDefinition: addDescription(
({ name, arguments: args, locations }) =>
'directive @' +
name +
(args.every(arg => arg.indexOf('\n') === -1)
? wrap('(', join(args, ', '), ')')
: wrap('(\n', indent(join(args, '\n')), '\n)')) +
' on ' +
join(locations, ' | '),
),

SchemaExtension: ({ directives, operationTypes }) =>
join(['extend schema', join(directives, ' '), block(operationTypes)], ' '),

Expand Down Expand Up @@ -212,17 +223,6 @@ const printDocASTReducer = {

InputObjectTypeExtension: ({ name, directives, fields }) =>
join(['extend input', name, join(directives, ' '), block(fields)], ' '),

DirectiveDefinition: addDescription(
({ name, arguments: args, locations }) =>
'directive @' +
name +
(args.every(arg => arg.indexOf('\n') === -1)
? wrap('(', join(args, ', '), ')')
: wrap('(\n', indent(join(args, '\n')), '\n)')) +
' on ' +
join(locations, ' | '),
),
};

function addDescription(cb) {
Expand Down
7 changes: 4 additions & 3 deletions src/language/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export const QueryDocumentKeys = {
NonNullType: ['type'],

SchemaDefinition: ['directives', 'operationTypes'],
SchemaExtension: ['directives', 'operationTypes'],
OperationTypeDefinition: ['type'],

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

DirectiveDefinition: ['description', 'name', 'arguments', 'locations'],

SchemaExtension: ['directives', 'operationTypes'],

ScalarTypeExtension: ['name', 'directives'],
ObjectTypeExtension: ['name', 'interfaces', 'directives', 'fields'],
InterfaceTypeExtension: ['name', 'directives', 'fields'],
UnionTypeExtension: ['name', 'directives', 'types'],
EnumTypeExtension: ['name', 'directives', 'values'],
InputObjectTypeExtension: ['name', 'directives', 'fields'],

DirectiveDefinition: ['description', 'name', 'arguments', 'locations'],
};

export const BREAK = {};
Expand Down
3 changes: 3 additions & 0 deletions src/validation/__tests__/ExecutableDefinitions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,13 @@ describe('Validate: Executable definitions', () => {
type Query {
test: String
}
extend schema @directive
`,
[
nonExecutableDefinition('schema', 2, 7),
nonExecutableDefinition('Query', 6, 7),
nonExecutableDefinition('schema', 10, 7),
],
);
});
Expand Down
5 changes: 5 additions & 0 deletions src/validation/__tests__/KnownDirectives-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ describe('Validate: Known directives', () => {
schema @onSchema {
query: MyQuery
}
extend schema @onSchema
`,
);
});
Expand Down Expand Up @@ -209,6 +211,8 @@ describe('Validate: Known directives', () => {
schema @onObject {
query: MyQuery
}
extend schema @onObject
`,
[
misplacedDirective('onInterface', 'OBJECT', 2, 43),
Expand Down Expand Up @@ -249,6 +253,7 @@ describe('Validate: Known directives', () => {
24,
),
misplacedDirective('onObject', 'SCHEMA', 22, 16),
misplacedDirective('onObject', 'SCHEMA', 26, 23),
],
);
});
Expand Down

0 comments on commit ff5f744

Please sign in to comment.