Skip to content

Resolves #383: Added a search in FieldResolverScanner for getters where the field name needs to be converted from snake to camel case. #464

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
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 @@ -4,10 +4,7 @@ import graphql.Scalars
import graphql.kickstart.tools.ResolverInfo
import graphql.kickstart.tools.RootResolverInfo
import graphql.kickstart.tools.SchemaParserOptions
import graphql.kickstart.tools.util.GraphQLLangType
import graphql.kickstart.tools.util.JavaType
import graphql.kickstart.tools.util.declaredNonProxyMethods
import graphql.kickstart.tools.util.unwrap
import graphql.kickstart.tools.util.*
import graphql.language.FieldDefinition
import graphql.language.TypeName
import graphql.schema.DataFetchingEnvironment
Expand Down Expand Up @@ -83,6 +80,7 @@ internal class FieldResolverScanner(val options: SchemaParserOptions) {
// 2. Method that returns a boolean with "is" style getter
// 3. Method with "get" style getter
// 4. Method with "getField" style getter
// 5. Method with "get" style getter with the field name converted from snake_case to camelCased. ex: key_ops -> getKeyOps()
return methods.find {
it.name == name && verifyMethodArguments(it, argumentCount, search)
} ?: methods.find {
Expand All @@ -91,6 +89,8 @@ internal class FieldResolverScanner(val options: SchemaParserOptions) {
it.name == "get${name.capitalize()}" && verifyMethodArguments(it, argumentCount, search)
} ?: methods.find {
it.name == "getField${name.capitalize()}" && verifyMethodArguments(it, argumentCount, search)
} ?: methods.find {
it.name == "get${name.snakeToCamelCase()}" && verifyMethodArguments(it, argumentCount, search)
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/graphql/kickstart/tools/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ private fun isBooleanGetter(method: Method) = (method.name.startsWith("is")
&& (method.returnType == java.lang.Boolean::class.java)
|| method.returnType == Boolean::class.java)

internal fun String.snakeToCamelCase(): String = split("_").joinToString(separator = "") { it.capitalize() }

Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ class FieldResolverScannerSpec extends Specification {
((MethodFieldResolver) meta).getMethod().getReturnType() == BoatInformation.class
}

def "scanner finds field resolver method using camelCase for snake_cased field_name"() {
setup:
def resolver = new RootResolverInfo([new CamelCaseQuery1()], options)

when:
def meta = scanner.findFieldResolver(new FieldDefinition("hull_type", new TypeName("HullType")), resolver)

then:
meta instanceof MethodFieldResolver
((MethodFieldResolver) meta).getMethod().getReturnType() == HullType.class
}

class RootQuery1 implements GraphQLQueryResolver {
def field1() {}
}
Expand All @@ -99,6 +111,12 @@ class FieldResolverScannerSpec extends Specification {
def field1() {}
}

class CamelCaseQuery1 implements GraphQLQueryResolver {
HullType getHullType(){}
}

class HullType {}

class ParentPropertyQuery {
private Integer version = 1
}
Expand Down