Skip to content
Open
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
103 changes: 103 additions & 0 deletions src/language/__tests__/schema-parser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,109 @@ describe('Schema Parser', () => {
});
});

it('rejects empty fields definition by default', () => {
expectSyntaxError('type Hello {}').to.deep.equal({
message: 'Syntax Error: Expected Name, found "}".',
locations: [{ line: 1, column: 13 }],
});
});

it('parses empty fields definition with experimentalAllowEmptyTypes', () => {
const doc = parse('type Hello {}', {
experimentalAllowEmptyTypes: true,
});

expectJSON(doc).toDeepEqual({
kind: 'Document',
definitions: [
{
kind: 'ObjectTypeDefinition',
name: nameNode('Hello', { start: 5, end: 10 }),
description: undefined,
interfaces: undefined,
directives: undefined,
fields: [],
loc: { start: 0, end: 13 },
},
],
loc: { start: 0, end: 13 },
});
});

it('parses empty fields definition on interfaces and extensions with experimentalAllowEmptyTypes', () => {
const doc = parse('interface Hello {} extend type World {}', {
experimentalAllowEmptyTypes: true,
});

expectJSON(doc).toDeepNestedProperty('definitions[0].fields', []);
expectJSON(doc).toDeepNestedProperty('definitions[1].fields', []);
});

it('rejects empty interface fields definition by default', () => {
expectSyntaxError('interface Hello {}').to.deep.equal({
message: 'Syntax Error: Expected Name, found "}".',
locations: [{ line: 1, column: 18 }],
});
});

it('parses empty interface fields definition with experimentalAllowEmptyTypes', () => {
const doc = parse('interface Hello {}', {
experimentalAllowEmptyTypes: true,
});

expectJSON(doc).toDeepEqual({
kind: 'Document',
definitions: [
{
kind: 'InterfaceTypeDefinition',
name: nameNode('Hello', { start: 10, end: 15 }),
description: undefined,
interfaces: undefined,
directives: undefined,
fields: [],
loc: { start: 0, end: 18 },
},
],
loc: { start: 0, end: 18 },
});
});

it('rejects empty input fields definition by default', () => {
expectSyntaxError('input Hello {}').to.deep.equal({
message: 'Syntax Error: Expected Name, found "}".',
locations: [{ line: 1, column: 14 }],
});
});

it('parses empty input fields definition with experimentalAllowEmptyTypes', () => {
const doc = parse('input Hello {}', {
experimentalAllowEmptyTypes: true,
});

expectJSON(doc).toDeepEqual({
kind: 'Document',
definitions: [
{
kind: 'InputObjectTypeDefinition',
name: nameNode('Hello', { start: 6, end: 11 }),
description: undefined,
directives: undefined,
fields: [],
loc: { start: 0, end: 14 },
},
],
loc: { start: 0, end: 14 },
});
});

it('parses empty input fields definition on extensions with experimentalAllowEmptyTypes', () => {
const doc = parse('extend input Hello {}', {
experimentalAllowEmptyTypes: true,
});

expectJSON(doc).toDeepNestedProperty('definitions[0].fields', []);
});

it('parses type with description string', () => {
const doc = parse(dedent`
"Description"
Expand Down
49 changes: 49 additions & 0 deletions src/language/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ export interface ParseOptions {
* @internal
*/
lexer?: LexerInterface | undefined;

/**
* EXPERIMENTAL:
*
* If enabled, the parser accepts an empty set of braces as a fields
* definition or an input fields definition, as proposed in
* https://github.com/graphql/graphql-spec/pull/1228,
* https://github.com/graphql/graphql-spec/pull/1229 and
* https://github.com/graphql/graphql-spec/pull/1249
*
* ```graphql
* type Empty {}
* interface Empty {}
* input Empty {}
* ```
*/
experimentalAllowEmptyTypes?: boolean | undefined;
}

/**
Expand Down Expand Up @@ -1204,9 +1221,25 @@ export class Parser {
* FieldsDefinition : { FieldDefinition+ }
* ```
*
* With `experimentalAllowEmptyTypes`:
*
* ```
* FieldsDefinition : { FieldDefinition* }
* ```
*
* @internal
*/
parseFieldsDefinition(): Array<FieldDefinitionNode> | undefined {
if (
this._options.experimentalAllowEmptyTypes === true &&
this.peek(TokenKind.BRACE_L)
) {
return this.any(
TokenKind.BRACE_L,
this.parseFieldDefinition,
TokenKind.BRACE_R,
);
}
return this.optionalMany(
TokenKind.BRACE_L,
this.parseFieldDefinition,
Expand Down Expand Up @@ -1441,9 +1474,25 @@ export class Parser {
* InputFieldsDefinition : { InputValueDefinition+ }
* ```
*
* With `experimentalAllowEmptyTypes`:
*
* ```
* InputFieldsDefinition : { InputValueDefinition* }
* ```
*
* @internal
*/
parseInputFieldsDefinition(): Array<InputValueDefinitionNode> | undefined {
if (
this._options.experimentalAllowEmptyTypes === true &&
this.peek(TokenKind.BRACE_L)
) {
return this.any(
TokenKind.BRACE_L,
this.parseInputValueDef,
TokenKind.BRACE_R,
);
}
return this.optionalMany(
TokenKind.BRACE_L,
this.parseInputValueDef,
Expand Down
Loading
Loading