Skip to content

Commit 8f7575d

Browse files
authored
Merge pull request #871 from graphql-java-kickstart/cleanup/type-util
Use graphql-java's TypeUtil to unwrap and print SDL types
2 parents ccefcba + 18182ad commit 8f7575d

5 files changed

Lines changed: 11 additions & 15 deletions

File tree

‎src/main/kotlin/graphql/kickstart/tools/SchemaClassScanner.kt‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ internal class SchemaClassScanner(
196196
}
197197

198198
private fun handleDirectiveInput(inputType: Type<*>) {
199-
val inputTypeName = (inputType.unwrap() as TypeName).name
199+
val inputTypeName = inputType.unwrap().name
200200
val typeDefinition = ScalarInfo.GRAPHQL_SPECIFICATION_SCALARS_DEFINITIONS[inputTypeName]
201201
?: definitionsByName[inputTypeName]
202202
?: error("No ${TypeDefinition::class.java.simpleName} for type name $inputTypeName")
@@ -242,7 +242,7 @@ internal class SchemaClassScanner(
242242
else -> emptyList()
243243
}
244244
}
245-
.mapNotNull { (it.unwrap() as TypeName).name }
245+
.mapNotNull { it.unwrap().name }
246246
.toSet()
247247
}
248248

@@ -366,7 +366,7 @@ internal class SchemaClassScanner(
366366
private fun handleInterfaceFieldTypes(): Boolean {
367367
val newTypes = dictionary.keys.filterIsInstance<InterfaceTypeDefinition>().flatMap { iface ->
368368
iface.withExtensions(typeExtensions).flatMap { it.fieldDefinitions }.mapNotNull { field ->
369-
val type = definitionsByName[(field.type.unwrap() as TypeName).name]
369+
val type = definitionsByName[field.type.unwrap().name]
370370
if ((type is UnionTypeDefinition || type is InterfaceTypeDefinition) && !dictionary.containsKey(type)) {
371371
type to FieldTypeReference("${iface.name}.${field.name}")
372372
} else {
@@ -596,7 +596,7 @@ internal class SchemaClassScanner(
596596
.flatMap { it.inputValueDefinitions }
597597
.forEach { inputValueDefinition ->
598598
val inputGraphQLType = inputValueDefinition.type.unwrap()
599-
if (inputGraphQLType !is TypeName || ScalarInfo.GRAPHQL_SPECIFICATION_SCALARS_DEFINITIONS.containsKey(inputGraphQLType.name)) {
599+
if (ScalarInfo.GRAPHQL_SPECIFICATION_SCALARS_DEFINITIONS.containsKey(inputGraphQLType.name)) {
600600
return@forEach
601601
}
602602

‎src/main/kotlin/graphql/kickstart/tools/TypeClassMatcher.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import graphql.kickstart.tools.util.JavaType
66
import graphql.kickstart.tools.util.isSubtypeOf
77
import graphql.language.*
88
import graphql.schema.idl.ScalarInfo
9+
import graphql.schema.idl.TypeUtil
910
import java.lang.reflect.ParameterizedType
1011
import java.util.*
1112

@@ -14,7 +15,7 @@ import java.util.*
1415
*/
1516
internal class TypeClassMatcher(private val definitionsByName: Map<String, TypeDefinition<*>>) {
1617

17-
private fun error(potentialMatch: PotentialMatch, msg: String) = SchemaClassScannerError("Unable to match type definition (${potentialMatch.graphQLType}) for reference ${potentialMatch.reference} with java type (${potentialMatch.javaType}): $msg")
18+
private fun error(potentialMatch: PotentialMatch, msg: String) = SchemaClassScannerError("Unable to match type definition (${TypeUtil.simplePrint(potentialMatch.graphQLType)}) for reference ${potentialMatch.reference} with java type (${potentialMatch.javaType}): $msg")
1819

1920
fun match(potentialMatch: PotentialMatch): Match {
2021
return match(potentialMatch, potentialMatch.graphQLType, potentialMatch.javaType, true)

‎src/main/kotlin/graphql/kickstart/tools/UnmappedInputFieldChecker.kt‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import com.fasterxml.jackson.databind.deser.std.ReferenceTypeDeserializer
1313
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException
1414
import graphql.kickstart.tools.resolver.MethodFieldResolver
1515
import graphql.kickstart.tools.util.unwrap
16-
import graphql.language.TypeName
1716
import graphql.schema.GraphQLInputObjectField
1817
import graphql.schema.GraphQLInputObjectType
1918
import graphql.schema.GraphQLNamedType
@@ -33,7 +32,7 @@ internal class UnmappedInputFieldChecker(
3332

3433
fun check(objectType: String, resolver: MethodFieldResolver) {
3534
resolver.field.inputValueDefinitions.forEachIndexed { index, argument ->
36-
val inputObject = (argument.type.unwrap() as? TypeName)?.let { inputObjects[it.name] } ?: return@forEachIndexed
35+
val inputObject = inputObjects[argument.type.unwrap().name] ?: return@forEachIndexed
3736
val usage = "argument '${argument.name}' of field '$objectType.${resolver.field.name}'"
3837

3938
try {
@@ -138,7 +137,7 @@ internal class UnmappedInputFieldChecker(
138137
}
139138

140139
private fun inputObjectOf(type: GraphQLType): GraphQLInputObjectType? =
141-
(GraphQLTypeUtil.unwrapAllAs<GraphQLType>(type) as? GraphQLNamedType)?.let { inputObjects[it.name] }
140+
inputObjects[GraphQLTypeUtil.unwrapAllAs<GraphQLNamedType>(type).name]
142141

143142
private fun consequence(mapper: ObjectMapper, inputObject: GraphQLInputObjectType, field: GraphQLInputObjectField): String =
144143
when {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import graphql.kickstart.tools.RootResolverInfo
77
import graphql.kickstart.tools.SchemaParserOptions
88
import graphql.kickstart.tools.util.*
99
import graphql.language.FieldDefinition
10-
import graphql.language.TypeName
1110
import graphql.schema.DataFetchingEnvironment
1211
import kotlinx.coroutines.channels.ReceiveChannel
1312
import org.apache.commons.lang3.ClassUtils
@@ -159,7 +158,7 @@ internal class FieldResolverScanner(val options: SchemaParserOptions) {
159158
isReceiveChannelWrapper && hasPublisherTransformer
160159
}
161160

162-
private fun isBoolean(type: GraphQLLangType) = type.unwrap().let { it is TypeName && it.name == Scalars.GraphQLBoolean.name }
161+
private fun isBoolean(type: GraphQLLangType) = type.unwrap().name == Scalars.GraphQLBoolean.name
163162

164163
private fun verifyMethodArguments(method: Method, requiredCount: Int, search: Search): Boolean {
165164
val appropriateFirstParameter = if (search.requiredFirstParameterType != null) {

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import graphql.kickstart.tools.SchemaParserOptions
55
import graphql.kickstart.tools.TypeExtensions
66
import graphql.language.*
77
import graphql.schema.DataFetchingEnvironment
8+
import graphql.schema.idl.TypeUtil
89
import kotlinx.coroutines.CoroutineScope
910
import kotlinx.coroutines.Dispatchers
1011
import org.apache.commons.lang3.reflect.TypeUtils
@@ -29,11 +30,7 @@ internal typealias JavaType = java.lang.reflect.Type
2930
internal typealias JavaMethod = Method
3031
internal typealias GraphQLLangType = Type<*>
3132

32-
internal fun Type<*>.unwrap(): Type<*> = when (this) {
33-
is NonNullType -> this.type.unwrap()
34-
is ListType -> this.type.unwrap()
35-
else -> this
36-
}
33+
internal fun Type<*>.unwrap(): TypeName = TypeUtil.unwrapAll(this)
3734

3835
internal inline fun <reified T : TypeDefinition<*>> TypeExtensions.extensionsOf(name: String): List<T> =
3936
this[name].orEmpty().filterIsInstance<T>()

0 commit comments

Comments
 (0)