|
| 1 | +package com.coxautodev.graphql.tools |
| 2 | + |
| 3 | +import graphql.Scalars |
| 4 | +import graphql.language.FieldDefinition |
| 5 | +import graphql.language.TypeName |
| 6 | +import graphql.schema.DataFetchingEnvironment |
| 7 | +import org.apache.commons.lang3.ClassUtils |
| 8 | +import org.apache.commons.lang3.reflect.FieldUtils |
| 9 | +import java.lang.reflect.Field |
| 10 | +import java.lang.reflect.Method |
| 11 | + |
| 12 | +/** |
| 13 | + * @author Andrew Potter |
| 14 | + */ |
| 15 | +internal class FieldResolverScanner { |
| 16 | + |
| 17 | + companion object { |
| 18 | + fun getAllMethods(type: Class<*>): List<Method> { |
| 19 | + return type.declaredMethods.toList() + ClassUtils.getAllSuperclasses(type).flatMap { it.declaredMethods.toList() } |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + fun findFieldResolver(field: FieldDefinition, resolverInfo: ResolverInfo): FieldResolver { |
| 24 | + val searches = resolverInfo.getFieldSearches() |
| 25 | + |
| 26 | + val scanProperties = field.inputValueDefinitions.isEmpty() |
| 27 | + val found = searches.mapNotNull { search -> findFieldResolver(field, search, scanProperties) } |
| 28 | + |
| 29 | + if(resolverInfo is RootResolverInfo && found.size > 1) { |
| 30 | + throw FieldResolverError("Found more than one matching resolver for field '$field': $found") |
| 31 | + } |
| 32 | + |
| 33 | + return found.firstOrNull() ?: throw FieldResolverError(getMissingFieldMessage(field, searches, scanProperties)) |
| 34 | + } |
| 35 | + |
| 36 | + private fun findFieldResolver(field: FieldDefinition, search: Search, scanProperties: Boolean): FieldResolver? { |
| 37 | + val method = findResolverMethod(field, search) |
| 38 | + if(method != null) { |
| 39 | + return MethodFieldResolver(field, search, method.apply { isAccessible = true }) |
| 40 | + } |
| 41 | + |
| 42 | + if(scanProperties) { |
| 43 | + val property = findResolverProperty(field, search) |
| 44 | + if(property != null) { |
| 45 | + return PropertyFieldResolver(field, search, property.apply { isAccessible = true }) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return null |
| 50 | + } |
| 51 | + |
| 52 | + private fun isBoolean(type: GraphQLLangType) = type.unwrap().let { it is TypeName && it.name == Scalars.GraphQLBoolean.name } |
| 53 | + |
| 54 | + private fun findResolverMethod(field: FieldDefinition, search: Search): java.lang.reflect.Method? { |
| 55 | + |
| 56 | + val methods = getAllMethods(search.type) |
| 57 | + val argumentCount = field.inputValueDefinitions.size + if(search.requiredFirstParameterType != null) 1 else 0 |
| 58 | + val name = field.name |
| 59 | + |
| 60 | + val isBoolean = isBoolean(field.type) |
| 61 | + |
| 62 | + // Check for the following one by one: |
| 63 | + // 1. Method with exact field name |
| 64 | + // 2. Method that returns a boolean with "is" style getter |
| 65 | + // 3. Method with "get" style getter |
| 66 | + return methods.find { |
| 67 | + it.name == name && verifyMethodArguments(it, argumentCount, search) |
| 68 | + } ?: methods.find { |
| 69 | + (isBoolean && it.name == "is${name.capitalize()}") && verifyMethodArguments(it, argumentCount, search) |
| 70 | + } ?: methods.find { |
| 71 | + it.name == "get${name.capitalize()}" && verifyMethodArguments(it, argumentCount, search) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private fun verifyMethodArguments(method: java.lang.reflect.Method, requiredCount: Int, search: Search): Boolean { |
| 76 | + val correctParameterCount = method.parameterCount == requiredCount || (method.parameterCount == (requiredCount + 1) && method.parameterTypes.last() == DataFetchingEnvironment::class.java) |
| 77 | + val appropriateFirstParameter = if(search.requiredFirstParameterType != null) method.parameterTypes.firstOrNull() == search.requiredFirstParameterType else true |
| 78 | + return correctParameterCount && appropriateFirstParameter |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + private fun findResolverProperty(field: FieldDefinition, search: Search): Field? { |
| 83 | + return FieldUtils.getAllFields(search.type).find { it.name == field.name } |
| 84 | + } |
| 85 | + |
| 86 | + fun getMissingFieldMessage(field: FieldDefinition, searches: List<Search>, scannedProperties: Boolean): String { |
| 87 | + val signatures = mutableListOf("") |
| 88 | + val isBoolean = isBoolean(field.type) |
| 89 | + |
| 90 | + searches.forEach { search -> |
| 91 | + signatures.addAll(getMissingMethodSignatures(field, search, isBoolean, scannedProperties)) |
| 92 | + } |
| 93 | + |
| 94 | + return "No method${if(scannedProperties) " or field" else ""} found with any of the following signatures (with or without ${DataFetchingEnvironment::class.java.name} as the last argument), in priority order:\n${signatures.joinToString("\n ")}" |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + fun getMissingMethodSignatures(field: FieldDefinition, search: Search, isBoolean: Boolean, scannedProperties: Boolean): List<String> { |
| 99 | + val baseType = search.type |
| 100 | + val signatures = mutableListOf<String>() |
| 101 | + val args = mutableListOf<String>() |
| 102 | + val sep = ", " |
| 103 | + |
| 104 | + if(search.requiredFirstParameterType != null) { |
| 105 | + args.add(search.requiredFirstParameterType.name) |
| 106 | + } |
| 107 | + |
| 108 | + args.addAll(field.inputValueDefinitions.map { "~${it.name}" }) |
| 109 | + |
| 110 | + val argString = args.joinToString(sep) |
| 111 | + |
| 112 | + signatures.add("${baseType.name}.${field.name}($argString)") |
| 113 | + if(isBoolean) { |
| 114 | + signatures.add("${baseType.name}.is${field.name.capitalize()}($argString)") |
| 115 | + } |
| 116 | + signatures.add("${baseType.name}.get${field.name.capitalize()}($argString)") |
| 117 | + if(scannedProperties) { |
| 118 | + signatures.add("${baseType.name}.${field.name}") |
| 119 | + } |
| 120 | + |
| 121 | + return signatures |
| 122 | + } |
| 123 | + |
| 124 | + data class Search(val type: Class<*>, val resolverInfo: ResolverInfo, val source: Any?, val requiredFirstParameterType: Class<*>? = null) |
| 125 | +} |
| 126 | + |
| 127 | +class FieldResolverError(msg: String): RuntimeException(msg) |
0 commit comments