@@ -7,11 +7,15 @@ import graphql.schema.Coercing
77import graphql.schema.GraphQLScalarType
88import org.junit.Assert
99import 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.*
1014
1115class MethodFieldResolverTest {
1216
1317 @Test
14- fun `should handle scalar types as method input argument` () {
18+ fun shouldHandleScalarTypesAsMethodInputArgument () {
1519 val schema = SchemaParser .newParser()
1620 .schemaString("""
1721 scalar CustomScalar
@@ -44,7 +48,7 @@ class MethodFieldResolverTest {
4448 }
4549
4650 @Test
47- fun `should handle lists of scalar types` () {
51+ fun shouldHandleListsOfScalarTypes () {
4852 val schema = SchemaParser .newParser()
4953 .schemaString("""
5054 scalar CustomScalar
@@ -76,6 +80,55 @@ class MethodFieldResolverTest {
7680 Assert .assertEquals(6 , result.getData<Map <String , Any >>()[" test" ])
7781 }
7882
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+
79132 /* *
80133 * Custom Scalar Class type that doesn't work with Jackson serialization/deserialization
81134 */
@@ -90,6 +143,10 @@ class MethodFieldResolverTest {
90143 }
91144 }
92145
146+ interface Resolver {
147+ fun test (scalars : List <CustomScalar >): Int
148+ }
149+
93150 private val customScalarType: GraphQLScalarType = GraphQLScalarType .newScalar()
94151 .name(" CustomScalar" )
95152 .description(" customScalar" )
0 commit comments