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

Unix timestamps are not serialized correctly for scalar DateTime #2139

Open
yamatsafi opened this issue Sep 22, 2023 · 1 comment
Open

Unix timestamps are not serialized correctly for scalar DateTime #2139

yamatsafi opened this issue Sep 22, 2023 · 1 comment

Comments

@yamatsafi
Copy link

yamatsafi commented Sep 22, 2023

Describe the bug

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 argument is in milliseconds, not seconds, the number of seconds needs to be multiplied by 1000.

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

To Reproduce

1- define your scalars and type

scalar DateTime;
type Event {
  id: ID!
  name: String!
  start: DateTime!
  end: DateTime!
};

2- then import and export these types in a resolver:

const {
  GraphQLDateTime
} = require('graphql-scalars');

module.exports = {
  DateTime: GraphQLDateTime,
};

3- Now you have some event resolver that returns this data:

[
  {
    id: 18797,
    start: 1695301200,
    end: 1695330000,
    name: 'Regular'
  },
  {
    id: 18798,
    start: 1695387600,
    end: 1695416400,
    name: 'Regular'
  }
]

4- The start and date will come across as 1970-01-20T14:56:56.400Z and not the correct date.

This is because the Unix timestamps aren't serialized correctly here, below:
https://github.com/Urigo/graphql-scalars/blob/v1.22.2/src/scalars/iso-date/DateTime.ts#L36

This line should be:
return new Date(value * 1000); (this matches graphql-iso-date implementation as well)

Expected behavior

Unix timestamp: 1695301200
Actual: 1970-01-20T14:55:01.200Z
Expected: 2023-09-21T13:00:00.000Z

Unix timestamp: 1695330000
Actual: 1970-01-20T14:55:30.000Z
Expected: 2023-09-21T21:00:00.000Z

Unix timestamp: 1695387600
Actual: 1970-01-20T14:56:27.600Z
Expected: 2023-09-22T13:00:00.000Z

Unix timestamp: 1695416400
Actual: 1970-01-20T14:56:56.400Z
Expected: 2023-09-22T21:00:00.000Z

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",
@design1online
Copy link

I just found this same issue today. This is breaking all my bigint timestamps that I'm pulling from my DB. I had to manually add in the old package I was using so my timestamps would stop breaking everywhere.

Can we please prioritize this?

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

No branches or pull requests

2 participants