leangen / graphql-spqr-spring-boot-starter

Spring Boot 2 starter powered by GraphQL SPQR
Apache License 2.0
275 stars 68 forks source link

Serialized OffsetDateTime has no time zone offset #143

Closed guofengzh closed 8 months ago

guofengzh commented 8 months ago

This may not be the place to ask a question, but I couldn't find an answer after googling for a long time.

The question is how to enable graphql-spqr to serialize OffsetDateTime with offset, for example, 2024-01-06 20:23:24.548994+08. Currently, graphql-spqr serializes this time into 2024-01-06T12:23:24.548994 Z.

My application uses graphql-spqr-spring-boot-starter.

I searched for #88, which led me to #366, and tried its solution, but still no success.

Urgent to resolve this issue, your help is greatly appreciated.

guofengzh commented 8 months ago

It seems that it is not related to customize the ObjectMapper, like what we did in Spring Rest app. I close it.

kaqqao commented 8 months ago

SPQR will by default serialize all zoned time values as ISO 8601 strings in UTC. Now that I think about it, for OffsetDateTime, the conversion to UTC wasn't necessary. But it is at least consistent with other time values. UTC is generally the safest to use for storage and APIs, and can be easily interpreted/converted as needed by the client. If you want to change how any scalar value is represented, it is easy enough to wire in a custom TypeMapper. Tell me if you need an example.

Btw, SPQR's date/time scalars predate graphql-java's, and have stayed largely unchanged over the years... But maybe it is time to switch to graphql-java ones by default now.

guofengzh commented 8 months ago

@kaqqao

I just understand that I should define a TypeMapper for such issue.

Following the instruction at Custom type mapper for GraphQL output and input types and browse the starter's code, I have solved the issue.

Thanks for your kind response!