Skip to content

Add the TrivialDataFetcher interface to MethodFieldResolver so that i… #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.coxautodev.graphql.tools
import com.coxautodev.graphql.tools.SchemaParserOptions.GenericWrapper
import com.esotericsoftware.reflectasm.MethodAccess
import com.fasterxml.jackson.core.type.TypeReference
import graphql.TrivialDataFetcher
import graphql.execution.batched.Batched
import graphql.language.FieldDefinition
import graphql.language.NonNullType
Expand Down Expand Up @@ -105,7 +106,12 @@ internal class MethodFieldResolver(field: FieldDefinition, search: FieldResolver
return if (batched) {
BatchedMethodFieldResolverDataFetcher(getSourceResolver(), this.method, args, options)
} else {
MethodFieldResolverDataFetcher(getSourceResolver(), this.method, args, options)
if (args.isEmpty() && isTrivialDataFetcher(this.method)) {
TrivialMethodFieldResolverDataFetcher(getSourceResolver(), this.method, args, options)
} else {
MethodFieldResolverDataFetcher(getSourceResolver(), this.method, args, options)
}

}
}

Expand Down Expand Up @@ -191,6 +197,10 @@ open class MethodFieldResolverDataFetcher(private val sourceResolver: SourceReso
}
}

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

}

private suspend inline fun MethodAccess.invokeSuspend(target: Any, methodIndex: Int, args: Array<Any?>): Any? {
return suspendCoroutineUninterceptedOrReturn { continuation ->
invoke(target, methodIndex, *args + continuation)
Expand Down
19 changes: 19 additions & 0 deletions src/main/kotlin/com/coxautodev/graphql/tools/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import graphql.language.NonNullType
import graphql.language.ObjectTypeDefinition
import graphql.language.ObjectTypeExtensionDefinition
import graphql.language.Type
import java.lang.reflect.Method
import java.lang.reflect.ParameterizedType

/**
Expand Down Expand Up @@ -33,3 +34,21 @@ internal fun JavaType.unwrap(): Class<out Any> =
} else {
this as Class<*>
}

/**
* Simple heuristic to check is a method is a trivial data fetcher.
*
* Requirements are:
* prefixed with get
* must have zero parameters
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should become more strict and only return true for 'primitives'.

E.g. any actual primitives and String, Integer, Double etc and the java date/time objects.

or If the field returns a scalar. that should work too

*/
internal fun isTrivialDataFetcher(method: Method): Boolean {
return (method.parameterCount == 0
&& (
method.name.startsWith("get")
|| isBooleanGetter(method)))
}

private fun isBooleanGetter(method: Method) = (method.name.startsWith("is")
&& (method.returnType == java.lang.Boolean::class.java)
|| method.returnType == Boolean::class.java)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.coxautodev.graphql.tools;

public class UtilsTestTrivialDataFetcherBean {

public boolean isBooleanPrimitive() {
return false;
}
}
40 changes: 40 additions & 0 deletions src/test/kotlin/com/coxautodev/graphql/tools/UtilsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.coxautodev.graphql.tools

import org.junit.Assert
import org.junit.Test

class UtilsTest {

@Suppress("unused")
class Bean {
fun getterValid(): String = ""

fun getterWithArgument(@Suppress("UNUSED_PARAMETER") arg1: String): String = ""

internal fun getterInternal(): String = ""

fun notAGetter(): String = ""

fun isString(): String = ""

@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
fun isJavaBoolean(): java.lang.Boolean = java.lang.Boolean(false)

fun isKotlinBoolean(): Boolean = false
}

@Test
fun `isTrivialDataFetcher`() {
val clazz = Bean::class.java

Assert.assertTrue(isTrivialDataFetcher(clazz.getMethod("getterValid")))
Assert.assertFalse(isTrivialDataFetcher(clazz.getMethod("getterWithArgument", String::class.java)))
Assert.assertFalse(isTrivialDataFetcher(clazz.getMethod("notAGetter")))

Assert.assertFalse(isTrivialDataFetcher(clazz.getMethod("isString")))
Assert.assertTrue(isTrivialDataFetcher(clazz.getMethod("isJavaBoolean")))
Assert.assertTrue(isTrivialDataFetcher(clazz.getMethod("isKotlinBoolean")))

Assert.assertTrue(isTrivialDataFetcher(UtilsTestTrivialDataFetcherBean::class.java.getMethod("isBooleanPrimitive")))
}
}