aPureBase / KGraphQL

Pure Kotlin GraphQL implementation
https://kgraphql.io
MIT License
298 stars 58 forks source link

When I use java.util.Date as a class param, I got an serialization issue. #204

Closed sustainjane98 closed 1 year ago

sustainjane98 commented 1 year ago

When I create a data class like this:

data class Person(
    val firstname: String,
    val lastname: String,
    val age: Int,
    val birthdate: Date
)

And thewn create a query resolver like this:

fun SchemaBuilder.person() {
    query("me") {
        resolver {
            ->
            Person("Hans", "Meier", 24, Date.valueOf(LocalDate.parse("1998-02-12")))
        }
    }
}

I get the following error:

An Object type must define one or more fields. Found none on type Date

Is there any way to tell the framework to use a custom serializer, to fix this?

sustainjane98 commented 1 year ago

You can write a custom serializer in the schema like that:

fun SchemaBuilder.serializerDate() {
    longScalar<Date> {
        serialize = { date -> date.time }
        deserialize = { dateString -> Date(dateString) }
    }
}