Skip to content

Commit 882a56b

Browse files
committed
Added unit test to reproduce and fix #101
1 parent 6c31fe8 commit 882a56b

File tree

3 files changed

+86
-8
lines changed

3 files changed

+86
-8
lines changed

src/main/kotlin/com/coxautodev/graphql/tools/MethodFieldResolver.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ package com.coxautodev.graphql.tools
33
import com.coxautodev.graphql.tools.SchemaParserOptions.GenericWrapper
44
import com.esotericsoftware.reflectasm.MethodAccess
55
import com.fasterxml.jackson.core.type.TypeReference
6-
import com.fasterxml.jackson.databind.ObjectMapper
7-
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module
8-
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
96
import graphql.execution.batched.Batched
107
import graphql.language.FieldDefinition
118
import graphql.language.NonNullType
@@ -108,7 +105,14 @@ internal class MethodFieldResolver(field: FieldDefinition, search: FieldResolver
108105
} + listOf(returnValueMatch)
109106
}
110107

111-
private fun getIndexOffset() = if (resolverInfo is NormalResolverInfo || resolverInfo is MultiResolverInfo) 1 else 0
108+
private fun getIndexOffset(): Int {
109+
return if (resolverInfo is DataClassTypeResolverInfo && !method.declaringClass.isAssignableFrom(resolverInfo.dataClassType)) {
110+
1
111+
} else {
112+
0
113+
}
114+
}
115+
112116
private fun getJavaMethodParameterIndex(index: Int) = index + getIndexOffset()
113117

114118
private fun getJavaMethodParameterType(index: Int): JavaType? {

src/main/kotlin/com/coxautodev/graphql/tools/ResolverInfo.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ internal abstract class ResolverInfo {
1010
options.proxyHandlers.find { it.canHandle(resolver) }?.getTargetClass(resolver) ?: resolver.javaClass
1111
}
1212

13-
internal class NormalResolverInfo(val resolver: GraphQLResolver<*>, private val options: SchemaParserOptions) : ResolverInfo() {
13+
internal interface DataClassTypeResolverInfo {
14+
val dataClassType: Class<*>
15+
}
16+
17+
internal class NormalResolverInfo(val resolver: GraphQLResolver<*>, private val options: SchemaParserOptions) : DataClassTypeResolverInfo, ResolverInfo() {
1418
val resolverType = getRealResolverClass(resolver, options)
15-
val dataClassType = findDataClass()
19+
override val dataClassType = findDataClass()
1620

1721
private fun findDataClass(): Class<*> {
1822
// Grab the parent interface with type GraphQLResolver from our resolver and get its first type argument.
@@ -42,8 +46,8 @@ internal class NormalResolverInfo(val resolver: GraphQLResolver<*>, private val
4246
}
4347
}
4448

45-
internal class MultiResolverInfo(val resolverInfoList: List<NormalResolverInfo>) : ResolverInfo() {
46-
private val dataClassType = findDataClass()
49+
internal class MultiResolverInfo(val resolverInfoList: List<NormalResolverInfo>) : DataClassTypeResolverInfo, ResolverInfo() {
50+
override val dataClassType = findDataClass()
4751

4852
/**
4953
* Checks if all `ResolverInfo` instances are related to the same data type
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.coxautodev.graphql.tools
2+
3+
import graphql.GraphQL
4+
import graphql.execution.AsyncExecutionStrategy
5+
import graphql.schema.GraphQLSchema
6+
import spock.lang.Shared
7+
import spock.lang.Specification
8+
9+
class ParameterizedGetterSpec extends Specification {
10+
11+
@Shared
12+
GraphQL gql
13+
14+
def setupSpec() {
15+
GraphQLSchema schema = SchemaParser.newParser().schemaString('''\
16+
type Query {
17+
human: Human
18+
}
19+
20+
type Human {
21+
bestFriends: [Character!]!
22+
allFriends(limit: Int!): [Character!]!
23+
}
24+
25+
type Character {
26+
name: String!
27+
}
28+
'''.stripIndent())
29+
.resolvers(new QueryResolver(), new HumanResolver())
30+
.build()
31+
.makeExecutableSchema()
32+
gql = GraphQL.newGraphQL(schema)
33+
.queryExecutionStrategy(new AsyncExecutionStrategy())
34+
.build()
35+
}
36+
37+
def "parameterized query is resolved on data type instead of on its resolver"() {
38+
when:
39+
def data = Utils.assertNoGraphQlErrors(gql, [limit: 10]) {
40+
'''
41+
query allFriends($limit: Int!) {
42+
human {
43+
allFriends(limit: $limit) {
44+
name
45+
}
46+
}
47+
}
48+
'''
49+
}
50+
51+
then:
52+
data.human
53+
}
54+
55+
class QueryResolver implements GraphQLQueryResolver {
56+
Human human() { new Human() }
57+
}
58+
59+
class Human {
60+
List<Character> allFriends(int limit) { Collections.emptyList() }
61+
}
62+
63+
class HumanResolver implements GraphQLResolver<Human> {
64+
List<Character> bestFriends(Human human) { Collections.emptyList() }
65+
}
66+
67+
class Character {
68+
String name
69+
}
70+
}

0 commit comments

Comments
 (0)