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

VariableDefinition Directives: hide behind experimental flag #1454

Merged
merged 1 commit into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjmahone Please mark all tests as Experimental simular to this:

it('Experimental: allows parsing fragment defined variables', () => {

Also please add this prefix to tests from #1437

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged before I saw your comments: will fix up now.

// 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