From 8fe4803f5178bba8f9024d254ed136c56e77ef73 Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Wed, 20 Dec 2017 11:42:17 -0800 Subject: [PATCH] Parse option: allowLegacySDLEmptyFields This offers a slightly smoother adoption path for existing code that uses the legacy empty field sets. --- src/language/__tests__/schema-parser-test.js | 13 +++++++++++++ src/language/parser.js | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/language/__tests__/schema-parser-test.js b/src/language/__tests__/schema-parser-test.js index b94286ed16..e150a3f594 100644 --- a/src/language/__tests__/schema-parser-test.js +++ b/src/language/__tests__/schema-parser-test.js @@ -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: [], + }, + ], + }); + }); }); diff --git a/src/language/parser.js b/src/language/parser.js index 0cf6225a64..9655ad3700 100644 --- a/src/language/parser.js +++ b/src/language/parser.js @@ -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: * @@ -929,6 +939,16 @@ function parseImplementsInterfaces(lexer: Lexer<*>): Array { * FieldsDefinition : { FieldDefinition+ } */ function parseFieldsDefinition(lexer: Lexer<*>): Array { + // 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) : [];