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
11 changes: 10 additions & 1 deletion src/scalars/iso-date/DateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@ 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);
// This should support 13 and 12-digit timestamps
if (
(value <= 9999999999999 && value.toString().length === 13) ||
(value <= 999999999999 && value.toString().length === 12)
) {
return new Date(value);
} else {
// Convert to milliseconds
return new Date(value * 1000);
}
} catch (e) {
throw createGraphQLError('DateTime cannot represent an invalid Unix timestamp ' + value);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/iso-date/DateTime.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?

type: GraphQLDateTime,
resolve: () => 854325678,
},
validUnixTimestamp: {
type: GraphQLDateTime,
resolve: () => 854325678000,
Expand Down
11 changes: 8 additions & 3 deletions tests/iso-date/DateTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 maximum representable unix timestamp
[854325678, '1997-01-27T00:41:18.000Z'],
[866478, '1970-01-11T00:41:18.000Z'],
[1713305032000, '2024-04-16T22:03:52.000Z'],
[1713305032, '2024-04-16T22:03:52.000Z'],
// The maximum representable unix timestamp in milliseconds
[2147483647000, '2038-01-19T03:14:07.000Z'],
// The maximum representable unix timestamp in seconds
[2147483647, '2038-01-19T03:14:07.000Z'],
// 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?

[-2147483648, '1901-12-13T20:45:52.000Z'],
].forEach(([value, expected]) => {
it(`serializes unix timestamp ${stringify(value)} into date-string ${expected}`, () => {
expect(GraphQLDateTime.serialize(value).toJSON()).toEqual(expected);
Expand Down