Skip to content

Commit 613d0b4

Browse files
committed
New DateTimeISO scalar
1 parent 9d350f5 commit 613d0b4

File tree

7 files changed

+41
-1
lines changed

7 files changed

+41
-1
lines changed

.changeset/selfish-taxis-smell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'graphql-scalars': patch
3+
---
4+
5+
Introduce DateTimeISO scalar

src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import {
99
GraphQLCurrency,
1010
GraphQLDate,
1111
GraphQLDateTime,
12+
GraphQLDateTimeISO,
1213
GraphQLDeweyDecimal,
1314
GraphQLDID,
15+
GraphQLDuration,
1416
GraphQLEmailAddress,
1517
GraphQLGUID,
1618
GraphQLHexadecimal,
@@ -66,12 +68,12 @@ import {
6668
GraphQLUUID,
6769
GraphQLVoid,
6870
} from './scalars/index.js';
69-
import { GraphQLDuration } from './scalars/iso-date/Duration.js';
7071

7172
export {
7273
Date as DateTypeDefinition,
7374
Time as TimeTypeDefinition,
7475
DateTime as DateTimeTypeDefinition,
76+
DateTimeISO as DateTimeISOTypeDefinition,
7577
Timestamp as TimestampTypeDefinition,
7678
TimeZone as TimeZoneTypeDefinition,
7779
UtcOffset as UtcOffsetTypeDefinition,
@@ -141,6 +143,7 @@ export {
141143
GraphQLDate as DateResolver,
142144
GraphQLTime as TimeResolver,
143145
GraphQLDateTime as DateTimeResolver,
146+
GraphQLDateTimeISO as DateTimeISOResolver,
144147
GraphQLTimestamp as TimestampResolver,
145148
GraphQLTimeZone as TimeZoneResolver,
146149
GraphQLUtcOffset as UtcOffsetResolver,
@@ -208,6 +211,7 @@ export const resolvers: Record<string, GraphQLScalarType> = {
208211
Date: GraphQLDate,
209212
Time: GraphQLTime,
210213
DateTime: GraphQLDateTime,
214+
DateTimeISO: GraphQLDateTimeISO,
211215
Timestamp: GraphQLTimestamp,
212216
TimeZone: GraphQLTimeZone,
213217
UtcOffset: GraphQLUtcOffset,
@@ -276,6 +280,7 @@ export {
276280
Date as DateMock,
277281
Time as TimeMock,
278282
DateTime as DateTimeMock,
283+
DateTimeISO as DateTimeISOMock,
279284
Duration as DurationMock,
280285
ISO8601Duration as ISO8601DurationMock,
281286
Timestamp as TimestampMock,
@@ -352,6 +357,7 @@ export {
352357
GraphQLDate,
353358
GraphQLTime,
354359
GraphQLDateTime,
360+
GraphQLDateTimeISO,
355361
GraphQLTimestamp,
356362
GraphQLTimeZone,
357363
GraphQLUtcOffset,

src/mocks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const ByteMock = () => new Uint8Array([1988, 1981, 1965, 1963, 1959, 1955]);
33
const DateMock = () => '2007-12-03';
44
export const Time = () => '10:15:30Z';
55
export const DateTime = () => '2007-12-03T10:15:30Z';
6+
export const DateTimeISO = () => '2007-12-03T10:15:30Z';
67
export const Timestamp = () => 1592577642;
78
export const TimeZone = () => 'Etc/UTC';
89
export const UtcOffset = () => '+03:00';

src/scalars/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export { GraphQLDate } from './iso-date/Date.js';
22
export { GraphQLTime } from './iso-date/Time.js';
33
export { GraphQLDateTime } from './iso-date/DateTime.js';
4+
export { GraphQLDateTimeISO } from './iso-date/DateTimeISO.js';
5+
export { GraphQLDuration } from './iso-date/Duration.js';
46
export { GraphQLTimestamp } from './Timestamp.js';
57
export { GraphQLTimeZone } from './TimeZone.js';
68
export { GraphQLUtcOffset } from './UtcOffset.js';

src/scalars/iso-date/DateTimeISO.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql';
2+
import { GraphQLDateTimeConfig } from './DateTime.js';
3+
4+
export const GraphQLDateTimeISOConfig: GraphQLScalarTypeConfig<Date, string> = /*#__PURE__*/ {
5+
...GraphQLDateTimeConfig,
6+
name: 'DateTimeISO',
7+
description:
8+
'A date-time string at UTC, such as 2007-12-03T10:15:30Z, ' +
9+
'compliant with the `date-time` format outlined in section 5.6 of ' +
10+
'the RFC 3339 profile of the ISO 8601 standard for representation ' +
11+
'of dates and times using the Gregorian calendar.' +
12+
'This scalar is serialized to a string in ISO 8601 format and parsed from a string in ISO 8601 format.',
13+
serialize(value) {
14+
const date = GraphQLDateTimeConfig.serialize(value);
15+
return date.toISOString();
16+
},
17+
};
18+
19+
export const GraphQLDateTimeISO = /*#__PURE__*/ new GraphQLScalarType(GraphQLDateTimeISOConfig);

src/typeDefs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const Time = 'scalar Time';
55
export const Timestamp = 'scalar Timestamp';
66
export const TimeZone = 'scalar TimeZone';
77
export const DateTime = 'scalar DateTime';
8+
export const DateTimeISO = 'scalar DateTimeISO';
89
export const UtcOffset = 'scalar UtcOffset';
910
export const Duration = 'scalar Duration';
1011
export const ISO8601Duration = 'scalar ISO8601Duration';
@@ -72,6 +73,7 @@ export const typeDefs = [
7273
Date,
7374
Time,
7475
DateTime,
76+
DateTimeISO,
7577
Timestamp,
7678
TimeZone,
7779
UtcOffset,

website/src/pages/docs/scalars/date-time.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ When expected as an input type, only RFC 3339 compliant date-time strings are ac
2424
query error indicating an incorrect type.
2525

2626
<Callout>Taken from [graphql-iso-date](https://github.com/excitement-engineer/graphql-iso-date)</Callout>
27+
28+
<Callout>
29+
This scalar serializes into [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) and parses into [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date).
30+
But if you want to get the value serialized into an ISO string, you can use the `DateTimeISO` scalar.
31+
</Callout>

0 commit comments

Comments
 (0)