Skip to content

Commit

Permalink
VariableDefinition Directives: hide behind experimental flag (#1454)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjmahone authored Aug 8, 2018
1 parent 1d7efa9 commit 3fdf240
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/language/__tests__/parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ describe('Parser', () => {

it('parses variable definition directives', () => {
expect(() =>
parse('query Foo($x: Boolean = false @bar) { field }'),
parse('query Foo($x: Boolean = false @bar) { field }', {
experimentalVariableDefinitionDirectives: true,
}),
).to.not.throw();
});

Expand Down
2 changes: 2 additions & 0 deletions src/language/__tests__/printer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('Printer: Query document', () => {

const queryAstWithArtifacts = parse(
'query ($foo: TestType) @testDirective { id, name }',
{ experimentalVariableDefinitionDirectives: true },
);
expect(print(queryAstWithArtifacts)).to.equal(dedent`
query ($foo: TestType) @testDirective {
Expand All @@ -66,6 +67,7 @@ describe('Printer: Query document', () => {

const queryAstWithVariableDirective = parse(
'query ($foo: TestType = {a: 123} @testDirective(if: true) @test) { id }',
{ experimentalVariableDefinitionDirectives: true },
);
expect(print(queryAstWithVariableDirective)).to.equal(dedent`
query ($foo: TestType = {a: 123} @testDirective(if: true) @test) {
Expand Down
25 changes: 24 additions & 1 deletion src/language/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ export type ParseOptions = {
* future.
*/
experimentalFragmentVariables?: boolean,

/**
* EXPERIMENTAL:
*
* If enabled, the parser understands directives on variable definitions:
*
* query Foo($var: String = "abc" @variable_definition_directive) {
* ...
* }
*/
experimentalVariableDefinitionDirectives?: boolean,
};

/**
Expand Down Expand Up @@ -336,14 +347,26 @@ function parseVariableDefinitions(
*/
function parseVariableDefinition(lexer: Lexer<*>): VariableDefinitionNode {
const start = lexer.token;
if (lexer.options.experimentalVariableDefinitionDirectives) {
return {
kind: Kind.VARIABLE_DEFINITION,
variable: parseVariable(lexer),
type: (expect(lexer, TokenKind.COLON), parseTypeReference(lexer)),
defaultValue: skip(lexer, TokenKind.EQUALS)
? parseValueLiteral(lexer, true)
: undefined,
directives: parseDirectives(lexer, true),
loc: loc(lexer, start),
};
}

return {
kind: Kind.VARIABLE_DEFINITION,
variable: parseVariable(lexer),
type: (expect(lexer, TokenKind.COLON), parseTypeReference(lexer)),
defaultValue: skip(lexer, TokenKind.EQUALS)
? parseValueLiteral(lexer, true)
: undefined,
directives: parseDirectives(lexer, true),
loc: loc(lexer, start),
};
}
Expand Down
45 changes: 41 additions & 4 deletions src/validation/__tests__/KnownDirectives-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
* LICENSE file in the root directory of this source tree.
*/

import { expect } from 'chai';
import { describe, it } from 'mocha';
import { parse } from '../../language';
import { buildSchema } from '../../utilities';
import { validate } from '../validate';
import {
expectPassesRule,
expectFailsRule,
expectSDLErrorsFromRule,
testSchema,
} from './harness';

import {
Expand Down Expand Up @@ -127,7 +131,7 @@ describe('Validate: Known directives', () => {
expectPassesRule(
KnownDirectives,
`
query Foo($var: Boolean @onVariableDefinition) @onQuery {
query Foo($var: Boolean) @onQuery {
name @include(if: $var)
...Frag @include(if: true)
skippedField @skip(if: true)
Expand All @@ -141,11 +145,26 @@ describe('Validate: Known directives', () => {
);
});

it('with well placed variable definition directive', () => {
// Need to parse with experimental flag
const queryString = `
query Foo($var: Boolean @onVariableDefinition) {
name
}
`;
const errors = validate(
testSchema,
parse(queryString, { experimentalVariableDefinitionDirectives: true }),
[KnownDirectives],
);
expect(errors).to.deep.equal([], 'Should validate');
});

it('with misplaced directives', () => {
expectFailsRule(
KnownDirectives,
`
query Foo($var: Boolean @onField) @include(if: true) {
query Foo($var: Boolean) @include(if: true) {
name @onQuery @include(if: $var)
...Frag @onQuery
}
Expand All @@ -155,15 +174,33 @@ describe('Validate: Known directives', () => {
}
`,
[
misplacedDirective('onField', 'VARIABLE_DEFINITION', 2, 31),
misplacedDirective('include', 'QUERY', 2, 41),
misplacedDirective('include', 'QUERY', 2, 32),
misplacedDirective('onQuery', 'FIELD', 3, 14),
misplacedDirective('onQuery', 'FRAGMENT_SPREAD', 4, 17),
misplacedDirective('onQuery', 'MUTATION', 7, 20),
],
);
});

it('with misplaced variable definition directive', () => {
// Need to parse with experimental flag
const queryString = `
query Foo($var: Boolean @onField) {
name
}
`;
const errors = validate(
testSchema,
parse(queryString, { experimentalVariableDefinitionDirectives: true }),
[KnownDirectives],
);
const expectedErrors = [
misplacedDirective('onField', 'VARIABLE_DEFINITION', 2, 31),
];
expect(errors).to.have.length.of.at.least(1, 'Should not validate');
expect(errors).to.deep.equal(expectedErrors);
});

describe('within SDL', () => {
it('with directive defined inside SDL', () => {
expectSDLErrors(`
Expand Down

0 comments on commit 3fdf240

Please sign in to comment.