spring-graphql
spring-graphql copied to clipboard
Custom scalar for kotlin.time.Duration coercing issue
For custom kotlin.time.Duration scalar, f.e.
scalar Duration
type Query {
test: [Sample]
}
type Sample {
duration: Duration
}
when I define custom GraphQLScalarType in spring configuration for kotlin.time.Duration (GraphQLScalarType.newScalar().name("Duration").coercing(DurationCoercing))
with Coercing<I,O> implementation class as described below, dataFetcherResult comes as Long type (the actual value passed to serialize method is "rawValue" property of kotlin.time.Duration, which is a private field)
object DurationCoercing : Coercing<kotlin.time.Duration, String> {
override fun serialize(dataFetcherResult: Any): String {
return ...
}
....
```
So the question is, why kotlin.time.Duration comes as Long to Coercing<I,O> serialize method - is that correct behaviour or a bug? (if bug - is that a spring issue or issue of Kotlin kotlin.time.Duration?)
here is a [link](https://github.com/ghmulti/spring-graphql-kotlin-duration) with sample application that demonstrates the problem (I have to do `((dataFetcherResult as Long) shr 1).toDuration(DurationUnit.NANOSECONDS).toString()`, due to implementation details of kotlin.time.Duration to serialize properly)
Hi.
It seems buggy, but it's a correct behavior. kotlin.time.Duration
is a value class and it is unboxed here.
Hi.
It seems buggy, but it's a correct behavior.
kotlin.time.Duration
is a value class and it is unboxed here.
right, seems that in kotlin.serialization you could choose which primitive type will be used for boxing/unboxing(through PrimitiveSerialDescriptor) - is that supported somehow in spring?
I'm not sure where the unboxing happens, but Coercing
is a GraphQL Java type, so this is more of a GraphQL Java question I think. There isn't any behavior specific to Spring in this, we just pass on the return value from the controller as is.
Closing for now, as I don't see anything we can do, but feel free to add more comments if you have further information or links to related issues.