@@ -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