Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scan for resolvers that have a supertype of the data class #638

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions src/main/kotlin/graphql/kickstart/tools/ResolverInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,10 @@ internal class NormalResolverInfo(
}
}

internal class MultiResolverInfo(val resolverInfoList: List<NormalResolverInfo>) : DataClassTypeResolverInfo, ResolverInfo() {
override val dataClassType = findDataClass()

/**
* Checks if all `ResolverInfo` instances are related to the same data type
*/
private fun findDataClass(): Class<out Any> {
val dataClass = resolverInfoList.asSequence().map { it.dataClassType }.distinct().singleOrNull()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we fix this to find the correct type instead of deleting it?


if (dataClass == null) {
throw ResolverError("Resolvers may not use the same type.")
} else {
return dataClass
}
}
internal class MultiResolverInfo(val resolverInfoList: List<NormalResolverInfo>,
override val dataClassType: Class<out Any>
) : DataClassTypeResolverInfo, ResolverInfo() {

override fun getFieldSearches(): List<FieldResolverScanner.Search> {
return resolverInfoList
Expand Down
12 changes: 8 additions & 4 deletions src/main/kotlin/graphql/kickstart/tools/SchemaClassScanner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,18 @@ internal class SchemaClassScanner(
* Scan a new object for types that haven't been mapped yet.
*/
private fun scanQueueItemForPotentialMatches(item: QueueItem) {
val resolverInfoList = this.resolverInfos.filter { it.dataClassType == item.clazz }
val resolverInfo: ResolverInfo = (if (resolverInfoList.size > 1) {
MultiResolverInfo(resolverInfoList)
val resolverInfoList = this.resolverInfos.filter { it.dataClassType.unwrap().isAssignableFrom(item.clazz.unwrap()) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets extract item.clazz.unwrap() to a variable.

val resolverInfo: ResolverInfo = (if (resolverInfoList.size > 1 && item.clazz != Object::class.java) {
MultiResolverInfo(resolverInfoList, item.clazz.unwrap())
} else {
if (item.clazz == Object::class.java) {
getResolverInfoFromTypeDictionary(item.type.name)
} else {
resolverInfosByDataClass[item.clazz] ?: DataClassResolverInfo(item.clazz)
if (resolverInfoList.size == 1) {
MultiResolverInfo(resolverInfoList, item.clazz.unwrap())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why multi if there's only one?

} else {
DataClassResolverInfo(item.clazz)
}
}
})
?: throw throw SchemaClassScannerError("The GraphQL schema type '${item.type.name}' maps to a field of type java.lang.Object however there is no matching entry for this type in the type dictionary. You may need to add this type to the dictionary before building the schema.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ internal class FieldResolverScanner(val options: SchemaParserOptions) {
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)
it == search.requiredFirstParameterType || (it is Class<*> && it.unwrap().isAssignableFrom(search.requiredFirstParameterType))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the cast to Class here?

|| method.declaringClass.typeParameters.contains(it)
} ?: false
} else {
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import javax.servlet.http.Part

fun createSchema() = SchemaParser.newParser()
.schemaString(schemaDefinition)
.resolvers(Query(), Mutation(), Subscription(), ItemResolver(), UnusedRootResolver(), UnusedResolver(), EchoFilesResolver())
.resolvers(Query(), Mutation(), Subscription(), ItemResolver(), ItemInterfaceResolver(), UnusedRootResolver(), UnusedResolver(), EchoFilesResolver())
.scalars(customScalarUUID, customScalarMap, customScalarId, uploadScalar)
.dictionary("OtherItem", OtherItemWithWrongName::class)
.dictionary("ThirdItem", ThirdItem::class)
Expand Down Expand Up @@ -155,6 +155,7 @@ type Item implements ItemInterface {
type: Type!
uuid: UUID!
tags(names: [String!]): [Tag!]
uppercaseName: String!
}

type OtherItem implements ItemInterface {
Expand Down Expand Up @@ -394,6 +395,10 @@ class ItemResolver : GraphQLResolver<Item> {
}
}

class ItemInterfaceResolver : GraphQLResolver<ItemInterface> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind creating a separate test class with an example closer to the original issue instead of adding to this one?

fun uppercaseName(item: ItemInterface): String = item.name.toUpperCase()
}

class EchoFilesResolver : GraphQLMutationResolver {
fun echoFiles(fileParts: List<Part>): List<String> = fileParts.map { String(it.inputStream.readBytes()) }
}
Expand Down