Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jun 23, 2020
1 parent fcb8cc5 commit 3b1352c
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 115 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-scalars",
"version": "1.1.8",
"version": "1.2.0",
"description": "A collection of scalar types not included in base GraphQL.",
"repository": {
"type": "git",
Expand Down
14 changes: 5 additions & 9 deletions src/scalars/iso-date/DateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
import { GraphQLScalarType, Kind } from 'graphql';
import type { GraphQLScalarTypeConfig } from 'graphql'; // eslint-disable-line
import { validateJSDate, validateDateTime } from './validator';
import {
serializeDateTime,
serializeDateTimeString,
parseDateTime,
} from './formatter';
import { parseDateTime } from './formatter';

/**
* An RFC 3339 compliant date-time scalar.
Expand All @@ -28,7 +24,7 @@ import {
* RFC 3339 date-time strings and unix timestamps
* to RFC 3339 UTC date-time strings.
*/
const config: GraphQLScalarTypeConfig<Date, string> = {
const config: GraphQLScalarTypeConfig<Date, Date> = {
name: 'DateTime',
description:
'A date-time string at UTC, such as 2007-12-03T10:15:30Z, ' +
Expand All @@ -38,19 +34,19 @@ const config: GraphQLScalarTypeConfig<Date, string> = {
serialize(value) {
if (value instanceof Date) {
if (validateJSDate(value)) {
return serializeDateTime(value);
return value;
}
throw new TypeError('DateTime cannot represent an invalid Date instance');
} else if (typeof value === 'string') {
if (validateDateTime(value)) {
return serializeDateTimeString(value);
return parseDateTime(value);
}
throw new TypeError(
`DateTime cannot represent an invalid date-time-string ${value}.`,
);
} else if (typeof value === 'number') {
try {
return new Date(value).toISOString();
return new Date(value);

This comment has been minimized.

Copy link
@blackwindforce

blackwindforce Aug 14, 2023

@ardatan Is it intended? Because it never throws.

} catch (e) {
throw new TypeError(
'DateTime cannot represent an invalid Unix timestamp ' + value,
Expand Down
20 changes: 4 additions & 16 deletions src/scalars/iso-date/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,12 @@ export const parseDateTime = (dateTime: string): Date => {
return new Date(dateTime);
};

// Serializes a Date into an RFC 3339 compliant date-time-string
// in the format YYYY-MM-DDThh:mm:ss.sssZ.
export const serializeDateTime = (dateTime: Date): string => {
return dateTime.toISOString();
};

// Serializes an RFC 3339 compliant date-time-string by shifting
// it to UTC.
export const serializeDateTimeString = (dateTime: string): string => {
export const serializeDateTimeString = (dateTime: string): Date => {
// If already formatted to UTC then return the time string
if (dateTime.indexOf('Z') !== -1) {
return dateTime;
return new Date(dateTime);
} else {
// These are time-strings with timezone information,
// these need to be shifted to UTC.
Expand All @@ -118,21 +112,15 @@ export const serializeDateTimeString = (dateTime: string): string => {
// The date-time-string has no fractional part,
// so we remove it from the dateTimeUTC variable.
dateTimeUTC = dateTimeUTC.replace(regexFracSec, '');
return dateTimeUTC;
return new Date(dateTimeUTC);
} else {
// These are datetime-string with fractional seconds.
// Make sure that we inject the fractional
// second part back in. The `dateTimeUTC` variable
// has millisecond precision, we may want more or less
// depending on the string that was passed.
dateTimeUTC = dateTimeUTC.replace(regexFracSec, fractionalPart[0]);
return dateTimeUTC;
return new Date(dateTimeUTC);
}
}
};

// Serializes a Unix timestamp to an RFC 3339 compliant date-time-string
// in the format YYYY-MM-DDThh:mm:ss.sssZ
export const serializeUnixTimestamp = (timestamp: number): string => {
return new Date(timestamp * 1000).toISOString();
};
21 changes: 6 additions & 15 deletions tests/iso-date/DateTime.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ const schema = new GraphQLSchema({
type: GraphQLDateTime,
resolve: () => new Date('wrong'),
},
invalidUnixTimestamp: {
type: GraphQLDateTime,
resolve: () => Number.POSITIVE_INFINITY,
},
invalidType: {
type: GraphQLDateTime,
resolve: () => [],
Expand Down Expand Up @@ -82,11 +78,11 @@ it('executes a query that includes a DateTime', async () => {

expect(response).toEqual({
data: {
validDate: '2016-05-02T10:31:42.200Z',
validUTCDateString: '1991-12-24T00:00:00Z',
validDateString: '2016-02-01T11:00:00Z',
input: '2017-10-01T00:00:00.000Z',
validUnixTimestamp: '1997-01-27T00:41:18.000Z',
validDate: new Date('2016-05-02T10:31:42.200Z'),
validUTCDateString: new Date('1991-12-24T00:00:00Z'),
validDateString: new Date('2016-02-01T11:00:00Z'),
input: new Date('2017-10-01T00:00:00.000Z'),
validUnixTimestamp: new Date('1997-01-27T00:41:18.000Z'),
inputNull: null,
},
});
Expand All @@ -105,7 +101,7 @@ it('shifts an input date-time to UTC', async () => {

expect(response).toEqual({
data: {
input: '2016-02-01T11:00:00.000Z',
input: new Date('2016-02-01T11:00:00.000Z'),
},
});
});
Expand Down Expand Up @@ -152,7 +148,6 @@ it('errors if an invalid date-time is returned from the resolver', async () => {
{
invalidDateString
invalidDate
invalidUnixTimestamp
invalidType
}
`;
Expand All @@ -163,17 +158,13 @@ it('errors if an invalid date-time is returned from the resolver', async () => {
data: {
invalidDateString: null,
invalidDate: null,
invalidUnixTimestamp: null,
invalidType: null,
},
errors: [
new GraphQLError(
'DateTime cannot represent an invalid date-time-string 2017-01-001T00:00:00Z.',
),
new GraphQLError('DateTime cannot represent an invalid Date instance'),
new GraphQLError(
'DateTime cannot represent an invalid Unix timestamp Infinity',
),
new GraphQLError(
'DateTime cannot be serialized from a non string, non numeric or non Date type []',
),
Expand Down
26 changes: 7 additions & 19 deletions tests/iso-date/DateTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('GraphQLDateTime', () => {
it(`serializes javascript Date ${stringify(value)} into ${stringify(
expected,
)}`, () => {
expect(GraphQLDateTime.serialize(value)).toEqual(expected);
expect(GraphQLDateTime.serialize(value).toJSON()).toEqual(expected);
});
});

Expand All @@ -79,13 +79,13 @@ describe('GraphQLDateTime', () => {
});

[
['2016-02-01T00:00:15Z', '2016-02-01T00:00:15Z'],
['2016-02-01T00:00:00.23498Z', '2016-02-01T00:00:00.23498Z'],
['2016-02-01T00:00:00-11:00', '2016-02-01T11:00:00Z'],
['2017-01-07T00:00:00.1+01:20', '2017-01-06T22:40:00.1Z'],
['2016-02-01T00:00:15.000Z', '2016-02-01T00:00:15.000Z'],
['2016-02-01T00:00:00.234Z', '2016-02-01T00:00:00.234Z'],
['2016-02-01T00:00:00-11:00', '2016-02-01T11:00:00.000Z'],
['2017-01-07T00:00:00.1+01:20', '2017-01-06T22:40:00.100Z'],
].forEach(([input, output]) => {
it(`serializes date-time-string ${input} into UTC date-time-string ${output}`, () => {
expect(GraphQLDateTime.serialize(input)).toEqual(output);
expect(GraphQLDateTime.serialize(input).toJSON()).toEqual(output);
});
});

Expand All @@ -111,21 +111,9 @@ describe('GraphQLDateTime', () => {
it(`serializes unix timestamp ${stringify(
value,
)} into date-string ${expected}`, () => {
expect(GraphQLDateTime.serialize(value)).toEqual(expected);
expect(GraphQLDateTime.serialize(value).toJSON()).toEqual(expected);
});
});

[Number.NaN, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY].forEach(
(value) => {
it(`throws an error serializing the invalid unix timestamp ${stringify(
value,
)}`, () => {
expect(() =>
GraphQLDateTime.serialize(value),
).toThrowErrorMatchingSnapshot();
});
},
);
});

describe('value parsing', () => {
Expand Down
6 changes: 0 additions & 6 deletions tests/iso-date/__snapshots__/DateTime.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ exports[`GraphQLDateTime literial parsing errors when parsing invalid literal {"

exports[`GraphQLDateTime literial parsing errors when parsing invalid literal {"kind": "StringValue", "value": "Invalid date"} 1`] = `"DateTime cannot represent an invalid date-time-string Invalid date."`;

exports[`GraphQLDateTime serialization throws an error serializing the invalid unix timestamp Infinity 1`] = `"DateTime cannot represent an invalid Unix timestamp Infinity"`;

exports[`GraphQLDateTime serialization throws an error serializing the invalid unix timestamp Infinity 2`] = `"DateTime cannot represent an invalid Unix timestamp Infinity"`;

exports[`GraphQLDateTime serialization throws an error serializing the invalid unix timestamp NaN 1`] = `"DateTime cannot represent an invalid Unix timestamp NaN"`;

exports[`GraphQLDateTime serialization throws an error when serializing an invalid date-string "2015-02-24T00:00:00.000+0100" 1`] = `"DateTime cannot represent an invalid date-time-string 2015-02-24T00:00:00.000+0100."`;

exports[`GraphQLDateTime serialization throws an error when serializing an invalid date-string "2016-02-01T00:00:00.Z" 1`] = `"DateTime cannot represent an invalid date-time-string 2016-02-01T00:00:00.Z."`;
Expand Down
55 changes: 6 additions & 49 deletions tests/iso-date/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import {
serializeTime,
serializeTimeString,
serializeDate,
serializeDateTime,
serializeDateTimeString,
serializeUnixTimestamp,
parseTime,
parseDate,
parseDateTime,
Expand Down Expand Up @@ -57,47 +55,6 @@ describe('formatting', () => {
});
});

([
[new Date(Date.UTC(2016, 1, 1)), '2016-02-01T00:00:00.000Z'],
[new Date(Date.UTC(2016, 3, 5, 10, 1, 4, 555)), '2016-04-05T10:01:04.555Z'],
] as [Date, string][]).forEach(([date, dateTimeString]) => {
it(`serializes ${stringify(
date,
)} into date-time-string ${dateTimeString}`, () => {
expect(serializeDateTime(date)).toEqual(dateTimeString);
});
});

([
[new Date(Date.UTC(2016, 1, 1)), '2016-02-01T00:00:00.000Z'],
[new Date(Date.UTC(2016, 3, 5, 10, 1, 4, 555)), '2016-04-05T10:01:04.555Z'],
] as [Date, string][]).forEach(([date, dateTimeString]) => {
it(`serializes ${stringify(
date,
)} into date-time-string ${dateTimeString}`, () => {
expect(serializeDateTime(date)).toEqual(dateTimeString);
});
});

([
[854325678, '1997-01-27T00:41:18.000Z'],
[876535, '1970-01-11T03:28:55.000Z'],
[876535.8, '1970-01-11T03:28:55.800Z'],
[876535.8321, '1970-01-11T03:28:55.832Z'],
[-876535.8, '1969-12-21T20:31:04.200Z'],
[0, '1970-01-01T00:00:00.000Z'],
// The maximum representable unix timestamp
[2147483647, '2038-01-19T03:14:07.000Z'],
// The minimum representable unit timestamp
[-2147483648, '1901-12-13T20:45:52.000Z'],
] as [number, string][]).forEach(([timestamp, dateTimeString]) => {
it(`serializes Unix timestamp ${stringify(
timestamp,
)} into date-time-string ${dateTimeString}`, () => {
expect(serializeUnixTimestamp(timestamp)).toEqual(dateTimeString);
});
});

([
['00:00:59Z', new Date(Date.UTC(2017, 0, 1, 0, 0, 59))],
['00:00:00+01:30', new Date(Date.UTC(2016, 11, 31, 22, 30))],
Expand Down Expand Up @@ -160,14 +117,14 @@ describe('formatting', () => {
});

[
['2016-02-01T00:00:00Z', '2016-02-01T00:00:00Z'],
['2016-02-01T12:23:44Z', '2016-02-01T12:23:44Z'],
['2016-02-01T14:38:12-01:00', '2016-02-01T15:38:12Z'],
['2016-02-02T00:00:00.4567+01:30', '2016-02-01T22:30:00.4567Z'],
['2016-02-01T14:38:12.1+01:00', '2016-02-01T13:38:12.1Z'],
['2016-02-01T00:00:00Z', '2016-02-01T00:00:00.000Z'],
['2016-02-01T12:23:44Z', '2016-02-01T12:23:44.000Z'],
['2016-02-01T14:38:12-01:00', '2016-02-01T15:38:12.000Z'],
['2016-02-02T00:00:00.456+01:30', '2016-02-01T22:30:00.456Z'],
['2016-02-01T14:38:12.1+01:00', '2016-02-01T13:38:12.100Z'],
].forEach(([input, output]) => {
it(`serializes date-time-string ${input} into UTC date-time-string ${output}`, () => {
expect(serializeDateTimeString(input)).toEqual(output);
expect(serializeDateTimeString(input).toJSON()).toEqual(output);
});
});
});

0 comments on commit 3b1352c

Please sign in to comment.