Skip to content

Commit 9a6bf5e

Browse files
committed
Reduce duplication in MethodFieldResolver.kt
1 parent 0ab9029 commit 9a6bf5e

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

src/main/kotlin/graphql/kickstart/tools/resolver/FieldResolver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal abstract class FieldResolver(
3535
} else {
3636
SourceResolver { environment, sourceObject ->
3737
val source = if (sourceObject != null) {
38-
// if source object is known environment is null as an optimization (LightDataFetcher)
38+
// if source object is known, environment is null as an optimization (LightDataFetcher)
3939
sourceObject
4040
} else {
4141
environment

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

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -191,48 +191,33 @@ internal class MethodFieldResolver(
191191
override fun toString() = "MethodFieldResolver{method=$method}"
192192
}
193193

194-
internal open class MethodFieldResolverDataFetcher(
194+
internal class MethodFieldResolverDataFetcher(
195195
private val sourceResolver: SourceResolver,
196-
method: Method,
196+
private val method: Method,
197197
private val args: List<ArgumentPlaceholder>,
198198
private val options: SchemaParserOptions,
199199
) : DataFetcher<Any> {
200200

201-
private val resolverMethod = method
202-
private val isSuspendFunction = try {
203-
method.kotlinFunction?.isSuspend == true
204-
} catch (e: InternalError) {
205-
false
206-
}
201+
private val isSuspendFunction = method.isSuspendFunction()
207202

208203
override fun get(environment: DataFetchingEnvironment): Any? {
209204
val source = sourceResolver.resolve(environment, null)
210205
val args = this.args.map { it(environment) }.toTypedArray()
211206

212207
return if (isSuspendFunction) {
213208
environment.coroutineScope().future(options.coroutineContextProvider.provide()) {
214-
invokeSuspend(source, resolverMethod, args)?.transformWithGenericWrapper(environment)
209+
invokeSuspend(source, method, args)?.transformWithGenericWrapper(options.genericWrappers, { environment })
215210
}
216211
} else {
217-
invoke(resolverMethod, source, args)?.transformWithGenericWrapper(environment)
212+
invoke(method, source, args)?.transformWithGenericWrapper(options.genericWrappers, { environment })
218213
}
219214
}
220215

221-
private fun Any.transformWithGenericWrapper(environment: DataFetchingEnvironment): Any? {
222-
return options.genericWrappers
223-
.asSequence()
224-
.filter { it.type.isInstance(this) }
225-
.sortedWith(CompareGenericWrappers)
226-
.firstOrNull()
227-
?.transformer?.invoke(this, environment) ?: this
228-
}
229-
230216
/**
231-
* Function that returns the object used to fetch the data.
232-
* It can be a DataFetcher or an entity.
217+
* Function that returns the object used to fetch the data. It can be a DataFetcher or an entity.
233218
*/
234219
@Suppress("unused")
235-
open fun getWrappedFetchingObject(environment: DataFetchingEnvironment): Any {
220+
fun getWrappedFetchingObject(environment: DataFetchingEnvironment): Any {
236221
return sourceResolver.resolve(environment, null)
237222
}
238223
}
@@ -247,38 +232,45 @@ internal class LightMethodFieldResolverDataFetcher(
247232
private val options: SchemaParserOptions,
248233
) : LightDataFetcher<Any?> {
249234

250-
private val resolverMethod = method
251-
private val isSuspendFunction = try {
252-
method.kotlinFunction?.isSuspend == true
253-
} catch (e: InternalError) {
254-
false
255-
}
235+
private val isSuspendFunction = method.isSuspendFunction()
256236

257237
override fun get(fieldDefinition: GraphQLFieldDefinition, sourceObject: Any, environmentSupplier: Supplier<DataFetchingEnvironment>): Any? {
258238
val source = sourceResolver.resolve(null, sourceObject)
239+
259240
return if (isSuspendFunction) {
260241
environmentSupplier.get().coroutineScope().future(options.coroutineContextProvider.provide()) {
261-
invokeSuspend(source, resolverMethod, emptyArray())?.transformWithGenericWrapper(environmentSupplier)
242+
invokeSuspend(source, method, emptyArray())?.transformWithGenericWrapper(options.genericWrappers, environmentSupplier)
262243
}
263244
} else {
264-
invoke(resolverMethod, source, emptyArray())?.transformWithGenericWrapper(environmentSupplier)
245+
invoke(method, source, emptyArray())?.transformWithGenericWrapper(options.genericWrappers, environmentSupplier)
265246
}
266247
}
267248

268249
override fun get(environment: DataFetchingEnvironment): Any? {
269250
return get(environment.fieldDefinition, sourceResolver.resolve(environment, null), { environment })
270251
}
271252

272-
private fun Any.transformWithGenericWrapper(environment: Supplier<DataFetchingEnvironment>): Any? {
273-
return options.genericWrappers
274-
.asSequence()
275-
.filter { it.type.isInstance(this) }
276-
.sortedWith(CompareGenericWrappers)
277-
.firstOrNull()
278-
?.transformer?.invoke(this, environment.get()) ?: this
253+
/**
254+
* Function that returns the object used to fetch the data. It can be a DataFetcher or an entity.
255+
*/
256+
@Suppress("unused")
257+
fun getWrappedFetchingObject(environment: DataFetchingEnvironment): Any {
258+
return sourceResolver.resolve(environment, null)
279259
}
280260
}
281261

262+
private fun Any.transformWithGenericWrapper(
263+
genericWrappers: List<GenericWrapper>,
264+
environmentSupplier: Supplier<DataFetchingEnvironment>
265+
): Any? {
266+
return genericWrappers
267+
.asSequence()
268+
.filter { it.type.isInstance(this) }
269+
.sortedWith(CompareGenericWrappers)
270+
.firstOrNull()
271+
?.transformer?.invoke(this, environmentSupplier.get()) ?: this
272+
}
273+
282274
private class CompareGenericWrappers {
283275
companion object : Comparator<GenericWrapper> {
284276
override fun compare(w1: GenericWrapper, w2: GenericWrapper): Int = when {
@@ -288,6 +280,14 @@ private class CompareGenericWrappers {
288280
}
289281
}
290282

283+
private fun Method.isSuspendFunction(): Boolean {
284+
return try {
285+
this.kotlinFunction?.isSuspend == true
286+
} catch (e: InternalError) {
287+
false
288+
}
289+
}
290+
291291
private suspend inline fun invokeSuspend(target: Any, resolverMethod: Method, args: Array<Any?>): Any? {
292292
return suspendCoroutineUninterceptedOrReturn { continuation ->
293293
invoke(resolverMethod, target, args + continuation)

0 commit comments

Comments
 (0)