Skip to content

Commit d754faa

Browse files
authored
Merge pull request #948 from graphql-java-kickstart/agent-friendly-p7-extension-tests
Pin how type extensions behave before merging definitions
2 parents af9ba4a + 7d3341a commit d754faa

4 files changed

Lines changed: 480 additions & 10 deletions

File tree

‎src/test/kotlin/graphql/kickstart/tools/SchemaClassScannerInterfaceFieldTest.kt‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,54 @@ class SchemaClassScannerInterfaceFieldTest {
304304
assert(schema.getType("Entry") is GraphQLInterfaceType)
305305
}
306306

307+
@Test
308+
fun `scanner handles interfaces used only by fields of interface extensions`() {
309+
val schema = SchemaParser.newParser()
310+
.schemaString(
311+
"""
312+
type Query {
313+
container: Container
314+
}
315+
316+
interface Container {
317+
id: ID
318+
}
319+
320+
extend interface Container {
321+
entry: Entry
322+
}
323+
324+
interface Entry {
325+
id: ID!
326+
}
327+
328+
interface Draft implements Entry {
329+
id: ID!
330+
}
331+
332+
type Box implements Container {
333+
id: ID
334+
entry: Draft
335+
}
336+
""")
337+
.resolvers(InterfaceOnlyInterface())
338+
.dictionary(InterfaceOnlyInterface.Box::class)
339+
.build()
340+
.makeExecutableSchema()
341+
.assertEveryFieldResolves()
342+
343+
assertEquals((schema.getType("Container") as GraphQLInterfaceType).fieldDefinitions.map { it.name }, listOf("id", "entry"))
344+
assert(schema.getType("Entry") is GraphQLInterfaceType)
345+
}
346+
307347
class InterfaceOnlyInterface : GraphQLQueryResolver {
308348
fun container(): Container? = null
309349

310350
interface Container
311351

312352
interface Draft
313353

314-
class Box(val entry: Draft?) : Container
354+
class Box(val entry: Draft?, val id: String? = null) : Container
315355
}
316356

317357
@Test

‎src/test/kotlin/graphql/kickstart/tools/SchemaParserDictionaryTest.kt‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,28 @@ class SchemaParserDictionaryTest {
196196
assertEquals(error.message, "Class in supplied dictionary '${ThirdItem::class.java.name}' specified type names 'A', 'B', " +
197197
"but only input object types can share a class!")
198198
}
199+
200+
@Test
201+
fun `dictionary names need a type definition, which an object or input extension alone isn't`() {
202+
fun parser(extension: String) = SchemaParser.newParser()
203+
.schemaString("type Query { id: ID }\n$extension")
204+
.resolvers(object : GraphQLQueryResolver {
205+
fun id(): String = "1"
206+
})
207+
.dictionary("Item", ThirdItem::class)
208+
.build()
209+
210+
listOf("extend type Item { id: Int }", "extend input Item { id: Int }").forEach { extension ->
211+
val error = assertThrows(SchemaClassScannerError::class.java) { parser(extension) }
212+
213+
assertEquals(error.message, "Class in supplied dictionary '${ThirdItem::class.java.name}' specified type name 'Item', " +
214+
"but a type definition with that name was not found!")
215+
}
216+
217+
val (_, warnings) = withWarnings { parser("extend union Item = Query") }
218+
assertEquals(warnings, listOf(
219+
"Dictionary mapping was provided but never used, and can be safely deleted: \"Item\" -> ${ThirdItem::class.java.name}",
220+
"Schema type was defined but can never be accessed, and can be safely deleted: Item"
221+
))
222+
}
199223
}

‎src/test/kotlin/graphql/kickstart/tools/SchemaParserTest.kt‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package graphql.kickstart.tools
33
import graphql.ExecutionResult
44
import graphql.GraphQL
55
import graphql.kickstart.tools.resolver.FieldResolverError
6+
import graphql.language.ObjectTypeDefinition
67
import graphql.parser.InvalidSyntaxException
78
import graphql.schema.*
89
import 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

Comments
 (0)