Skip to content

Commit 950e291

Browse files
committed
Remove reflectasm. Fixes #390
1 parent 2dacd8e commit 950e291

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@
104104
<artifactId>jackson-datatype-jdk8</artifactId>
105105
<version>${jackson.version}</version>
106106
</dependency>
107-
<dependency>
108-
<groupId>com.esotericsoftware</groupId>
109-
<artifactId>reflectasm</artifactId>
110-
<version>1.11.9</version>
111-
</dependency>
112107
<dependency>
113108
<groupId>org.apache.commons</groupId>
114109
<artifactId>commons-lang3</artifactId>

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package graphql.kickstart.tools
22

3-
import com.esotericsoftware.reflectasm.MethodAccess
43
import com.fasterxml.jackson.core.type.TypeReference
54
import graphql.TrivialDataFetcher
65
import graphql.execution.batched.Batched
@@ -182,9 +181,7 @@ internal class MethodFieldResolver(field: FieldDefinition, search: FieldResolver
182181

183182
open class MethodFieldResolverDataFetcher(private val sourceResolver: SourceResolver, method: Method, private val args: List<ArgumentPlaceholder>, private val options: SchemaParserOptions) : DataFetcher<Any> {
184183

185-
// Convert to reflactasm reflection
186-
private val methodAccess = MethodAccess.get(method.declaringClass)!!
187-
private val methodIndex = methodAccess.getIndex(method.name, *method.parameterTypes)
184+
private val resolverMethod = method
188185
private val isSuspendFunction = try {
189186
method.kotlinFunction?.isSuspend == true
190187
} catch (e: InternalError) {
@@ -206,10 +203,10 @@ open class MethodFieldResolverDataFetcher(private val sourceResolver: SourceReso
206203

207204
return if (isSuspendFunction) {
208205
environment.coroutineScope().future(options.coroutineContextProvider.provide()) {
209-
methodAccess.invokeSuspend(source, methodIndex, args)?.transformWithGenericWrapper(environment)
206+
invokeSuspend(source, resolverMethod, args)?.transformWithGenericWrapper(environment)
210207
}
211208
} else {
212-
methodAccess.invoke(source, methodIndex, *args)?.transformWithGenericWrapper(environment)
209+
invoke(resolverMethod, source, args)?.transformWithGenericWrapper(environment)
213210
}
214211
}
215212

@@ -236,9 +233,26 @@ open class TrivialMethodFieldResolverDataFetcher(sourceResolver: SourceResolver,
236233

237234
}
238235

239-
private suspend inline fun MethodAccess.invokeSuspend(target: Any, methodIndex: Int, args: Array<Any?>): Any? {
236+
private suspend inline fun invokeSuspend(target: Any, resolverMethod: Method, args: Array<Any?>): Any? {
240237
return suspendCoroutineUninterceptedOrReturn { continuation ->
241-
invoke(target, methodIndex, *args + continuation)
238+
invoke(resolverMethod, target, args + continuation)
239+
}
240+
}
241+
242+
@Suppress("NOTHING_TO_INLINE")
243+
private inline fun invoke(method: Method, instance:Any, args: Array<Any?>): Any? {
244+
try {
245+
return method.invoke(instance, *args)
246+
} catch (invocationException: java.lang.reflect.InvocationTargetException) {
247+
val e = invocationException.cause
248+
if (e is RuntimeException) {
249+
throw e
250+
}
251+
if (e is Error) {
252+
throw e
253+
}
254+
255+
throw java.lang.reflect.UndeclaredThrowableException(e);
242256
}
243257
}
244258

src/test/groovy/graphql/kickstart/tools/EndToEndSpec.groovy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.kickstart.tools
22

3+
import graphql.ExecutionInput
34
import graphql.ExecutionResult
45
import graphql.GraphQL
56
import graphql.execution.AsyncExecutionStrategy
@@ -664,4 +665,17 @@ class EndToEndSpec extends Specification {
664665
data.arrayItems.collect { it.name } == ['item1', 'item2']
665666
}
666667
668+
def "generated schema should re-throw original runtime exception when executing a resolver method"() {
669+
when:
670+
671+
gql.execute(ExecutionInput.newExecutionInput().query('''
672+
{
673+
throwsIllegalArgumentException
674+
}
675+
'''
676+
))
677+
678+
then:
679+
IllegalArgumentException
680+
}
667681
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ type Query {
8181
coroutineItems: [Item!]!
8282
8383
arrayItems: [Item!]!
84+
85+
throwsIllegalArgumentException: String
8486
}
8587
8688
type ExtendedType {
@@ -296,6 +298,10 @@ class Query : GraphQLQueryResolver, ListListResolver<String>() {
296298
suspend fun coroutineItems(): List<Item> = CompletableDeferred(items).await()
297299

298300
fun arrayItems() = items.toTypedArray()
301+
302+
fun throwsIllegalArgumentException(): String {
303+
throw IllegalArgumentException("Expected")
304+
}
299305
}
300306

301307
class UnusedRootResolver : GraphQLQueryResolver

0 commit comments

Comments
 (0)