Java Time types cannot be used when serialising GraphQlRequest
See original GitHub issueIssue 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
-
Add custom scalar type called Time
scalar Time
-
Add a custom scalar parser so Time gets generated as java.time.OffsetTime
-
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:
- Created 2 years ago
- Reactions:10
- Comments:8 (3 by maintainers)
Top GitHub Comments
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
This is good if you need this more flexible format. 👍🏻