Skip to content

Commit 7b31d5e

Browse files
committed
Fix method order in FieldResolverScanner
1 parent 35962b5 commit 7b31d5e

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

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

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,6 @@ internal class FieldResolverScanner(val options: SchemaParserOptions) {
2929

3030
private val allowedLastArgumentTypes = listOfNotNull(DataFetchingEnvironment::class.java, options.contextClass)
3131

32-
fun getAllMethods(type: JavaType) =
33-
(type.unwrap().declaredNonProxyMethods.toList()
34-
+ ClassUtils.getAllInterfaces(type.unwrap()).flatMap { it.methods.toList() }
35-
+ ClassUtils.getAllSuperclasses(type.unwrap()).flatMap { it.methods.toList() })
36-
.asSequence()
37-
.filter { !it.isSynthetic }
38-
.filter { !Modifier.isPrivate(it.modifiers) }
39-
// discard any methods that are coming off the root of the class hierarchy
40-
// to avoid issues with duplicate method declarations
41-
.filter { it.declaringClass != Object::class.java }
42-
.toList()
43-
4432
fun findFieldResolver(field: FieldDefinition, resolverInfo: ResolverInfo): FieldResolver {
4533
val searches = resolverInfo.getFieldSearches()
4634

@@ -54,16 +42,6 @@ internal class FieldResolverScanner(val options: SchemaParserOptions) {
5442
return found.firstOrNull() ?: missingFieldResolver(field, searches, scanProperties)
5543
}
5644

57-
private fun missingFieldResolver(field: FieldDefinition, searches: List<Search>, scanProperties: Boolean): FieldResolver {
58-
return if (options.allowUnimplementedResolvers) {
59-
log.warn("Missing resolver for field: $field")
60-
61-
MissingFieldResolver(field, options)
62-
} else {
63-
throw FieldResolverError(getMissingFieldMessage(field, searches, scanProperties))
64-
}
65-
}
66-
6745
private fun findFieldResolver(field: FieldDefinition, search: Search, scanProperties: Boolean): FieldResolver? {
6846
val method = findResolverMethod(field, search)
6947
if (method != null) {
@@ -84,15 +62,21 @@ internal class FieldResolverScanner(val options: SchemaParserOptions) {
8462
return null
8563
}
8664

87-
private fun isBoolean(type: GraphQLLangType) = type.unwrap().let { it is TypeName && it.name == Scalars.GraphQLBoolean.name }
65+
private fun missingFieldResolver(field: FieldDefinition, searches: List<Search>, scanProperties: Boolean): FieldResolver {
66+
return if (options.allowUnimplementedResolvers) {
67+
log.warn("Missing resolver for field: $field")
68+
69+
MissingFieldResolver(field, options)
70+
} else {
71+
throw FieldResolverError(getMissingFieldMessage(field, searches, scanProperties))
72+
}
73+
}
8874

8975
private fun findResolverMethod(field: FieldDefinition, search: Search): java.lang.reflect.Method? {
9076
val methods = getAllMethods(search.type)
9177
val argumentCount = field.inputValueDefinitions.size + if (search.requiredFirstParameterType != null) 1 else 0
9278
val name = field.name
9379

94-
val isBoolean = isBoolean(field.type)
95-
9680
// Check for the following one by one:
9781
// 1. Method with exact field name
9882
// 2. Method that returns a boolean with "is" style getter
@@ -101,14 +85,28 @@ internal class FieldResolverScanner(val options: SchemaParserOptions) {
10185
return methods.find {
10286
it.name == name && verifyMethodArguments(it, argumentCount, search)
10387
} ?: methods.find {
104-
(isBoolean && it.name == "is${name.capitalize()}") && verifyMethodArguments(it, argumentCount, search)
88+
(isBoolean(field.type) && it.name == "is${name.capitalize()}") && verifyMethodArguments(it, argumentCount, search)
10589
} ?: methods.find {
10690
it.name == "get${name.capitalize()}" && verifyMethodArguments(it, argumentCount, search)
10791
} ?: methods.find {
10892
it.name == "getField${name.capitalize()}" && verifyMethodArguments(it, argumentCount, search)
10993
}
11094
}
11195

96+
private fun getAllMethods(type: JavaType) =
97+
(type.unwrap().declaredNonProxyMethods.toList()
98+
+ ClassUtils.getAllInterfaces(type.unwrap()).flatMap { it.methods.toList() }
99+
+ ClassUtils.getAllSuperclasses(type.unwrap()).flatMap { it.methods.toList() })
100+
.asSequence()
101+
.filter { !it.isSynthetic }
102+
.filter { !Modifier.isPrivate(it.modifiers) }
103+
// discard any methods that are coming off the root of the class hierarchy
104+
// to avoid issues with duplicate method declarations
105+
.filter { it.declaringClass != Object::class.java }
106+
.toList()
107+
108+
private fun isBoolean(type: GraphQLLangType) = type.unwrap().let { it is TypeName && it.name == Scalars.GraphQLBoolean.name }
109+
112110
private fun verifyMethodArguments(method: java.lang.reflect.Method, requiredCount: Int, search: Search): Boolean {
113111
val appropriateFirstParameter = if (search.requiredFirstParameterType != null) {
114112
method.genericParameterTypes.firstOrNull()?.let {

0 commit comments

Comments
 (0)