Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Added support for repeatable directives #1541

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/language/__tests__/schema-kitchen-sink.graphql
Expand Up @@ -124,6 +124,10 @@ directive @include2(if: Boolean!) on
| FRAGMENT_SPREAD
| INLINE_FRAGMENT

directive @myRepeatableDir(name: String!) repeatable on
| OBJECT
| INTERFACE

extend schema @onSchema

extend schema @onSchema {
Expand Down
102 changes: 102 additions & 0 deletions src/language/__tests__/schema-parser-test.js
Expand Up @@ -804,6 +804,108 @@ input Hello {
);
});

it('Directive definition', () => {
const body = 'directive @foo on OBJECT | INTERFACE';
const doc = parse(body);

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
definitions: [
{
kind: 'DirectiveDefinition',
description: undefined,
name: {
kind: 'Name',
value: 'foo',
loc: {
start: 11,
end: 14,
},
},
arguments: [],
repeatable: false,
locations: [
{
kind: 'Name',
value: 'OBJECT',
loc: {
start: 18,
end: 24,
},
},
{
kind: 'Name',
value: 'INTERFACE',
loc: {
start: 27,
end: 36,
},
},
],
loc: {
start: 0,
end: 36,
},
},
],
loc: {
start: 0,
end: 36,
},
});
});

it('Repeatable directive definition', () => {
const body = 'directive @foo repeatable on OBJECT | INTERFACE';
const doc = parse(body);

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
definitions: [
{
kind: 'DirectiveDefinition',
description: undefined,
name: {
kind: 'Name',
value: 'foo',
loc: {
start: 11,
end: 14,
},
},
arguments: [],
repeatable: true,
locations: [
{
kind: 'Name',
value: 'OBJECT',
loc: {
start: 29,
end: 35,
},
},
{
kind: 'Name',
value: 'INTERFACE',
loc: {
start: 38,
end: 47,
},
},
],
loc: {
start: 0,
end: 47,
},
},
],
loc: {
start: 0,
end: 47,
},
});
});

it('Directive with incorrect locations', () => {
expectSyntaxError(
`
Expand Down
2 changes: 2 additions & 0 deletions src/language/__tests__/schema-printer-test.js
Expand Up @@ -164,6 +164,8 @@ describe('Printer: SDL document', () => {

directive @include2(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @myRepeatableDir(name: String!) repeatable on OBJECT | INTERFACE

extend schema @onSchema

extend schema @onSchema {
Expand Down
1 change: 1 addition & 0 deletions src/language/ast.js
Expand Up @@ -508,6 +508,7 @@ export type DirectiveDefinitionNode = {
+description?: StringValueNode,
+name: NameNode,
+arguments?: $ReadOnlyArray<InputValueDefinitionNode>,
+repeatable: boolean,
+locations: $ReadOnlyArray<NameNode>,
};

Expand Down
19 changes: 18 additions & 1 deletion src/language/parser.js
Expand Up @@ -1378,7 +1378,7 @@ function parseInputObjectTypeExtension(

/**
* DirectiveDefinition :
* - Description? directive @ Name ArgumentsDefinition? on DirectiveLocations
* - Description? directive @ Name ArgumentsDefinition? `repeatable`? on DirectiveLocations
*/
function parseDirectiveDefinition(lexer: Lexer<*>): DirectiveDefinitionNode {
const start = lexer.token;
Expand All @@ -1387,13 +1387,15 @@ function parseDirectiveDefinition(lexer: Lexer<*>): DirectiveDefinitionNode {
expect(lexer, TokenKind.AT);
const name = parseName(lexer);
const args = parseArgumentDefs(lexer);
const repeatable = expectOptionalKeyword(lexer, 'repeatable');
expectKeyword(lexer, 'on');
const locations = parseDirectiveLocations(lexer);
return {
kind: Kind.DIRECTIVE_DEFINITION,
description,
name,
arguments: args,
repeatable,
locations,
loc: loc(lexer, start),
};
Expand Down Expand Up @@ -1529,6 +1531,21 @@ function expectKeyword(lexer: Lexer<*>, value: string): Token {
);
}

/**
* If the next token is a keyword with the given value, return true after advancing
* the lexer. Otherwise, do not change the parser state and return false.
*/
function expectOptionalKeyword(lexer: Lexer<*>, value: string): boolean {
const token = lexer.token;
const match = token.kind === TokenKind.NAME && token.value === value;

if (match) {
lexer.advance();
}

return match;
}

/**
* Helper function for creating an error when an unexpected lexed token
* is encountered.
Expand Down
3 changes: 2 additions & 1 deletion src/language/printer.js
Expand Up @@ -181,12 +181,13 @@ const printDocASTReducer = {
),

DirectiveDefinition: addDescription(
({ name, arguments: args, locations }) =>
({ name, arguments: args, locations, repeatable }) =>
'directive @' +
name +
(args.every(arg => arg.indexOf('\n') === -1)
? wrap('(', join(args, ', '), ')')
: wrap('(\n', indent(join(args, '\n')), '\n)')) +
(repeatable ? ' repeatable' : '') +
' on ' +
join(locations, ' | '),
),
Expand Down
18 changes: 18 additions & 0 deletions src/type/__tests__/introspection-test.js
Expand Up @@ -700,6 +700,21 @@ describe('Introspection', () => {
isDeprecated: false,
deprecationReason: null,
},
{
args: [],
deprecationReason: null,
isDeprecated: false,
name: 'repeatable',
type: {
kind: 'NON_NULL',
name: null,
ofType: {
kind: 'SCALAR',
name: 'Boolean',
ofType: null,
},
},
},
],
inputFields: null,
interfaces: [],
Expand Down Expand Up @@ -815,6 +830,7 @@ describe('Introspection', () => {
directives: [
{
name: 'include',
repeatable: false,
locations: ['FIELD', 'FRAGMENT_SPREAD', 'INLINE_FRAGMENT'],
args: [
{
Expand All @@ -834,6 +850,7 @@ describe('Introspection', () => {
},
{
name: 'skip',
repeatable: false,
locations: ['FIELD', 'FRAGMENT_SPREAD', 'INLINE_FRAGMENT'],
args: [
{
Expand All @@ -853,6 +870,7 @@ describe('Introspection', () => {
},
{
name: 'deprecated',
repeatable: false,
locations: ['FIELD_DEFINITION', 'ENUM_VALUE'],
args: [
{
Expand Down
4 changes: 4 additions & 0 deletions src/type/directives.js
Expand Up @@ -44,12 +44,15 @@ export class GraphQLDirective {
locations: Array<DirectiveLocationEnum>;
args: Array<GraphQLArgument>;
astNode: ?DirectiveDefinitionNode;
repeatable: boolean;

constructor(config: GraphQLDirectiveConfig): void {
this.name = config.name;
this.description = config.description;
this.locations = config.locations;
this.astNode = config.astNode;
this.repeatable = config.repeatable == null ? false : config.repeatable;

invariant(config.name, 'Directive must be named.');
invariant(
Array.isArray(config.locations),
Expand Down Expand Up @@ -92,6 +95,7 @@ export type GraphQLDirectiveConfig = {|
locations: Array<DirectiveLocationEnum>,
args?: ?GraphQLFieldConfigArgumentMap,
astNode?: ?DirectiveDefinitionNode,
repeatable?: ?boolean,
|};

/**
Expand Down
6 changes: 6 additions & 0 deletions src/type/introspection.js
Expand Up @@ -98,6 +98,12 @@ export const __Directive = new GraphQLObjectType({
type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))),
resolve: directive => directive.args || [],
},
repeatable: {
type: GraphQLNonNull(GraphQLBoolean),
description:
'Permits using the directive multiple times at the same location.',
resolve: directive => directive.repeatable,
},
}),
});

Expand Down
16 changes: 16 additions & 0 deletions src/utilities/__tests__/buildASTSchema-test.js
Expand Up @@ -91,6 +91,22 @@ describe('Schema Builder', () => {
expect(output).to.equal(body);
});

it('With repeatable directives', () => {
const body = dedent`
directive @foo(arg: Int) repeatable on FIELD

type Query {
str: String
}
`;

const output = cycleOutput(body);
expect(output).to.equal(body);

const schema = buildASTSchema(parse(body));
expect(schema.getDirective('foo').repeatable).to.equal(true);
});

it('Supports descriptions', () => {
const body = dedent`
"""This is a directive"""
Expand Down
6 changes: 6 additions & 0 deletions src/utilities/__tests__/buildClientSchema-test.js
Expand Up @@ -583,6 +583,12 @@ describe('Type System: build schema from introspection', () => {
description: 'This is a custom directive',
locations: ['FIELD'],
}),
new GraphQLDirective({
name: 'customRepeatableDirective',
description: 'This is a custom repeatable directive',
repeatable: true,
locations: ['FIELD'],
}),
],
});

Expand Down
11 changes: 10 additions & 1 deletion src/utilities/__tests__/extendSchema-test.js
Expand Up @@ -115,6 +115,15 @@ const FooDirective = new GraphQLDirective({
],
});

const RepeatableDirective = new GraphQLDirective({
name: 'repeatableDirective',
args: {
input: { type: SomeInputType },
},
repeatable: true,
locations: [DirectiveLocation.OBJECT, DirectiveLocation.INTERFACE],
});

const testSchema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
Expand All @@ -134,7 +143,7 @@ const testSchema = new GraphQLSchema({
}),
}),
types: [FooType, BarType],
directives: specifiedDirectives.concat([FooDirective]),
directives: specifiedDirectives.concat([FooDirective, RepeatableDirective]),
});

function extendTestSchema(sdl, options) {
Expand Down
18 changes: 18 additions & 0 deletions src/utilities/__tests__/findBreakingChanges-test.js
Expand Up @@ -35,6 +35,7 @@ import {
findAddedNonNullDirectiveArgs,
findRemovedLocationsForDirective,
findRemovedDirectiveLocations,
findRemovedDirectiveRepeatable,
} from '../findBreakingChanges';

import {
Expand Down Expand Up @@ -1007,6 +1008,23 @@ describe('findBreakingChanges', () => {
},
]);
});

it('should detect removal of repeatable flag', () => {
const oldSchema = buildSchema(`
directive @foo repeatable on OBJECT
`);

const newSchema = buildSchema(`
directive @foo on OBJECT
`);

expect(findRemovedDirectiveRepeatable(oldSchema, newSchema)).to.eql([
{
type: BreakingChangeType.DIRECTIVE_REPEATABLE_REMOVED,
description: 'Repeatable flag was removed from foo',
},
]);
});
});

describe('findDangerousChanges', () => {
Expand Down
6 changes: 6 additions & 0 deletions src/utilities/__tests__/schemaPrinter-test.js
Expand Up @@ -650,6 +650,9 @@ describe('Type System Printer', () => {
description: String
locations: [__DirectiveLocation!]!
args: [__InputValue!]!

"""Permits using the directive multiple times at the same location."""
repeatable: Boolean!
}

"""
Expand Down Expand Up @@ -883,6 +886,9 @@ describe('Type System Printer', () => {
description: String
locations: [__DirectiveLocation!]!
args: [__InputValue!]!

# Permits using the directive multiple times at the same location.
repeatable: Boolean!
}

# A Directive can be adjacent to many parts of the GraphQL language, a
Expand Down