Skip to content

Test case and proposed fix for #558 #559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions src/main/kotlin/graphql/kickstart/tools/DictionaryTypeResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,15 @@ internal abstract class DictionaryTypeResolver(
private val dictionary: BiMap<JavaType, TypeDefinition<*>>,
private val types: Map<String, GraphQLObjectType>
) : TypeResolver {
private fun <T> getTypeName(clazz: Class<T>): String? {
val name = dictionary[clazz]?.name

if (name == null && clazz.superclass != null) {
return getTypeName(clazz.superclass)
}

return name
private fun <T> getTypeDefinition(clazz: Class<T>): TypeDefinition<*>? {
return dictionary[clazz]
?: (if (clazz.superclass == null) null else getTypeDefinition(clazz.superclass))
?: clazz.interfaces.mapNotNull { getTypeDefinition(it) }.firstOrNull()
}

override fun getType(env: TypeResolutionEnvironment): GraphQLObjectType? {
val clazz = env.getObject<Any>().javaClass
val name = clazz.interfaces.fold(getTypeName(clazz), { name, interfaceClazz ->
name ?: getTypeName(interfaceClazz)
}) ?: clazz.simpleName

val name = getTypeDefinition(clazz)?.name ?: clazz.simpleName
return types[name] ?: throw TypeResolverError(getError(name))
}

Expand Down
32 changes: 32 additions & 0 deletions src/test/kotlin/graphql/kickstart/tools/EndToEndSpecHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fun createSchema() = SchemaParser.newParser()
.dictionary("ThirdItem", ThirdItem::class)
.dictionary("ComplexMapItem", ComplexMapItem::class)
.dictionary("NestedComplexMapItem", NestedComplexMapItem::class)
.dictionary("NoDogError", NoDogError::class)
.build()
.makeExecutableSchema()

Expand Down Expand Up @@ -83,6 +84,9 @@ type Query {
arrayItems: [Item!]!

throwsIllegalArgumentException: String

allDogs: [Dog!]!
findSuitableDog(preferredColor: String!, minimumFluffiness: Int!): FindDogResult!
}

type ExtendedType {
Expand Down Expand Up @@ -216,6 +220,18 @@ type Tag {
type ItemWithGenericProperties {
keys: [String!]!
}

type Dog {
name: String!
color: String!
fluffiness: Int!
}

type NoDogError {
msg: String
}

union FindDogResult = Dog | NoDogError
"""

val items = listOf(
Expand Down Expand Up @@ -314,6 +330,13 @@ class Query : GraphQLQueryResolver, ListListResolver<String>() {
fun throwsIllegalArgumentException(): String {
throw IllegalArgumentException("Expected")
}

fun allDogs(): List<Dog> = listOf(LabradorRetriever("Hershey", "chocolate", 42, 3.14159f))

fun findSuitableDog(preferredColor: String, minimumFluffiness: Int): Any =
allDogs()
.firstOrNull { it.color == preferredColor && it.fluffiness >= minimumFluffiness }
?: NoDogError("No $preferredColor-colored dog found that is sufficiently fluffy")
}

class UnusedRootResolver : GraphQLQueryResolver
Expand Down Expand Up @@ -410,6 +433,15 @@ class MockPart(private val name: String, private val content: String) : Part {
override fun delete() = throw IllegalArgumentException("Not supported")
}

interface Dog {
val name: String
val color: String
val fluffiness: Int
}
interface Retriever : Dog { val speed: Float }
class LabradorRetriever(override val name: String, override val color: String, override val fluffiness: Int, override val speed: Float) : Retriever
class NoDogError(val msg: String)

val customScalarId = GraphQLScalarType.newScalar()
.name("ID")
.description("Overrides built-in ID")
Expand Down
16 changes: 16 additions & 0 deletions src/test/kotlin/graphql/kickstart/tools/EndToEndTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ class EndToEndTest {
assertNotNull(data["itemByUUID"])
}

@Test
fun `generated schema should handle union types with deep hierarchy`() {
val data = assertNoGraphQlErrors(gql) {
"""
{
findSuitableDog(preferredColor: "chocolate", minimumFluffiness: 31) {
... on Dog { name }
... on NoDogError { msg }
}
}
"""
}

assertNotNull(data["findSuitableDog"])
}

@Test
fun `generated schema should handle non nullable scalar types`() {
val fileParts = listOf(MockPart("test.doc", "Hello"), MockPart("test.doc", "World"))
Expand Down