@@ -6,7 +6,6 @@ import graphql.kickstart.tools.*
6
6
import graphql.kickstart.tools.SchemaParserOptions.GenericWrapper
7
7
import graphql.kickstart.tools.util.JavaType
8
8
import graphql.kickstart.tools.util.coroutineScope
9
- import graphql.kickstart.tools.util.isTrivialDataFetcher
10
9
import graphql.kickstart.tools.util.unwrap
11
10
import graphql.language.*
12
11
import graphql.schema.DataFetcher
@@ -37,13 +36,9 @@ internal class MethodFieldResolver(
37
36
38
37
private val log = LoggerFactory .getLogger(javaClass)
39
38
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 )
47
42
48
43
override fun createDataFetcher (): DataFetcher <* > {
49
44
val args = mutableListOf<ArgumentPlaceholder >()
@@ -100,7 +95,7 @@ internal class MethodFieldResolver(
100
95
}
101
96
102
97
// Add DataFetchingEnvironment/Context argument
103
- if (this .additionalLastArgument ) {
98
+ if (this .hasAdditionalParameter ) {
104
99
when (this .method.parameterTypes.last()) {
105
100
null -> throw ResolverError (" Expected at least one argument but got none, this is most likely a bug with graphql-java-tools" )
106
101
options.contextClass -> args.add { environment ->
@@ -123,10 +118,12 @@ internal class MethodFieldResolver(
123
118
}
124
119
}
125
120
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)
128
124
} 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)
130
127
}
131
128
}
132
129
@@ -196,10 +193,9 @@ internal class MethodFieldResolverDataFetcher(
196
193
private val method : Method ,
197
194
private val args : List <ArgumentPlaceholder >,
198
195
private val options : SchemaParserOptions ,
196
+ private val isSuspendFunction : Boolean
199
197
) : DataFetcher<Any> {
200
198
201
- private val isSuspendFunction = method.isSuspendFunction()
202
-
203
199
override fun get (environment : DataFetchingEnvironment ): Any? {
204
200
val source = sourceResolver.resolve(environment, null )
205
201
val args = this .args.map { it(environment) }.toTypedArray()
@@ -223,27 +219,18 @@ internal class MethodFieldResolverDataFetcher(
223
219
}
224
220
225
221
/* *
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.
228
223
*/
229
224
internal class LightMethodFieldResolverDataFetcher (
230
225
private val sourceResolver : SourceResolver ,
231
226
private val method : Method ,
232
227
private val options : SchemaParserOptions ,
233
228
) : LightDataFetcher<Any?> {
234
229
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? {
238
231
val source = sourceResolver.resolve(null , sourceObject)
239
232
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)
247
234
}
248
235
249
236
override fun get (environment : DataFetchingEnvironment ): Any? {
0 commit comments