Skip to content

Java Time types cannot be used when serialising GraphQlRequest #912

@chusmc

Description

@chusmc

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"]
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingquestionFurther information is requested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions