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

Parse option: allowLegacySDLEmptyFields #1171

Merged
merged 1 commit into from
Dec 20, 2017
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
13 changes: 13 additions & 0 deletions src/language/__tests__/schema-parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,4 +708,17 @@ input Hello {
{ line: 2, column: 33 },
);
});

it('Option: allowLegacySDLEmptyFields supports type with empty fields', () => {
const body = 'type Hello { }';
expect(() => parse(body)).to.throw('Syntax Error: Expected Name, found }');
const doc = parse(body, { allowLegacySDLEmptyFields: true });
expect(doc).to.containSubset({
definitions: [
{
fields: [],
},
],
});
});
});
20 changes: 20 additions & 0 deletions src/language/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ export type ParseOptions = {
*/
noLocation?: boolean,

/**
* If enabled, the parser will parse empty fields sets in the Schema
* Definition Language. Otherwise, the parser will follow the current
* specification.
*
* This option is provided to ease adoption of the final SDL specification
* and will be removed in a future major release.
*/
allowLegacySDLEmptyFields?: boolean,

/**
* EXPERIMENTAL:
*
Expand Down Expand Up @@ -929,6 +939,16 @@ function parseImplementsInterfaces(lexer: Lexer<*>): Array<NamedTypeNode> {
* FieldsDefinition : { FieldDefinition+ }
*/
function parseFieldsDefinition(lexer: Lexer<*>): Array<FieldDefinitionNode> {
// Legacy support for the SDL?
if (
lexer.options.allowLegacySDLEmptyFields &&
peek(lexer, TokenKind.BRACE_L) &&
lexer.lookahead().kind === TokenKind.BRACE_R
) {
lexer.advance();
lexer.advance();
return [];
}
return peek(lexer, TokenKind.BRACE_L)
? many(lexer, TokenKind.BRACE_L, parseFieldDefinition, TokenKind.BRACE_R)
: [];
Expand Down