@@ -3,6 +3,7 @@ package graphql.kickstart.tools
33import graphql.ExecutionResult
44import graphql.GraphQL
55import graphql.kickstart.tools.resolver.FieldResolverError
6+ import graphql.language.ObjectTypeDefinition
67import graphql.parser.InvalidSyntaxException
78import graphql.schema.*
89import graphql.schema.idl.SchemaDirectiveWiring
@@ -316,6 +317,84 @@ class SchemaParserTest {
316317 assertEquals(error.message, " Union 'Result' contains itself through Result -> Result at line 5, column 23." )
317318 }
318319
320+ @Test
321+ fun `parser should throw descriptive exception when a union extension lists a union that contains it` () {
322+ val error = assertThrows(SchemaError ::class .java) {
323+ unionParser(
324+ """
325+ |union Result = Book | Media
326+ |union Media = Movie
327+ |extend union Media = Result
328+ """ .trimMargin())
329+ }
330+
331+ assertEquals(error.message, " Union 'Result' contains itself through Result -> Media -> Result at line 7, column 22." )
332+ }
333+
334+ @Test
335+ fun `parser lists a member once when nested unions reach it more than once` () {
336+ val schema = unionParser(
337+ """
338+ |union Result = Left | Right
339+ |union Left = Book
340+ |extend union Left = Shared
341+ |union Right = Shared
342+ |union Shared = Movie
343+ """ .trimMargin())
344+
345+ assertEquals(listOf (" Result" , " Left" , " Right" ).associateWith { name -> (schema.getType(name) as GraphQLUnionType ).types.map { it.name } }, mapOf (
346+ " Result" to listOf (" Book" , " Movie" ),
347+ " Left" to listOf (" Book" , " Movie" ),
348+ " Right" to listOf (" Movie" )
349+ ))
350+ }
351+
352+ @Test
353+ fun `parser builds a type defined twice from its last definition, or from its first when only its interface reaches it` () {
354+ val (throughField, fieldWarnings) = withWarnings {
355+ SchemaParser .newParser()
356+ .schemaString(
357+ """
358+ type Query { item: Item }
359+ type Item { id: ID }
360+ type Item { id: ID name: String }
361+ """ )
362+ .resolvers(TwiceDefinedQuery ())
363+ .build()
364+ }
365+
366+ assertEquals(fieldWarnings, listOf ())
367+ assertEquals(throughField.getUnusedDefinitions(), setOf ())
368+ assertEquals((throughField.makeExecutableSchema().getType(" Item" ) as GraphQLObjectType ).fieldDefinitions.map { it.name }, listOf (" id" , " name" ))
369+
370+ val (throughInterface, interfaceWarnings) = withWarnings {
371+ SchemaParser .newParser()
372+ .schemaString(
373+ """
374+ type Query { node: Node }
375+ interface Node { id: ID }
376+ type Item implements Node { id: ID }
377+ type Item implements Node { id: ID name: String }
378+ """ )
379+ .resolvers(TwiceDefinedQuery ())
380+ .dictionary(" Item" , TwiceDefined ::class )
381+ .build()
382+ }
383+
384+ assertEquals(interfaceWarnings, listOf (" Schema type was defined but can never be accessed, and can be safely deleted: Item" ))
385+ assertEquals(throughInterface.getUnusedDefinitions().map { it.name to (it as ObjectTypeDefinition ).fieldDefinitions.map { field -> field.name } }, listOf (" Item" to listOf (" id" , " name" )))
386+ assertEquals((throughInterface.makeExecutableSchema().getType(" Item" ) as GraphQLObjectType ).fieldDefinitions.map { it.name }, listOf (" id" ))
387+ }
388+
389+ class TwiceDefinedQuery : GraphQLQueryResolver {
390+ fun item (): TwiceDefined = TwiceDefined (" 1" , " one" )
391+ fun node (): TwiceDefinedNode = TwiceDefined (" 1" , " one" )
392+ }
393+
394+ interface TwiceDefinedNode
395+
396+ class TwiceDefined (val id : String , val name : String? ) : TwiceDefinedNode
397+
319398 private fun unionParser (unions : String ) = SchemaParser .newParser()
320399 .schemaString(
321400 """
0 commit comments