Skip to content

Commit b03b19c

Browse files
authored
Parse option: allowLegacySDLEmptyFields (#1171)
This offers a slightly smoother adoption path for existing code that uses the legacy empty field sets.
1 parent 70cb827 commit b03b19c

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/language/__tests__/schema-parser-test.js

+13
Original file line numberDiff line numberDiff line change
@@ -708,4 +708,17 @@ input Hello {
708708
{ line: 2, column: 33 },
709709
);
710710
});
711+
712+
it('Option: allowLegacySDLEmptyFields supports type with empty fields', () => {
713+
const body = 'type Hello { }';
714+
expect(() => parse(body)).to.throw('Syntax Error: Expected Name, found }');
715+
const doc = parse(body, { allowLegacySDLEmptyFields: true });
716+
expect(doc).to.containSubset({
717+
definitions: [
718+
{
719+
fields: [],
720+
},
721+
],
722+
});
723+
});
711724
});

src/language/parser.js

+20
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ export type ParseOptions = {
119119
*/
120120
noLocation?: boolean,
121121

122+
/**
123+
* If enabled, the parser will parse empty fields sets in the Schema
124+
* Definition Language. Otherwise, the parser will follow the current
125+
* specification.
126+
*
127+
* This option is provided to ease adoption of the final SDL specification
128+
* and will be removed in a future major release.
129+
*/
130+
allowLegacySDLEmptyFields?: boolean,
131+
122132
/**
123133
* EXPERIMENTAL:
124134
*
@@ -929,6 +939,16 @@ function parseImplementsInterfaces(lexer: Lexer<*>): Array<NamedTypeNode> {
929939
* FieldsDefinition : { FieldDefinition+ }
930940
*/
931941
function parseFieldsDefinition(lexer: Lexer<*>): Array<FieldDefinitionNode> {
942+
// Legacy support for the SDL?
943+
if (
944+
lexer.options.allowLegacySDLEmptyFields &&
945+
peek(lexer, TokenKind.BRACE_L) &&
946+
lexer.lookahead().kind === TokenKind.BRACE_R
947+
) {
948+
lexer.advance();
949+
lexer.advance();
950+
return [];
951+
}
932952
return peek(lexer, TokenKind.BRACE_L)
933953
? many(lexer, TokenKind.BRACE_L, parseFieldDefinition, TokenKind.BRACE_R)
934954
: [];

0 commit comments

Comments
 (0)