spring-projects / spring-graphql

Spring Integration for GraphQL
https://spring.io/projects/spring-graphql
Apache License 2.0
1.5k stars 297 forks source link

Field mapped to Kotlin function reported as unmapped by SchemaMappingInspector #995

Closed sothawo closed 1 week ago

sothawo commented 3 weeks ago

I have the following schema:

type Author {
    id: ID!
    firstName: String!
    lastName: String!
    fullName: String!
    birthDate: Date!
}

This is implemented with a Kotlin data class like

data class Author(
    val id: String,
    val firstName: String,
    val lastName: String,
    val birthDate: LocalDate,
) {
    fun fullName(): String = "$firstName $lastName"
}

This works fine, I get the data in my GraphQL responses, but fullName is reported as unmapped field. For this simple case I can change that to a synthesized getter and the reporting is gone:

data class Author(
    val id: String,
    val firstName: String,
    val lastName: String,
    val birthDate: LocalDate,
) {
    val fullName get() = "$firstName $lastName"
}

But we have cases where we need the implementation as a function and cannot switch to a property.

Is there a possibility to recognize these fields as mapped?

sothawo commented 3 weeks ago

ok, I get rid off the unmapped field by setting the JVM name:

    @JvmName("getFullName")
    fun fullName(): String = "$firstName $lastName"

Then the function is seen as the getter for a property with the name fullName and the field is mapped.

rstoyanchev commented 1 week ago

It looks like at runtime, GraphQL Java matches the function with a predicate for "record-like" methods. Essentially, public methods without arguments that return a value. I've added a similar check that should make this work. It's in 1.3.2-SNAPSHOT if you're able to give it a try.

sothawo commented 1 week ago

Hi Rossen, this works now, no need for the @JvmName anymore (which is pretty ugly).

rstoyanchev commented 1 week ago

Thanks for confirming, much appreciated.