Skip to content

Ability to add custom serialization for scalar types #5

@PavelSusloparov

Description

@PavelSusloparov

Is your feature request related to a problem? Please describe.

I have a custom scalar type, which I register as

tasks.named<GraphqlCodegenGradleTask>("graphqlCodegen") {
    ...
    customTypesMapping = mutableMapOf(Pair("EpochMillis", "java.time.LocalDateTime"))
    ...
}

it gives me

public class Comment {
    ...
    private java.time.LocalDateTime createdTimestamp;
    ...

I would like to take a step further and define serialization logic with Jackson for the type

public class Comment {
    ...
    @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = com.example.json.EpochMillisScalarDeserializer.class)
    private java.time.LocalDateTime createdTimestamp;
    ...

where(Kotlin code)

object EpochMillisScalarDeserializer : StdDeserializer<LocalDateTime>(LocalDateTime::class.java) {
    override fun deserialize(p: JsonParser?, ctxt: DeserializationContext?): LocalDateTime {
        return if (p != null) {
            toLocalDateTime(p.longValue)
        } else {
            LocalDateTime.now()
        }
    }
}

Describe the solution you'd like
I would like to have a configuration

tasks.named<GraphqlCodegenGradleTask>("graphqlCodegen") {
    ...
    customTypesMapping = mutableMapOf(Pair("EpochMillis", "java.time.LocalDateTime"))
    customAnnotationsMapping = mutableMapOf(Pair("EpochMillis", "com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = com.example.json.EpochMillisScalarDeserializer.class)"))
    ...
}

where custom annotations will be added above fields with the EpochMillis type

Describe alternatives you've considered
Write a custom file editor after the object generation, which is capable of find the right place to insert the desired logic.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions