@@ -6,7 +6,6 @@ import graphql.kickstart.tools.*
66import graphql.kickstart.tools.SchemaParserOptions.GenericWrapper
77import graphql.kickstart.tools.util.JavaType
88import graphql.kickstart.tools.util.coroutineScope
9- import graphql.kickstart.tools.util.isTrivialDataFetcher
109import graphql.kickstart.tools.util.unwrap
1110import graphql.language.*
1211import graphql.schema.DataFetcher
@@ -37,13 +36,9 @@ internal class MethodFieldResolver(
3736
3837 private val log = LoggerFactory .getLogger(javaClass)
3938
40- private val additionalLastArgument =
41- try {
42- (method.kotlinFunction?.valueParameters?.size
43- ? : method.parameterCount) == (field.inputValueDefinitions.size + getIndexOffset() + 1 )
44- } catch (e: InternalError ) {
45- method.parameterCount == (field.inputValueDefinitions.size + getIndexOffset() + 1 )
46- }
39+ private val isSuspendFunction = method.isSuspendFunction()
40+ private val numberOfParameters = method.kotlinFunction?.valueParameters?.size ? : method.parameterCount
41+ private val hasAdditionalParameter = numberOfParameters == (field.inputValueDefinitions.size + getIndexOffset() + 1 )
4742
4843 override fun createDataFetcher (): DataFetcher <* > {
4944 val args = mutableListOf<ArgumentPlaceholder >()
@@ -100,7 +95,7 @@ internal class MethodFieldResolver(
10095 }
10196
10297 // Add DataFetchingEnvironment/Context argument
103- if (this .additionalLastArgument ) {
98+ if (this .hasAdditionalParameter ) {
10499 when (this .method.parameterTypes.last()) {
105100 null -> throw ResolverError (" Expected at least one argument but got none, this is most likely a bug with graphql-java-tools" )
106101 options.contextClass -> args.add { environment ->
@@ -123,10 +118,12 @@ internal class MethodFieldResolver(
123118 }
124119 }
125120
126- return if (args.isEmpty() && isTrivialDataFetcher(this .method)) {
127- LightMethodFieldResolverDataFetcher (createSourceResolver(), this .method, options)
121+ return if (numberOfParameters > 0 || isSuspendFunction) {
122+ // requires arguments and environment or is a suspend function
123+ MethodFieldResolverDataFetcher (createSourceResolver(), this .method, args, options, isSuspendFunction)
128124 } else {
129- MethodFieldResolverDataFetcher (createSourceResolver(), this .method, args, options)
125+ // if there are no parameters an optimized version of the data fetcher can be used
126+ LightMethodFieldResolverDataFetcher (createSourceResolver(), this .method, options)
130127 }
131128 }
132129
@@ -196,10 +193,9 @@ internal class MethodFieldResolverDataFetcher(
196193 private val method : Method ,
197194 private val args : List <ArgumentPlaceholder >,
198195 private val options : SchemaParserOptions ,
196+ private val isSuspendFunction : Boolean
199197) : DataFetcher<Any> {
200198
201- private val isSuspendFunction = method.isSuspendFunction()
202-
203199 override fun get (environment : DataFetchingEnvironment ): Any? {
204200 val source = sourceResolver.resolve(environment, null )
205201 val args = this .args.map { it(environment) }.toTypedArray()
@@ -223,27 +219,18 @@ internal class MethodFieldResolverDataFetcher(
223219}
224220
225221/* *
226- * Similar to [MethodFieldResolverDataFetcher] but for light data fetchers which do not require the environment to be supplied unless suspend functions or
227- * generic wrappers are used.
222+ * Similar to [MethodFieldResolverDataFetcher] but for light data fetchers which do not require the environment to be supplied unless generic wrappers are used.
228223 */
229224internal class LightMethodFieldResolverDataFetcher (
230225 private val sourceResolver : SourceResolver ,
231226 private val method : Method ,
232227 private val options : SchemaParserOptions ,
233228) : LightDataFetcher<Any?> {
234229
235- private val isSuspendFunction = method.isSuspendFunction()
236-
237- override fun get (fieldDefinition : GraphQLFieldDefinition , sourceObject : Any , environmentSupplier : Supplier <DataFetchingEnvironment >): Any? {
230+ override fun get (fieldDefinition : GraphQLFieldDefinition , sourceObject : Any? , environmentSupplier : Supplier <DataFetchingEnvironment >): Any? {
238231 val source = sourceResolver.resolve(null , sourceObject)
239232
240- return if (isSuspendFunction) {
241- environmentSupplier.get().coroutineScope().future(options.coroutineContextProvider.provide()) {
242- invokeSuspend(source, method, emptyArray())?.transformWithGenericWrapper(options.genericWrappers, environmentSupplier)
243- }
244- } else {
245- invoke(method, source, emptyArray())?.transformWithGenericWrapper(options.genericWrappers, environmentSupplier)
246- }
233+ return invoke(method, source, emptyArray())?.transformWithGenericWrapper(options.genericWrappers, environmentSupplier)
247234 }
248235
249236 override fun get (environment : DataFetchingEnvironment ): Any? {
0 commit comments