spring-graphql icon indicating copy to clipboard operation
spring-graphql copied to clipboard

Custom scalar for kotlin.time.Duration coercing issue

Open ghmulti opened this issue 2 years ago • 2 comments

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)

ghmulti avatar Aug 25 '22 21:08 ghmulti

Hi.

It seems buggy, but it's a correct behavior. kotlin.time.Duration is a value class and it is unboxed here.

turboezh avatar Aug 30 '22 00:08 turboezh

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?

ghmulti avatar Aug 30 '22 06:08 ghmulti

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.

rstoyanchev avatar Oct 18 '22 09:10 rstoyanchev