Skip to content

Commit 863ecd5

Browse files
committed
Added empty {} support to type extensions if they have directives
1 parent f4ed68f commit 863ecd5

3 files changed

Lines changed: 59 additions & 27 deletions

File tree

src/main/antlr/GraphqlSDL.g4

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ typeExtension :
3535
inputObjectTypeExtensionDefinition
3636
;
3737

38+
emptyParentheses : '{' '}';
3839

3940
scalarTypeDefinition : description? SCALAR name directives?;
4041

@@ -44,7 +45,7 @@ objectTypeDefinition : description? TYPE name implementsInterfaces? directives?
4445

4546
objectTypeExtensionDefinition :
4647
EXTEND TYPE name implementsInterfaces? directives? extensionFieldsDefinition |
47-
EXTEND TYPE name implementsInterfaces? directives |
48+
EXTEND TYPE name implementsInterfaces? directives emptyParentheses? |
4849
EXTEND TYPE name implementsInterfaces
4950
;
5051

@@ -66,7 +67,7 @@ interfaceTypeDefinition : description? INTERFACE name directives? fieldsDefiniti
6667

6768
interfaceTypeExtensionDefinition :
6869
EXTEND INTERFACE name directives? extensionFieldsDefinition |
69-
EXTEND INTERFACE name directives
70+
EXTEND INTERFACE name directives emptyParentheses?
7071
;
7172

7273

@@ -88,7 +89,7 @@ enumTypeDefinition : description? ENUM name directives? enumValueDefinitions?;
8889

8990
enumTypeExtensionDefinition :
9091
EXTEND ENUM name directives? extensionEnumValueDefinitions |
91-
EXTEND ENUM name directives
92+
EXTEND ENUM name directives emptyParentheses?
9293
;
9394

9495
enumValueDefinitions : '{' enumValueDefinition* '}';
@@ -102,7 +103,7 @@ inputObjectTypeDefinition : description? INPUT name directives? inputObjectValue
102103

103104
inputObjectTypeExtensionDefinition :
104105
EXTEND INPUT name directives? extensionInputObjectValueDefinitions |
105-
EXTEND INPUT name directives
106+
EXTEND INPUT name directives emptyParentheses?
106107
;
107108

108109
inputObjectValueDefinitions : '{' inputValueDefinition* '}';

src/main/java/graphql/parser/GraphqlAntlrToLanguage.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,10 +508,13 @@ protected UnionTypeDefinition createUnionTypeDefinition(GraphqlParser.UnionTypeD
508508
def.description(newDescription(ctx.description()));
509509
def.directives(createDirectives(ctx.directives()));
510510
List<Type> members = new ArrayList<>();
511-
GraphqlParser.UnionMembersContext unionMembersContext = ctx.unionMembership().unionMembers();
512-
while (unionMembersContext != null) {
513-
members.add(0, createTypeName(unionMembersContext.typeName()));
514-
unionMembersContext = unionMembersContext.unionMembers();
511+
GraphqlParser.UnionMembershipContext unionMembership = ctx.unionMembership();
512+
if (unionMembership != null) {
513+
GraphqlParser.UnionMembersContext unionMembersContext = unionMembership.unionMembers();
514+
while (unionMembersContext != null) {
515+
members.add(0, createTypeName(unionMembersContext.typeName()));
516+
unionMembersContext = unionMembersContext.unionMembers();
517+
}
515518
}
516519
def.memberTypes(members);
517520
return def.build();

src/test/groovy/graphql/schema/idl/SchemaParserTest.groovy

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import graphql.language.ObjectTypeDefinition
66
import graphql.language.ScalarTypeDefinition
77
import graphql.schema.idl.errors.SchemaProblem
88
import spock.lang.Specification
9+
import spock.lang.Unroll
910

1011
/**
1112
* We don't want to retest the base GraphQL parser since it has its own testing
@@ -188,7 +189,8 @@ class SchemaParserTest extends Specification {
188189
}
189190

190191

191-
def "empty types (with and without parentheses) are allowed"() {
192+
@Unroll
193+
def "empty types (with and without parentheses) are allowed in '#schema'"() {
192194
//
193195
// empty parentheses are not quite allowed by the spec but in the name of backwards compatibility
194196
// AND general usefulness we are going to allow them. So in the list below the last two of each section
@@ -218,10 +220,13 @@ class SchemaParserTest extends Specification {
218220
''' enum Foo @directive ''' | _
219221
''' enum Foo { } ''' | _
220222
''' enum Foo @directive { } ''' | _
223+
224+
''' union Foo ''' | _
221225
}
222226
223227
224-
def "extensions are not allowed to be empty"() {
228+
@Unroll
229+
def "extensions are not allowed to be empty without directives in '#schema'"() {
225230
226231
expect:
227232
assertSchemaProblem(schema)
@@ -236,29 +241,52 @@ class SchemaParserTest extends Specification {
236241
''' extend input Foo {}''' | _
237242
''' extend enum Foo ''' | _
238243
''' extend enum Foo {}''' | _
244+
''' extend union Foo ''' | _
245+
}
246+
247+
@Unroll
248+
def "extensions are allowed to be empty with directives in '#schema'"() {
249+
250+
expect:
251+
assertNoSchemaProblem(schema)
252+
253+
where:
254+
schema | _
255+
''' extend type Foo @d1 @d2 {}''' | _
256+
''' extend interface Foo @d1 @d2 {}''' | _
257+
''' extend input Foo @d1 @d2 {}''' | _
258+
''' extend enum Foo @d1 @d2 {}''' | _
259+
''' extend union Foo @d1 @d2 ''' | _
239260
}
240261
241-
def "extensions must extend with fields or directives"() {
262+
@Unroll
263+
def "extensions must extend with fields or directives in '#schema'"() {
242264
243265
expect:
244266
assertNoSchemaProblem(schema)
245267
246268
where:
247-
schema | _
248-
''' extend type Foo @directive''' | _
249-
''' extend type Foo { f : Int }''' | _
250-
''' extend type Foo @directive { f : Int }''' | _
251-
252-
''' extend interface Foo @directive ''' | _
253-
''' extend interface Foo { f : Int }''' | _
254-
''' extend interface Foo { f : Int }''' | _
255-
256-
''' extend input Foo @directive ''' | _
257-
''' extend input Foo { f : Int }''' | _
258-
''' extend input Foo { f : Int }''' | _
259-
260-
''' extend enum Foo @directive ''' | _
261-
''' extend enum Foo { a,b,c }''' | _
262-
''' extend enum Foo @directive { a,b,c }''' | _
269+
schema | _
270+
''' extend type Foo @directive''' | _
271+
''' extend type Foo { f : Int }''' | _
272+
''' extend type Foo @directive { f : Int }''' | _
273+
274+
''' extend interface Foo @directive ''' | _
275+
''' extend interface Foo { f : Int }''' | _
276+
''' extend interface Foo { f : Int }''' | _
277+
278+
''' extend input Foo @directive ''' | _
279+
''' extend input Foo { f : Int }''' | _
280+
''' extend input Foo { f : Int }''' | _
281+
282+
''' extend enum Foo @directive ''' | _
283+
''' extend enum Foo { a,b,c }''' | _
284+
''' extend enum Foo @directive { a,b,c }''' | _
285+
286+
''' extend union Foo @directive ''' | _
287+
''' extend union Foo = | a | b | c''' | _
288+
''' extend union Foo = a | b | c''' | _
289+
''' extend union Foo @directive = | a | b | c''' | _
290+
''' extend union Foo @directive = a | b | c''' | _
263291
}
264292
}

0 commit comments

Comments
 (0)