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.

DateTime doesn't serialize a timestamp

See original GitHub issue

Using 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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
ardatancommented, Jul 30, 2020

@yvann You’re right! This has been fixed, now parseValue accepts Date instance. @gmac Feel free to open a new issue with more details because we couldn’t reproduce the issue.

1reaction
dyst5422commented, Jan 13, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

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