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
9 changes: 8 additions & 1 deletion src/scalars/iso-date/DateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

if (dateInMilliseconds.toString().length === value.toString().length) {
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
2 changes: 1 addition & 1 deletion tests/iso-date/DateTime.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const schema = new GraphQLSchema({
},
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: () => 854325678000,
resolve: () => 854325678,
},
invalidDateString: {
type: GraphQLDateTime,
Expand Down
8 changes: 4 additions & 4 deletions tests/iso-date/DateTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ 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?

[854325678, '1997-01-27T00:41:18.000Z'],
[866478, '1970-01-11T00:41:18.000Z'],
// The maximum representable unix timestamp
[2147483647000, '2038-01-19T03:14:07.000Z'],
[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