-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathFieldResolverScanner.kt
More file actions
247 lines (206 loc) · 11 KB
/
FieldResolverScanner.kt
File metadata and controls
247 lines (206 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package graphql.kickstart.tools.resolver
import graphql.GraphQLContext
import graphql.Scalars
import graphql.kickstart.tools.GraphQLSubscriptionResolver
import graphql.kickstart.tools.ResolverInfo
import graphql.kickstart.tools.RootResolverInfo
import graphql.kickstart.tools.SchemaParserOptions
import graphql.kickstart.tools.util.*
import graphql.language.FieldDefinition
import graphql.language.TypeName
import graphql.schema.DataFetchingEnvironment
import kotlinx.coroutines.channels.ReceiveChannel
import org.apache.commons.lang3.ClassUtils
import org.apache.commons.lang3.reflect.FieldUtils
import org.reactivestreams.Publisher
import org.slf4j.LoggerFactory
import java.lang.reflect.*
import java.util.concurrent.CompletableFuture
import kotlin.reflect.full.valueParameters
import kotlin.reflect.jvm.javaType
import kotlin.reflect.jvm.kotlinFunction
/**
* @author Andrew Potter
*/
internal class FieldResolverScanner(val options: SchemaParserOptions) {
private val log = LoggerFactory.getLogger(javaClass)
private val allowedLastArgumentTypes = listOfNotNull(DataFetchingEnvironment::class.java, GraphQLContext::class.java, options.contextClass)
fun findFieldResolver(field: FieldDefinition, resolverInfo: ResolverInfo): FieldResolver {
val searches = resolverInfo.getFieldSearches()
val scanProperties = field.inputValueDefinitions.isEmpty()
val found = searches.mapNotNull { search -> findFieldResolver(field, search, scanProperties) }
if (resolverInfo is RootResolverInfo && found.size > 1) {
throw FieldResolverError("Found more than one matching resolver for field '$field': $found")
}
return found.firstOrNull() ?: missingFieldResolver(field, searches, scanProperties)
}
private fun findFieldResolver(field: FieldDefinition, search: Search, scanProperties: Boolean): FieldResolver? {
val method = findResolverMethod(field, search)
if (method != null) {
return MethodFieldResolver(field, search, options, method.apply(trySetAccessible(field, search.type)))
}
if (scanProperties) {
val property = findResolverProperty(field, search)
if (property != null) {
return PropertyFieldResolver(field, search, options, property.apply(trySetAccessible(field, search.type)))
}
}
if (java.util.Map::class.java.isAssignableFrom(search.type.unwrap())) {
return MapFieldResolver(field, search, options, search.type.unwrap())
}
return null
}
private fun trySetAccessible(field: FieldDefinition, type: JavaType): AccessibleObject.() -> Unit = {
try {
isAccessible = true
} catch (e: RuntimeException) {
log.warn("Unable to make field ${type.unwrap().name}#${field.name} accessible. " +
"Be sure to provide a resolver or open the enclosing module if possible.")
}
}
private fun missingFieldResolver(field: FieldDefinition, searches: List<Search>, scanProperties: Boolean): FieldResolver {
return if (options.allowUnimplementedResolvers
|| options.missingResolverDataFetcher != null
|| options.missingResolverDataFetcherProvider != null) {
if (options.allowUnimplementedResolvers) {
log.warn("Missing resolver for field: $field")
}
MissingFieldResolver(field, options)
} else {
throw FieldResolverError(getMissingFieldMessage(field, searches, scanProperties))
}
}
private fun findResolverMethod(field: FieldDefinition, search: Search): Method? {
val methods = getAllMethods(search)
val argumentCount = field.inputValueDefinitions.size + if (search.requiredFirstParameterType != null) 1 else 0
val name = field.name
// Check for the following one by one:
// 1. Method with exact field name
// 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 {
(isBoolean(field.type) && it.name == "is${name.replaceFirstChar(Char::titlecase)}") && verifyMethodArguments(it, argumentCount, search)
} ?: methods.find {
it.name == "get${name.replaceFirstChar(Char::titlecase)}" && verifyMethodArguments(it, argumentCount, search)
} ?: methods.find {
it.name == "getField${name.replaceFirstChar(Char::titlecase)}" && verifyMethodArguments(it, argumentCount, search)
} ?: methods.find {
it.name == "get${name.snakeToCamelCase()}" && verifyMethodArguments(it, argumentCount, search)
}
}
private fun getAllMethods(search: Search): List<Method> {
val type = search.type.unwrap()
val declaredMethods = type.declaredNonProxyMethods
val superClassesMethods = ClassUtils.getAllSuperclasses(type).flatMap { it.methods.toList() }
val interfacesMethods = ClassUtils.getAllInterfaces(type).flatMap { it.methods.toList() }
return (declaredMethods + superClassesMethods + interfacesMethods)
.asSequence()
.filter { !it.isSynthetic }
.filter { !Modifier.isPrivate(it.modifiers) }
// discard any methods that are coming off the root of the class hierarchy
// to avoid issues with duplicate method declarations
.filter { it.declaringClass != Object::class.java }
// subscription resolvers must return a publisher
.filter { search.source !is GraphQLSubscriptionResolver || resolverMethodReturnsPublisher(it) }
.toList()
}
private fun resolverMethodReturnsPublisher(method: Method) =
method.returnType.isAssignableFrom(Publisher::class.java)
|| resolverMethodReturnsPublisherFuture(method)
|| receiveChannelToPublisherWrapper(method)
private fun resolverMethodReturnsPublisherFuture(method: Method) =
method.returnType.isAssignableFrom(CompletableFuture::class.java)
&& method.genericReturnType is ParameterizedType
&& (method.genericReturnType as ParameterizedType).actualTypeArguments
.any {
it is ParameterizedType && it.unwrap().isAssignableFrom(Publisher::class.java)
}
private fun receiveChannelToPublisherWrapper(method: Method) =
method.returnType.isAssignableFrom(ReceiveChannel::class.java)
&& options.genericWrappers.any { wrapper ->
val isReceiveChannelWrapper = wrapper.type == method.returnType
val hasPublisherTransformer = wrapper
.transformer.javaClass
.declaredMethods
.filter { it.name == "invoke" }
.any { it.returnType.isAssignableFrom(Publisher::class.java) }
isReceiveChannelWrapper && hasPublisherTransformer
}
private fun isBoolean(type: GraphQLLangType) = type.unwrap().let { it is TypeName && it.name == Scalars.GraphQLBoolean.name }
private fun verifyMethodArguments(method: Method, requiredCount: Int, search: Search): Boolean {
val appropriateFirstParameter = if (search.requiredFirstParameterType != null) {
method.genericParameterTypes.firstOrNull()?.let {
it == search.requiredFirstParameterType || method.declaringClass.typeParameters.contains(it)
} ?: false
} else {
true
}
val methodParameterCount = getMethodParameterCount(method)
val methodLastParameter = getMethodLastParameter(method)
val correctParameterCount = methodParameterCount == requiredCount ||
(methodParameterCount == (requiredCount + 1) && allowedLastArgumentTypes.contains(methodLastParameter))
return correctParameterCount && appropriateFirstParameter
}
private fun getMethodParameterCount(method: Method): Int {
return try {
method.kotlinFunction?.valueParameters?.size ?: method.parameterCount
} catch (e: InternalError) {
method.parameterCount
}
}
private fun getMethodLastParameter(method: Method): Type? {
return try {
method.kotlinFunction?.valueParameters?.lastOrNull()?.type?.javaType
?: method.parameterTypes.lastOrNull()
} catch (e: InternalError) {
method.parameterTypes.lastOrNull()
}
}
private fun findResolverProperty(field: FieldDefinition, search: Search) =
FieldUtils.getAllFields(search.type.unwrap()).find { it.name == field.name }
private fun getMissingFieldMessage(field: FieldDefinition, searches: List<Search>, scannedProperties: Boolean): String {
val signatures = mutableListOf("")
val isBoolean = isBoolean(field.type)
var isSubscription = false
searches.forEach { search ->
signatures.addAll(getMissingMethodSignatures(field, search, isBoolean, scannedProperties))
isSubscription = isSubscription || search.source is GraphQLSubscriptionResolver
}
val sourceName = field.sourceLocation?.sourceName ?: "<unknown>"
val sourceLocation = field.sourceLocation?.let { "$sourceName:${it.line}" } ?: "<unknown>"
return "No method${if (scannedProperties) " or field" else ""} found as defined in schema $sourceLocation with any of the following signatures " +
"(with or without one of $allowedLastArgumentTypes as the last argument), in priority order:\n${signatures.joinToString("\n ")}" +
if (isSubscription) "\n\nNote that a Subscription data fetcher must return a Publisher of events" else ""
}
private fun getMissingMethodSignatures(field: FieldDefinition, search: Search, isBoolean: Boolean, scannedProperties: Boolean): List<String> {
val baseType = search.type.unwrap()
val signatures = mutableListOf<String>()
val args = mutableListOf<String>()
val sep = ", "
if (search.requiredFirstParameterType != null) {
args.add(search.requiredFirstParameterType.name)
}
args.addAll(field.inputValueDefinitions.map { "~${it.name}" })
val argString = args.joinToString(sep)
signatures.add("${baseType.name}.${field.name}($argString)")
if (isBoolean) {
signatures.add("${baseType.name}.is${field.name.replaceFirstChar(Char::titlecase)}($argString)")
}
signatures.add("${baseType.name}.get${field.name.replaceFirstChar(Char::titlecase)}($argString)")
if (scannedProperties) {
signatures.add("${baseType.name}.${field.name}")
}
return signatures
}
data class Search(
val type: JavaType,
val resolverInfo: ResolverInfo,
val source: Any?,
val requiredFirstParameterType: Class<*>? = null
)
}
internal class FieldResolverError(msg: String) : RuntimeException(msg)