Skip to content

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

New issue

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

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

Already on GitHub? # to your account

Closed
sothawo opened this issue Jun 13, 2024 · 4 comments
Closed
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@sothawo
Copy link

sothawo commented Jun 13, 2024

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?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 13, 2024
@sothawo
Copy link
Author

sothawo commented Jun 14, 2024

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 rstoyanchev self-assigned this Jun 26, 2024
@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 26, 2024
@rstoyanchev rstoyanchev added this to the 1.3.2 milestone Jun 26, 2024
@rstoyanchev rstoyanchev changed the title Unmapped field reported for a field implemented as function in Kotlin Field mapped to Kotlin function reported as unmapped by SchemaMappingInspector Jun 26, 2024
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Jun 27, 2024

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
Copy link
Author

sothawo commented Jun 28, 2024

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

@rstoyanchev
Copy link
Contributor

Thanks for confirming, much appreciated.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants