Skip to content

Commit 866cfdf

Browse files
authored
Merge pull request #593 from timward60/timward/get-type
DictionaryResolverType should use return GraphQLObjectType from Schema rather than cached map
2 parents 004c964 + 28194bd commit 866cfdf

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

src/main/kotlin/graphql/kickstart/tools/DictionaryTypeResolver.kt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import graphql.schema.TypeResolver
1313
* @author Andrew Potter
1414
*/
1515
internal abstract class DictionaryTypeResolver(
16-
private val dictionary: BiMap<JavaType, TypeDefinition<*>>,
17-
private val types: Map<String, GraphQLObjectType>
16+
private val dictionary: BiMap<JavaType, TypeDefinition<*>>
1817
) : TypeResolver {
1918
private fun <T> getTypeDefinition(clazz: Class<T>): TypeDefinition<*>? {
2019
return dictionary[clazz]
@@ -25,30 +24,26 @@ internal abstract class DictionaryTypeResolver(
2524
override fun getType(env: TypeResolutionEnvironment): GraphQLObjectType? {
2625
val clazz = env.getObject<Any>().javaClass
2726
val name = getTypeDefinition(clazz)?.name ?: clazz.simpleName
28-
return types[name] ?: throw TypeResolverError(getError(name))
27+
return env.schema.getObjectType(name) ?: throw TypeResolverError(getError(name))
2928
}
3029

3130
abstract fun getError(name: String): String
3231
}
3332

3433
internal class InterfaceTypeResolver(
3534
dictionary: BiMap<JavaType, TypeDefinition<*>>,
36-
private val thisInterface: GraphQLInterfaceType,
37-
types: List<GraphQLObjectType>
35+
private val thisInterface: GraphQLInterfaceType
3836
) : DictionaryTypeResolver(
39-
dictionary,
40-
types.filter { type -> type.interfaces.any { it.name == thisInterface.name } }.associateBy { it.name }
37+
dictionary
4138
) {
4239
override fun getError(name: String) = "Expected object type with name '$name' to implement interface '${thisInterface.name}', but it doesn't!"
4340
}
4441

4542
internal class UnionTypeResolver(
4643
dictionary: BiMap<JavaType, TypeDefinition<*>>,
47-
private val thisUnion: GraphQLUnionType,
48-
types: List<GraphQLObjectType>
44+
private val thisUnion: GraphQLUnionType
4945
) : DictionaryTypeResolver(
50-
dictionary,
51-
types.filter { type -> thisUnion.types.any { it.name == type.name } }.associateBy { it.name }
46+
dictionary
5247
) {
5348
override fun getError(name: String) = "Expected object type with name '$name' to exist for union '${thisUnion.name}', but it doesn't!"
5449
}

src/main/kotlin/graphql/kickstart/tools/SchemaParser.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ class SchemaParser internal constructor(
8585
val enums = enumDefinitions.map { createEnumObject(it) }
8686

8787
// Assign type resolver to interfaces now that we know all of the object types
88-
interfaces.forEach { codeRegistryBuilder.typeResolver(it, InterfaceTypeResolver(dictionary.inverse(), it, objects)) }
89-
unions.forEach { codeRegistryBuilder.typeResolver(it, UnionTypeResolver(dictionary.inverse(), it, objects)) }
88+
interfaces.forEach { codeRegistryBuilder.typeResolver(it, InterfaceTypeResolver(dictionary.inverse(), it)) }
89+
unions.forEach { codeRegistryBuilder.typeResolver(it, UnionTypeResolver(dictionary.inverse(), it)) }
9090

9191
// Find query type and mutation/subscription type (if mutation/subscription type exists)
9292
val queryName = rootInfo.getQueryName()

src/test/kotlin/graphql/kickstart/tools/EndToEndTest.kt

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package graphql.kickstart.tools
22

3+
import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder
34
import graphql.*
45
import graphql.execution.AsyncExecutionStrategy
5-
import graphql.schema.GraphQLEnumType
6-
import graphql.schema.GraphQLSchema
6+
import graphql.schema.*
7+
import graphql.util.TraversalControl
8+
import graphql.util.TraverserContext
79
import org.junit.Test
810
import org.reactivestreams.Publisher
911
import org.reactivestreams.Subscriber
@@ -670,4 +672,40 @@ class EndToEndTest {
670672
val exceptionWhileDataFetching = result.errors[0] as ExceptionWhileDataFetching
671673
assert(exceptionWhileDataFetching.exception is IllegalArgumentException)
672674
}
675+
676+
class Transformer : GraphQLTypeVisitorStub() {
677+
override fun visitGraphQLObjectType(node: GraphQLObjectType?, context: TraverserContext<GraphQLSchemaElement>?): TraversalControl {
678+
val newNode = node?.transform { builder -> builder.description(node.description + " [MODIFIED]") }
679+
return changeNode(context, newNode)
680+
}
681+
}
682+
683+
@Test
684+
fun `transformed schema should execute query`() {
685+
val transformedSchema = SchemaTransformer().transform(schema, Transformer())
686+
val transformedGql: GraphQL = GraphQL.newGraphQL(transformedSchema)
687+
.queryExecutionStrategy(AsyncExecutionStrategy())
688+
.build()
689+
690+
val data = assertNoGraphQlErrors(transformedGql) {
691+
"""
692+
{
693+
otherUnionItems {
694+
... on Item {
695+
itemId: id
696+
}
697+
... on ThirdItem {
698+
thirdItemId: id
699+
}
700+
}
701+
}
702+
"""
703+
}
704+
705+
assertEquals(data["otherUnionItems"], listOf(
706+
mapOf("itemId" to 0),
707+
mapOf("itemId" to 1),
708+
mapOf("thirdItemId" to 100)
709+
))
710+
}
673711
}

0 commit comments

Comments
 (0)