Skip to content

Commit 0fdfd99

Browse files
oryan-blockclaude
andcommitted
Extract resolver parameter count into a util
Count JVM parameters minus the suspend Continuation instead of going through kotlin-reflect's parameter list, and read the last parameter from parameterTypes at that index. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 26db9e5 commit 0fdfd99

3 files changed

Lines changed: 18 additions & 31 deletions

File tree

‎src/main/kotlin/graphql/kickstart/tools/resolver/FieldResolverScanner.kt‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ import org.apache.commons.lang3.reflect.TypeUtils
1616
import org.reactivestreams.Publisher
1717
import org.slf4j.LoggerFactory
1818
import java.lang.reflect.*
19-
import kotlin.reflect.KParameter
2019
import kotlin.reflect.full.extensionReceiverParameter
21-
import kotlin.reflect.jvm.javaType
2220
import kotlin.reflect.jvm.kotlinFunction
2321

2422
/**
@@ -172,31 +170,14 @@ internal class FieldResolverScanner(val options: SchemaParserOptions) {
172170
!isExtensionFunction(method)
173171
}
174172

175-
val methodParameterCount = getMethodParameterCount(method)
176-
val methodLastParameter = getMethodLastParameter(method)
173+
val methodParameterCount = method.parameterCountWithoutContinuation()
174+
val methodLastParameter = method.parameterTypes.getOrNull(methodParameterCount - 1)
177175

178176
val correctParameterCount = methodParameterCount == requiredCount ||
179177
(methodParameterCount == (requiredCount + 1) && allowedLastArgumentTypes.contains(methodLastParameter))
180178
return correctParameterCount && appropriateFirstParameter
181179
}
182180

183-
private fun getMethodParameterCount(method: Method): Int {
184-
return try {
185-
method.kotlinFunction?.parameters?.count { it.kind != KParameter.Kind.INSTANCE } ?: method.parameterCount
186-
} catch (e: InternalError) {
187-
method.parameterCount
188-
}
189-
}
190-
191-
private fun getMethodLastParameter(method: Method): Type? {
192-
return try {
193-
method.kotlinFunction?.parameters?.lastOrNull { it.kind != KParameter.Kind.INSTANCE }?.type?.javaType
194-
?: method.parameterTypes.lastOrNull()
195-
} catch (e: InternalError) {
196-
method.parameterTypes.lastOrNull()
197-
}
198-
}
199-
200181
private fun isExtensionFunction(method: Method): Boolean {
201182
return try {
202183
method.kotlinFunction?.extensionReceiverParameter != null

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import graphql.kickstart.tools.SchemaParserOptions.GenericWrapper
77
import graphql.kickstart.tools.util.JavaType
88
import graphql.kickstart.tools.util.coroutineScope
99
import graphql.kickstart.tools.util.futureValueType
10+
import graphql.kickstart.tools.util.isSuspendFunction
11+
import graphql.kickstart.tools.util.parameterCountWithoutContinuation
1012
import graphql.kickstart.tools.util.typeArgument
1113
import graphql.kickstart.tools.util.unwrap
1214
import graphql.language.*
@@ -26,7 +28,6 @@ import java.lang.reflect.Method
2628
import java.util.*
2729
import java.util.function.Supplier
2830
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
29-
import kotlin.reflect.KParameter
3031
import kotlin.reflect.jvm.javaType
3132
import kotlin.reflect.jvm.kotlinFunction
3233

@@ -43,7 +44,7 @@ internal class MethodFieldResolver(
4344
private val log = LoggerFactory.getLogger(javaClass)
4445

4546
private val isSuspendFunction = method.isSuspendFunction()
46-
private val numberOfParameters = method.kotlinFunction?.parameters?.count { it.kind != KParameter.Kind.INSTANCE } ?: method.parameterCount
47+
private val numberOfParameters = method.parameterCountWithoutContinuation()
4748
private val hasAdditionalParameter = numberOfParameters == (field.inputValueDefinitions.size + getIndexOffset() + 1)
4849

4950
override fun createDataFetcher(): DataFetcher<*> {
@@ -283,14 +284,6 @@ private class CompareGenericWrappers {
283284
}
284285
}
285286

286-
private fun Method.isSuspendFunction(): Boolean {
287-
return try {
288-
this.kotlinFunction?.isSuspend == true
289-
} catch (e: InternalError) {
290-
false
291-
}
292-
}
293-
294287
private suspend inline fun invokeSuspend(target: Any, resolverMethod: Method, args: Array<Any?>): Any? {
295288
return suspendCoroutineUninterceptedOrReturn { continuation ->
296289
invoke(resolverMethod, target, args + continuation)

‎src/main/kotlin/graphql/kickstart/tools/util/Utils.kt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import java.lang.reflect.ParameterizedType
1212
import java.lang.reflect.Proxy
1313
import java.util.concurrent.CompletionStage
1414
import java.util.concurrent.Future
15+
import kotlin.reflect.jvm.kotlinFunction
1516

1617
/**
1718
* @author Andrew Potter
@@ -59,6 +60,18 @@ internal val Class<*>.declaredNonProxyMethods: List<JavaMethod>
5960
}
6061
}
6162

63+
internal fun JavaMethod.isSuspendFunction(): Boolean {
64+
return try {
65+
this.kotlinFunction?.isSuspend == true
66+
} catch (e: InternalError) {
67+
false
68+
}
69+
}
70+
71+
// the trailing Continuation of a suspend function isn't a resolver argument
72+
internal fun JavaMethod.parameterCountWithoutContinuation(): Int =
73+
if (isSuspendFunction()) parameterCount - 1 else parameterCount
74+
6275
internal fun getDocumentation(node: AbstractNode<*>, options: SchemaParserOptions): String? =
6376
when {
6477
node is AbstractDescribedNode<*> && node.description != null -> node.description?.content

0 commit comments

Comments
 (0)