apollographql / apollo-kotlin

:rocket:  A strongly-typed, caching GraphQL client for the JVM, Android, and Kotlin multiplatform.
https://www.apollographql.com/docs/kotlin
MIT License
3.75k stars 651 forks source link

Custom scalar does not allow generics #6156

Closed cvb941 closed 1 week ago

cvb941 commented 1 month ago

Version

4.0.0

Summary

I'm trying to setup an adapter for a Mass measurement from a float.

mapScalar("Mass", "io.nacular.measured.units.Measure<Mass>", "data.apollo.MassAdapter")

Notice that the target class has a type parameter.

Gradle sync fails with the following error: Can't escape identifier `Measure<Mass>` because it contains illegal characters: <>

This is the adapter (for completeness):

val MassAdapter = object : Adapter<Measure<Mass>> {
    override fun fromJson(reader: JsonReader, customScalarAdapters: CustomScalarAdapters): Measure<Mass> {
        val weightDouble = reader.nextDouble()
        return weightDouble.asMass(Mass.baseUnit)
    }

    override fun toJson(writer: JsonWriter, customScalarAdapters: CustomScalarAdapters, value: Measure<Mass>) {
        writer.value(value.`in`(Mass.baseUnit))
    }
}

Steps to reproduce the behavior

No response

Logs

(Your logs here)
BoD commented 1 month ago

Thanks for reporting this! We may be able to support this in the future but in the meantime a simple solution is to use a typealias:

typealias MassMeasure = Measure<Mass>
mapScalar("Mass", "data.apollo.MassMeasure", "data.apollo.MassAdapter")
martinbonnin commented 1 month ago

Closing as a duplicate of https://github.com/apollographql/apollo-kotlin/issues/3243

martinbonnin commented 1 week ago

(forgot to close) this will be available in next release, closing now.

github-actions[bot] commented 1 week ago

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Kotlin usage and allow us to serve you better.