question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Java Time types cannot be used when serialising GraphQlRequest

See original GitHub issue

Issue Description

We are trying to use the code-gen to generate the GraphQlRequest payload, from a graphql model with custom scalar types of type java.time

We have these custom scalars defined customTypesMapping = [ Time: "java.time.OffsetTime", ]

We found that the Json generated by graphQLRequest.toHttpJsonBody() didnt append the result of during OffsetTime.toString, which is for example “20:00Z”. Debugging your task, we found these undocumented option useObjectMapperForRequestSerialization to add the type names where ObjectMapper should be used instead.

Unfortunately, the ObjetMapper your code uses, its defined as a static constant, so its not customizable. In theory if your ObjectMapper was created with new ObjectMapper().findAndRegisterModules(), it would use any jackson modules registered in the class-path, and make possible for example to have the java time types serialised

Steps to Reproduce

  1. Add custom scalar type called Time scalar Time

  2. Add a custom scalar parser so Time gets generated as java.time.OffsetTime

  3. Add this config to the gradle task

task generateGraphqlClient(type: GraphQLCodegenGradleTask) {
    graphqlSchemaPaths = ["$projectDir/src/main/resources/schema.graphqls".toString()]
    outputDir = new File("$buildDir/generated-client")
    customTypesMapping = [
            Time: "java.time.OffsetTime",
    ]
    generateClient = true
    generateBuilder = true
    generateToString = true
    useObjectMapperForRequestSerialization = ["Time"]
}

Expected Result

We should be able to serialise graphql model objects where custom scalar types are used. ObjectMapper should be able to serialise fields where a custom ObjectMapper is used, or at least where the default ObjectMapper can load any jackson modules present in the classpath

'mutation .......(......: { ..... deliveryWindow: { start: \"10:00Z\", end: \"17:00Z \"}, .....'

Actual Result

Query failed to parse :
'mutation .......(......: { ..... deliveryWindow: { start: 10:00Z, end: 17:00Z }, .....'

Your Environment and Setup

  • graphql-java-codegen version: 5.3.0
  • Build tool: Gradle
  • Mapping Config:
task generateGraphqlClient(type: GraphQLCodegenGradleTask) {
    graphqlSchemaPaths = ["$projectDir/src/main/resources/schema.graphqls".toString()]
    outputDir = new File("$buildDir/generated-client")
    customTypesMapping = [
            Time: "java.time.OffsetTime",
    ]
    generateClient = true
    generateBuilder = true
    generateToString = true
    useObjectMapperForRequestSerialization = ["Time"]
}

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:10
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
chusmccommented, Dec 6, 2021

Ok. Thanks we weren’t sure we should reconfigure the internal OBJECT_MAPPER

We will give it a go, as its just some unit tests,

Is it possible to suggest, that GraphQLRequestSerializer could be configured with an external injected ObjectMaper also?

Regards

0reactions
jxnu-liguobincommented, Sep 5, 2022

One mentioned by @jxnu-liguobin does work , but I created custom Deserializer something like below and it worked

GraphQLRequestSerializer.OBJECT_MAPPER.registerModule( SimpleModule().addSerializer( DateSerializerKotlin() ) )

class DateSerializerKotlin : JsonSerializer() { override fun serialize( localDate: LocalDate, gen: JsonGenerator?, serializers: SerializerProvider ) { val formatter = DateTimeFormatter.ofPattern(“yyyy-MM-dd”) val formattedString: String = localDate?.format(formatter) gen!!.writeString(formattedString) }

override fun handledType(): Class<LocalDate>? {
    return LocalDate::class.java
}

}

This is good if you need this more flexible format. 👍🏻

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · kobylynskyi/graphql-java-codegen - GitHub
Contribute to kobylynskyi/graphql-java-codegen development by creating an account on ... Java Time types cannot be used when serialising GraphQlRequest bug ...
Read more >
Error : Can't serialize value : Expected a 'String' or 'java.time ...
Here is the error : Can't serialize value : Expected a 'String' or 'java.time.temporal.TemporalAccessor' but was 'Timestamp'." Here is my Doctor ...
Read more >
leangen/graphql-spqr - Gitter
Hi, i am trying to use graphql spring boot starter with ws enabled. Can it handle query, mutations also over ws ? How...
Read more >
Java GraphQL Client - DGS Framework - Netflix Open Source
The DGS framework provides a GraphQL client that can be used to retrieve data from a GraphQL endpoint. The client is also useful...
Read more >
Custom Scalars in GraphQL - ITNEXT
A common use for custom types is representing dates and times. ... Defining a custom scalar type in the schema is not enough....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found