Skip to content

Support for @SchemaMapping and @BatchMapping based off interface type  #294

@francis-a

Description

@francis-a

I'm having a hard time registering a @SchemaMapping function that should be used as a data fetcher for all subclasses for an interface.

Given the example schema:

interface User {
    username: String!
    color: String!
}

type PurpleUser implements User {
    username: String!
    color: String!
}

type GreenUser implements User {
    username: String!
    color: String!
}

type Query {
    userGet(username: String): User
}

And the following Kotlin model:

interface User {
    val username: String
}
data class PurpleUser(override val username: String) : User
data class GreenUser(override val username: String) : User

My expectation is that by defining a schema mapping for the User interface I would be able to register a data fetcher for the color field on all User subclasses.

  @SchemaMapping(typeName = "User", field = "color")
    fun mapColor(user: User): String {
        return when(user.username) {
            "purpleUser" -> "purple"
            "greenUser" -> "green"
            else -> throw RuntimeException()
        }
    }

However in this example the following query:

{
  userGet(username: "green") {
    username
    color
  }
}

Results in an exception.

{
  "errors": [
    {
      "message": "The field at path '/userGet/color' was declared as a non null type
....

Registering a @SchemaMapping for both concrete types (PurpleUser and GreenUser) fixes this issue.

Is it possible to configure this in a way to avoid duplication of mapping annotations across concrete subtypes? I've included a sample project that demonstrates this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions