Skip to content

Commit

Permalink
Parse option: allowLegacySDLEmptyFields (#1171)
Browse files Browse the repository at this point in the history
This offers a slightly smoother adoption path for existing code that uses the legacy empty field sets.
  • Loading branch information
leebyron authored Dec 20, 2017
1 parent 70cb827 commit b03b19c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
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

0 comments on commit b03b19c

Please sign in to comment.