Skip to content

Commit 5eae29a

Browse files
committed
fix union IDL parsing and add tests for it
1 parent 96578f4 commit 5eae29a

3 files changed

Lines changed: 149 additions & 6 deletions

File tree

‎src/main/java/graphql/schema/idl/SchemaGenerator.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import graphql.language.Type;
2323
import graphql.language.TypeDefinition;
2424
import graphql.language.TypeExtensionDefinition;
25-
import graphql.language.TypeName;
2625
import graphql.language.UnionTypeDefinition;
2726
import graphql.language.Value;
2827
import graphql.schema.DataFetcher;
@@ -253,6 +252,7 @@ private GraphQLInputType buildInputType(BuildContext buildCtx, Type rawType) {
253252
}
254253

255254
private GraphQLObjectType buildObjectType(BuildContext buildCtx, ObjectTypeDefinition typeDefinition) {
255+
256256
GraphQLObjectType.Builder builder = GraphQLObjectType.newObject();
257257
builder.name(typeDefinition.getName());
258258
builder.description(buildDescription(typeDefinition));
@@ -331,7 +331,8 @@ private GraphQLUnionType buildUnionType(BuildContext buildCtx, UnionTypeDefiniti
331331
builder.typeResolver(getTypeResolver(buildCtx, typeDefinition.getName()));
332332

333333
typeDefinition.getMemberTypes().forEach(mt -> {
334-
builder.possibleType(new GraphQLTypeReference(((TypeName) mt).getName()));
334+
GraphQLObjectType objectType = buildOutputType(buildCtx, mt);
335+
builder.possibleType(objectType);
335336
});
336337
return builder.build();
337338
}

‎src/test/groovy/graphql/schema/idl/SchemaCompilerTest.groovy‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import graphql.language.EnumTypeDefinition
44
import graphql.language.InterfaceTypeDefinition
55
import graphql.language.ObjectTypeDefinition
66
import graphql.language.ScalarTypeDefinition
7-
import graphql.language.TypeExtensionDefinition
87
import graphql.schema.idl.errors.SchemaProblem
98
import spock.lang.Specification
109

@@ -141,4 +140,32 @@ class SchemaCompilerTest extends Specification {
141140
thrown(SchemaProblem)
142141
}
143142

143+
def "schema with union"() {
144+
def schema = """
145+
146+
type Query {
147+
foobar: FooOrBar
148+
}
149+
150+
type Foo {
151+
name: String
152+
}
153+
154+
type Bar {
155+
other: String
156+
}
157+
158+
union FooOrBar = Foo | Bar
159+
160+
schema {
161+
query: Query
162+
}
163+
164+
"""
165+
when:
166+
TypeDefinitionRegistry typeRegistry = new SchemaCompiler().compile(schema)
167+
then:
168+
typeRegistry.types().size() == 4
169+
}
170+
144171
}

‎src/test/groovy/graphql/schema/idl/SchemaGeneratorTest.groovy‎

Lines changed: 118 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ class SchemaGeneratorTest extends Specification {
7070
assert authorField.arguments.get(0).type instanceof GraphQLNonNull
7171
assert unwrap(authorField.arguments.get(0).type).name == "Int"
7272

73-
7473
//type Post {
7574
// id: Int!
7675
// title: String
@@ -129,7 +128,6 @@ class SchemaGeneratorTest extends Specification {
129128
}
130129

131130

132-
133131
def "test simple schema generate"() {
134132

135133
def schemaSpec = """
@@ -185,6 +183,7 @@ class SchemaGeneratorTest extends Specification {
185183
commonSchemaAsserts(schema)
186184
}
187185

186+
188187
def "schema can come from multiple sources and be bound together"() {
189188
def schemaSpec1 = """
190189
type Author {
@@ -254,6 +253,123 @@ class SchemaGeneratorTest extends Specification {
254253
commonSchemaAsserts(schema)
255254

256255

256+
}
257+
258+
def "union type: union member used two times "() {
259+
def spec = """
260+
261+
type Query {
262+
foobar: FooOrBar
263+
foo: Foo
264+
}
265+
266+
type Foo {
267+
name: String
268+
}
269+
270+
type Bar {
271+
other: String
272+
}
273+
274+
union FooOrBar = Foo | Bar
275+
276+
schema {
277+
query: Query
278+
}
279+
280+
"""
281+
282+
def schema = generateSchema(spec, RuntimeWiring.newRuntimeWiring()
283+
.type("FooOrBar", buildResolver())
284+
.build())
285+
286+
287+
expect:
288+
289+
def foobar = schema.getQueryType().getFieldDefinition("foobar")
290+
foobar.type instanceof GraphQLUnionType
291+
def types = ((GraphQLUnionType) foobar.type).getTypes();
292+
types.size() == 2
293+
types[0].name == "Foo"
294+
types[1].name == "Bar"
295+
296+
}
297+
298+
def "union type: union members only used once"() {
299+
def spec = """
300+
301+
type Query {
302+
foobar: FooOrBar
303+
}
304+
305+
type Foo {
306+
name: String
307+
}
308+
309+
type Bar {
310+
other: String
311+
}
312+
313+
union FooOrBar = Foo | Bar
314+
315+
schema {
316+
query: Query
317+
}
318+
319+
"""
320+
321+
def schema = generateSchema(spec, RuntimeWiring.newRuntimeWiring()
322+
.type("FooOrBar", buildResolver())
323+
.build())
324+
325+
326+
expect:
327+
328+
def foobar = schema.getQueryType().getFieldDefinition("foobar")
329+
foobar.type instanceof GraphQLUnionType
330+
def types = ((GraphQLUnionType) foobar.type).getTypes();
331+
types.size() == 2
332+
types[0].name == "Foo"
333+
types[1].name == "Bar"
334+
335+
}
336+
337+
def "union type: union declared before members"() {
338+
def spec = """
339+
340+
union FooOrBar = Foo | Bar
341+
342+
type Foo {
343+
name: String
344+
}
345+
346+
type Bar {
347+
other: String
348+
}
349+
350+
type Query {
351+
foobar: FooOrBar
352+
}
353+
354+
schema {
355+
query: Query
356+
}
357+
358+
"""
359+
360+
def schema = generateSchema(spec, RuntimeWiring.newRuntimeWiring()
361+
.type("FooOrBar", buildResolver())
362+
.build())
363+
364+
365+
expect:
366+
367+
def foobar = schema.getQueryType().getFieldDefinition("foobar")
368+
foobar.type instanceof GraphQLUnionType
369+
def types = ((GraphQLUnionType) foobar.type).getTypes();
370+
types.size() == 2
371+
types[0].name == "Foo"
372+
types[1].name == "Bar"
257373

258374
}
259375

@@ -481,6 +597,5 @@ class SchemaGeneratorTest extends Specification {
481597
type.interfaces[0].name == "Character"
482598

483599

484-
485600
}
486601
}

0 commit comments

Comments
 (0)