DateTime doesn't serialize a timestamp
See original GitHub issueUsing the DateTime
scalar appears to serialize to a Date
object, which subsequently prints into the GraphQL response using Date’s default .toString()
implementation. As a result:
My GraphQL response returns this: Fri, 16 Mar 2018 11:50:07 GMT
But I was expecting this: 2018-03-16 11:50:07.000Z
The relevant code is here – https://github.com/Urigo/graphql-scalars/blob/master/src/scalars/iso-date/DateTime.ts#L35-L49. The choice to always return a Date
object from the serialize method appears to be intentional, though I can’t figure why. Am I overlooking something, or is this a legit issue?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:12 (2 by maintainers)
Top Results From Across the Web
How to overcome "datetime.datetime not JSON serializable"?
My quick & dirty JSON dump that eats dates and everything: json.dumps(my_dictionary, indent=4, sort_keys=True, default=str). default is a function applied ...
Read more >DateTimeZoneHandling setting - Json.NET
This sample uses the T:Newtonsoft.Json.DateTimeZoneHandling setting to control how T:System.DateTime and T:System.DateTimeOffset are serialized.
Read more >TypeError: Object of type datetime is not JSON serializable
The Python TypeError: Object of type datetime is not JSON serializable occurs when we try to convert a `datetime` object to a JSON...
Read more >Jackson Date - Baeldung
It's important to note that Jackson will serialize the Date to a timestamp format by default (number of milliseconds since January 1st, ...
Read more >Unity Json utility does not serialize DateTime
The problem with JsonUtility, is that it doesn't save properties, just public fields and fields with the [SerializeField] attribute.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@yvann You’re right! This has been fixed, now
parseValue
acceptsDate
instance. @gmac Feel free to open a new issue with more details because we couldn’t reproduce the issue.This is still an issue when used with introspection. See the code here https://github.com/graphql/graphql-js/blob/master/src/utilities/astFromValue.js#L106-L141 that drives introspection when using default values. The default values need to be serialized. Unfortunately, graphqljs doesn’t call JSON.toString() and so this hits the throw clause at the end there.
Still not sure whether this needs to be fixed here to serialize to a string, or on graphqljs to call JSON.toString() if its an object.
I suspect the approach has the same vulnerability in others of the scalar types if you relying on JSON.toString() to actually due the final serialization