@@ -7,11 +7,15 @@ import graphql.schema.Coercing
7
7
import graphql.schema.GraphQLScalarType
8
8
import org.junit.Assert
9
9
import org.junit.Test
10
+ import java.lang.reflect.InvocationHandler
11
+ import java.lang.reflect.Method
12
+ import java.lang.reflect.Proxy
13
+ import java.util.*
10
14
11
15
class MethodFieldResolverTest {
12
16
13
17
@Test
14
- fun `should handle scalar types as method input argument` () {
18
+ fun shouldHandleScalarTypesAsMethodInputArgument () {
15
19
val schema = SchemaParser .newParser()
16
20
.schemaString("""
17
21
scalar CustomScalar
@@ -44,7 +48,7 @@ class MethodFieldResolverTest {
44
48
}
45
49
46
50
@Test
47
- fun `should handle lists of scalar types` () {
51
+ fun shouldHandleListsOfScalarTypes () {
48
52
val schema = SchemaParser .newParser()
49
53
.schemaString("""
50
54
scalar CustomScalar
@@ -76,6 +80,55 @@ class MethodFieldResolverTest {
76
80
Assert .assertEquals(6 , result.getData<Map <String , Any >>()[" test" ])
77
81
}
78
82
83
+ @Test
84
+ fun shouldHandleProxies () {
85
+ val invocationHandler = object : InvocationHandler {
86
+ override fun invoke (proxy : Any , method : Method , args : Array <out Any >): Any {
87
+ return when (method.name) {
88
+ " toString" -> " Proxy$" + System .identityHashCode(this )
89
+ " hashCode" -> System .identityHashCode(this )
90
+ " equals" -> Proxy .isProxyClass(args[0 ].javaClass)
91
+ " test" -> (args[0 ] as List <* >).map { (it as CustomScalar ).value.length }.sum()
92
+ else -> UnsupportedOperationException ()
93
+ }
94
+ }
95
+ }
96
+
97
+ val resolver = Proxy .newProxyInstance(
98
+ MethodFieldResolverTest ::class .java.classLoader,
99
+ arrayOf(Resolver ::class .java, GraphQLQueryResolver ::class .java),
100
+ invocationHandler
101
+ ) as GraphQLQueryResolver
102
+
103
+ val schema = SchemaParser .newParser()
104
+ .schemaString("""
105
+ scalar CustomScalar
106
+ type Query {
107
+ test(input: [CustomScalar]): Int
108
+ }
109
+ """ .trimIndent()
110
+ )
111
+ .scalars(customScalarType)
112
+ .resolvers(resolver)
113
+ .build()
114
+ .makeExecutableSchema()
115
+
116
+ val gql = GraphQL .newGraphQL(schema).build()
117
+
118
+ val result = gql
119
+ .execute(ExecutionInput .newExecutionInput()
120
+ .query("""
121
+ query Test(${" $" } input: [CustomScalar]) {
122
+ test(input: ${" $" } input)
123
+ }
124
+ """ .trimIndent())
125
+ .variables(mapOf (" input" to listOf (" Foo" , " Bar" )))
126
+ .context(Object ())
127
+ .root(Object ()))
128
+
129
+ Assert .assertEquals(6 , result.getData<Map <String , Any >>()[" test" ])
130
+ }
131
+
79
132
/* *
80
133
* Custom Scalar Class type that doesn't work with Jackson serialization/deserialization
81
134
*/
@@ -90,6 +143,10 @@ class MethodFieldResolverTest {
90
143
}
91
144
}
92
145
146
+ interface Resolver {
147
+ fun test (scalars : List <CustomScalar >): Int
148
+ }
149
+
93
150
private val customScalarType: GraphQLScalarType = GraphQLScalarType .newScalar()
94
151
.name(" CustomScalar" )
95
152
.description(" customScalar" )
0 commit comments