Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Unix timestamps are not serialized correctly for scalar DateTime #2141

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

yamatsafi
Copy link

@yamatsafi yamatsafi commented Sep 22, 2023

Description

The DateTime scalar (GraphQLDateTime) fails to convert unix timestamp, for example, 1695416400, produces a date like 1970-01-20T14:56:56.400Z. Since the Unix timestamp represents the number of seconds since January 1st, 1970 at UTC, and the Javascript Date() function's [Time value or timestamp number] argument is in milliseconds, not seconds, the number of seconds needs to be multiplied by 1000.

In javascript, if one tries to convert a UNIX timestamp into an iso human-readable date string, one must do:

const humanReadableDate = new Date(1695416400 * 1000).toISOString();
// result 2023-09-22T21:00:00.000Z

Related # (issue)
#2139

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Screenshots/Sandbox (if appropriate/relevant):

Screenshot 2023-09-23 at 10 23 46 PM

How Has This Been Tested?

  • Test A
    Screenshot 2023-09-23 at 10 23 46 PM

  • Test B
    Changing this particular line in the library locally fixes our issue.

Test Environment:

  • OS: macOS (Ventura 13.5.2) / Docker in Red Hat Enterprise Linux Server release 7.9 (Maipo)
  • GraphQL Scalars Version: 1.22.2
  • NodeJS: 18.7.1

Additional context

  • Our stack is a nodejs with fastify v4 and the following:
  • "graphql": "^16.8.0",
  • "graphql-scalars": "^1.22.2",
  • "@graphql-tools/schema": "^10.0.0",
  • "@graphql-tools/utils": "^10.0.6",

Checklist:

  • I have followed the CONTRIBUTING doc and the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests and linter rules pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Further comments

This change is more in line with graphql-iso-date package.

The DateTime scalar (GraphQLDateTime) fails to convert any 10-digit unix timestamp, for example, 1695416400, produces a date like 1970-01-20T14:56:56.400Z.

In javascript, if one tries to convert a UNIX timestamp into a human-readable date, one must do:
const humanReadableDate = new Date(1695416400 * 1000).toISOString();
// result 2023-09-22T21:00:00.000Z
@yamatsafi yamatsafi changed the title Update DateTime.ts Update DateTime.ts to support unix timestamp Sep 24, 2023
@yamatsafi yamatsafi changed the title Update DateTime.ts to support unix timestamp fix: Unix timestamps are not serialized correctly for scalar DateTime Sep 24, 2023
@ardatan
Copy link
Collaborator

ardatan commented Sep 24, 2023

This is a breaking change and i am not sure if I want to sacrifica the support for the other timestamp format.

@yamatsafi
Copy link
Author

yamatsafi commented Sep 25, 2023

This is a breaking change and i am not sure if I want to sacrifica the support for the other timestamp format.

Not sure how this is sacrificing support for other timestamp formats, but this needs to be addressed as it doesn't work for unix timestamps. Fixed broken tests.

@yamatsafi
Copy link
Author

This is a breaking change and i am not sure if I want to sacrifica the support for the other timestamp format.

Not sure how this is sacrificing support for other timestamp formats, but this needs to be addressed as it doesn't work for unix timestamps. Fixed broken tests.

Arda, any further thoughts on this?

@yamatsafi
Copy link
Author

yamatsafi commented Oct 24, 2023

@ardatan added support for both seconds and milliseconds. I would appreciate it if you could take a look, please.

@jeremyzahner
Copy link

@ardatan We've just discovered this issue as well. Any chance of merging this?

@ardatan
Copy link
Collaborator

ardatan commented Apr 15, 2024

https://github.com/Urigo/graphql-scalars/pull/2141/files#diff-5bff20d592f8d56ae20cad088bf374d5ce38af414afd5631ab82f42481bb8473L90
This only tests the new behavior but no longer tests the old behavior.
Could you update the tests without touching the old tests, then we can merge and release?

@@ -33,7 +33,14 @@ export const GraphQLDateTimeConfig: GraphQLScalarTypeConfig<Date, Date> = /*#__P
throw createGraphQLError(`DateTime cannot represent an invalid date-time-string ${value}.`);
} else if (typeof value === 'number') {
try {
return new Date(value);
// Find out if the value is in seconds or milliseconds
const dateInMilliseconds = Date.now();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of getting the current Date, we can check the number of digits with a fixed value like
value.toString().length === X

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yamatsafi Do you have time to fix this? I could help otherwise.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd appreciate if you can :)

Copy link
Author

@yamatsafi yamatsafi Apr 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is future-proof instead of checking for 13-digits today which may not be true in a few years :-) I know it is a long way but still. @jeremyzahner Please feel free to add unit tests as @ardatan suggested or I can add it quickly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made changes, ready for review

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -26,6 +26,10 @@ const schema = new GraphQLSchema({
type: GraphQLDateTime,
resolve: () => '2016-02-01T00:00:00-11:00',
},
validUnixTimestamp: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have the same prop name with the one below?

@@ -87,11 +87,16 @@ describe('GraphQLDateTime', () => {
// Serializes Unix timestamp
[
[854325678000, '1997-01-27T00:41:18.000Z'],
[876535000, '1970-01-11T03:28:55.000Z'],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we remove this?

// The minimum representable unit timestamp
[-2147483648000, '1901-12-13T20:45:52.000Z'],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this changed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants