Skip to content

Commit

Permalink
New DateTimeISO scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed May 29, 2023
1 parent 9d350f5 commit 613d0b4
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/selfish-taxis-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-scalars': patch
---

Introduce DateTimeISO scalar
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
GraphQLCurrency,
GraphQLDate,
GraphQLDateTime,
GraphQLDateTimeISO,
GraphQLDeweyDecimal,
GraphQLDID,
GraphQLDuration,
GraphQLEmailAddress,
GraphQLGUID,
GraphQLHexadecimal,
Expand Down Expand Up @@ -66,12 +68,12 @@ import {
GraphQLUUID,
GraphQLVoid,
} from './scalars/index.js';
import { GraphQLDuration } from './scalars/iso-date/Duration.js';

export {
Date as DateTypeDefinition,
Time as TimeTypeDefinition,
DateTime as DateTimeTypeDefinition,
DateTimeISO as DateTimeISOTypeDefinition,
Timestamp as TimestampTypeDefinition,
TimeZone as TimeZoneTypeDefinition,
UtcOffset as UtcOffsetTypeDefinition,
Expand Down Expand Up @@ -141,6 +143,7 @@ export {
GraphQLDate as DateResolver,
GraphQLTime as TimeResolver,
GraphQLDateTime as DateTimeResolver,
GraphQLDateTimeISO as DateTimeISOResolver,
GraphQLTimestamp as TimestampResolver,
GraphQLTimeZone as TimeZoneResolver,
GraphQLUtcOffset as UtcOffsetResolver,
Expand Down Expand Up @@ -208,6 +211,7 @@ export const resolvers: Record<string, GraphQLScalarType> = {
Date: GraphQLDate,
Time: GraphQLTime,
DateTime: GraphQLDateTime,
DateTimeISO: GraphQLDateTimeISO,
Timestamp: GraphQLTimestamp,
TimeZone: GraphQLTimeZone,
UtcOffset: GraphQLUtcOffset,
Expand Down Expand Up @@ -276,6 +280,7 @@ export {
Date as DateMock,
Time as TimeMock,
DateTime as DateTimeMock,
DateTimeISO as DateTimeISOMock,
Duration as DurationMock,
ISO8601Duration as ISO8601DurationMock,
Timestamp as TimestampMock,
Expand Down Expand Up @@ -352,6 +357,7 @@ export {
GraphQLDate,
GraphQLTime,
GraphQLDateTime,
GraphQLDateTimeISO,
GraphQLTimestamp,
GraphQLTimeZone,
GraphQLUtcOffset,
Expand Down
1 change: 1 addition & 0 deletions src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const ByteMock = () => new Uint8Array([1988, 1981, 1965, 1963, 1959, 1955]);
const DateMock = () => '2007-12-03';
export const Time = () => '10:15:30Z';
export const DateTime = () => '2007-12-03T10:15:30Z';
export const DateTimeISO = () => '2007-12-03T10:15:30Z';
export const Timestamp = () => 1592577642;
export const TimeZone = () => 'Etc/UTC';
export const UtcOffset = () => '+03:00';
Expand Down
2 changes: 2 additions & 0 deletions src/scalars/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export { GraphQLDate } from './iso-date/Date.js';
export { GraphQLTime } from './iso-date/Time.js';
export { GraphQLDateTime } from './iso-date/DateTime.js';
export { GraphQLDateTimeISO } from './iso-date/DateTimeISO.js';
export { GraphQLDuration } from './iso-date/Duration.js';
export { GraphQLTimestamp } from './Timestamp.js';
export { GraphQLTimeZone } from './TimeZone.js';
export { GraphQLUtcOffset } from './UtcOffset.js';
Expand Down
19 changes: 19 additions & 0 deletions src/scalars/iso-date/DateTimeISO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql';
import { GraphQLDateTimeConfig } from './DateTime.js';

export const GraphQLDateTimeISOConfig: GraphQLScalarTypeConfig<Date, string> = /*#__PURE__*/ {
...GraphQLDateTimeConfig,
name: 'DateTimeISO',
description:
'A date-time string at UTC, such as 2007-12-03T10:15:30Z, ' +
'compliant with the `date-time` format outlined in section 5.6 of ' +
'the RFC 3339 profile of the ISO 8601 standard for representation ' +
'of dates and times using the Gregorian calendar.' +
'This scalar is serialized to a string in ISO 8601 format and parsed from a string in ISO 8601 format.',
serialize(value) {
const date = GraphQLDateTimeConfig.serialize(value);
return date.toISOString();
},
};

export const GraphQLDateTimeISO = /*#__PURE__*/ new GraphQLScalarType(GraphQLDateTimeISOConfig);
2 changes: 2 additions & 0 deletions src/typeDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const Time = 'scalar Time';
export const Timestamp = 'scalar Timestamp';
export const TimeZone = 'scalar TimeZone';
export const DateTime = 'scalar DateTime';
export const DateTimeISO = 'scalar DateTimeISO';
export const UtcOffset = 'scalar UtcOffset';
export const Duration = 'scalar Duration';
export const ISO8601Duration = 'scalar ISO8601Duration';
Expand Down Expand Up @@ -72,6 +73,7 @@ export const typeDefs = [
Date,
Time,
DateTime,
DateTimeISO,
Timestamp,
TimeZone,
UtcOffset,
Expand Down
5 changes: 5 additions & 0 deletions website/src/pages/docs/scalars/date-time.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ When expected as an input type, only RFC 3339 compliant date-time strings are ac
query error indicating an incorrect type.

<Callout>Taken from [graphql-iso-date](https://github.com/excitement-engineer/graphql-iso-date)</Callout>

<Callout>
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).
But if you want to get the value serialized into an ISO string, you can use the `DateTimeISO` scalar.
</Callout>

0 comments on commit 613d0b4

Please sign in to comment.