Skip to content

Commit ebebf2c

Browse files
authored
Merge pull request #464 from plaurina/bugfix/383_find_camelCaseGetter_from_snake_case_field
Resolves #383: Added a search in FieldResolverScanner for getters where the field name needs to be converted from snake to camel case.
2 parents 69e2f3d + 92ae337 commit ebebf2c

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import graphql.Scalars
44
import graphql.kickstart.tools.ResolverInfo
55
import graphql.kickstart.tools.RootResolverInfo
66
import graphql.kickstart.tools.SchemaParserOptions
7-
import graphql.kickstart.tools.util.GraphQLLangType
8-
import graphql.kickstart.tools.util.JavaType
9-
import graphql.kickstart.tools.util.declaredNonProxyMethods
10-
import graphql.kickstart.tools.util.unwrap
7+
import graphql.kickstart.tools.util.*
118
import graphql.language.FieldDefinition
129
import graphql.language.TypeName
1310
import graphql.schema.DataFetchingEnvironment
@@ -83,6 +80,7 @@ internal class FieldResolverScanner(val options: SchemaParserOptions) {
8380
// 2. Method that returns a boolean with "is" style getter
8481
// 3. Method with "get" style getter
8582
// 4. Method with "getField" style getter
83+
// 5. Method with "get" style getter with the field name converted from snake_case to camelCased. ex: key_ops -> getKeyOps()
8684
return methods.find {
8785
it.name == name && verifyMethodArguments(it, argumentCount, search)
8886
} ?: methods.find {
@@ -91,6 +89,8 @@ internal class FieldResolverScanner(val options: SchemaParserOptions) {
9189
it.name == "get${name.capitalize()}" && verifyMethodArguments(it, argumentCount, search)
9290
} ?: methods.find {
9391
it.name == "getField${name.capitalize()}" && verifyMethodArguments(it, argumentCount, search)
92+
} ?: methods.find {
93+
it.name == "get${name.snakeToCamelCase()}" && verifyMethodArguments(it, argumentCount, search)
9494
}
9595
}
9696

src/main/kotlin/graphql/kickstart/tools/util/Utils.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,5 @@ private fun isBooleanGetter(method: Method) = (method.name.startsWith("is")
7272
&& (method.returnType == java.lang.Boolean::class.java)
7373
|| method.returnType == Boolean::class.java)
7474

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

src/test/groovy/graphql/kickstart/tools/FieldResolverScannerSpec.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ class FieldResolverScannerSpec extends Specification {
8787
((MethodFieldResolver) meta).getMethod().getReturnType() == BoatInformation.class
8888
}
8989

90+
def "scanner finds field resolver method using camelCase for snake_cased field_name"() {
91+
setup:
92+
def resolver = new RootResolverInfo([new CamelCaseQuery1()], options)
93+
94+
when:
95+
def meta = scanner.findFieldResolver(new FieldDefinition("hull_type", new TypeName("HullType")), resolver)
96+
97+
then:
98+
meta instanceof MethodFieldResolver
99+
((MethodFieldResolver) meta).getMethod().getReturnType() == HullType.class
100+
}
101+
90102
class RootQuery1 implements GraphQLQueryResolver {
91103
def field1() {}
92104
}
@@ -99,6 +111,12 @@ class FieldResolverScannerSpec extends Specification {
99111
def field1() {}
100112
}
101113

114+
class CamelCaseQuery1 implements GraphQLQueryResolver {
115+
HullType getHullType(){}
116+
}
117+
118+
class HullType {}
119+
102120
class ParentPropertyQuery {
103121
private Integer version = 1
104122
}

0 commit comments

Comments
 (0)