Skip to content

Commit

Permalink
Add Duration scalar based on ISO8601Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Dec 5, 2020
1 parent 74df67a commit b555900
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ NOTE: `BigInt` is also available under the alias `Long`.

NOTE: `UUID` is also available under the alias `GUID`.

NOTE: `Duration` is also available under the alias `ISO8601Duration`.

Alternatively, use the default import and ES6's spread operator syntax:

```javascript
Expand Down
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.5.0",
"version": "1.6.0",
"description": "A collection of scalar types not included in base GraphQL.",
"repository": {
"type": "git",
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
GraphQLObjectID,
GraphQLVoid,
} from './scalars';
import { GraphQLDuration } from './scalars/iso-date/Duration';

export {
Date as DateTypeDefinition,
Expand Down Expand Up @@ -109,6 +110,7 @@ export {
GraphQLDateTime as DateTimeResolver,
GraphQLTimestamp as TimestampResolver,
GraphQLUtcOffset as UtcOffsetResolver,
GraphQLDuration as DurationResolver,
GraphQLISO8601Duration as ISO8601DurationResolver,
GraphQLLocalDate as LocalDateResolver,
GraphQLLocalTime as LocalTimeResolver,
Expand Down Expand Up @@ -160,6 +162,7 @@ export const resolvers = {
DateTime: GraphQLDateTime,
Timestamp: GraphQLTimestamp,
UtcOffset: GraphQLUtcOffset,
Duration: GraphQLDuration,
ISO8601Duration: GraphQLISO8601Duration,
LocalDate: GraphQLLocalDate,
LocalTime: GraphQLLocalTime,
Expand Down Expand Up @@ -209,6 +212,7 @@ export {
Date as DateMock,
Time as TimeMock,
DateTime as DateTimeMock,
Duration as DurationMock,
ISO8601Duration as ISO8601DurationMock,
Timestamp as TimestampMock,
UtcOffset as UtcOffsetMock,
Expand Down Expand Up @@ -266,6 +270,7 @@ export {
GraphQLDateTime,
GraphQLTimestamp,
GraphQLUtcOffset,
GraphQLDuration,
GraphQLISO8601Duration,
GraphQLLocalDate,
GraphQLLocalTime,
Expand Down
3 changes: 2 additions & 1 deletion src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const Time = () => '10:15:30Z';
export const DateTime = () => '2007-12-03T10:15:30Z';
export const Timestamp = () => 1592577642;
export const UtcOffset = () => '+03:00';
export const ISO8601Duration = () => 'P3Y6M4DT12H30M5S';
export const Duration = () => 'P3Y6M4DT12H30M5S';
export const LocalDate = () => '2020-07-19';
export const LocalTime = () => '08:45:59';
export const LocalEndTime = () => '24:00:00';
Expand Down Expand Up @@ -100,4 +100,5 @@ export {
BigIntMock as Long,
BigIntMock as BigInt,
ByteMock as Byte,
Duration as ISO8601Duration,
};
24 changes: 21 additions & 3 deletions src/scalars/iso-date/Duration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { GraphQLError, GraphQLScalarType, Kind } from 'graphql';
import {
GraphQLError,
GraphQLScalarType,
GraphQLScalarTypeConfig,
Kind,
} from 'graphql';
export type ISO8601Duration = string;

// original implementation
Expand All @@ -9,8 +14,11 @@ export type ISO8601Duration = string;
// negative and positive durations allowed, commas and decimal points valid for fractions
const ISO_DURATION = /^(-|\+)?P(?!$)((-|\+)?\d+(?:(\.|,)\d+)?Y)?((-|\+)?\d+(?:(\.|,)\d+)?M)?((-|\+)?\d+(?:(\.|,)\d+)?W)?((-|\+)?\d+(?:(\.|,)\d+)?D)?(T(?=(-|\+)?\d)((-|\+)?\d+(?:(\.|,)\d+)?H)?((-|\+)?\d+(?:(\.|,)\d+)?M)?((-|\+)?\d+(?:(\.|,)\d+)?S)?)?$/;

export const GraphQLISO8601Duration = /*#__PURE__*/ new GraphQLScalarType({
name: 'ISO8601Duration',
export const GraphQLDurationConfig: GraphQLScalarTypeConfig<
string,
string
> = /*#__PURE__*/ {
name: 'Duration',
description: `
A string representing a duration conforming to the ISO8601 standard,
such as: P1W1DT13H23M34S
Expand Down Expand Up @@ -66,4 +74,14 @@ export const GraphQLISO8601Duration = /*#__PURE__*/ new GraphQLScalarType({

return ast.value;
},
};

export const GraphQLISO8601Duration = /*#__PURE__*/ new GraphQLScalarType({
...GraphQLDurationConfig,
name: 'ISO8601Duration',
});

export const GraphQLDuration = /*#__PURE__*/ new GraphQLScalarType({
...GraphQLDurationConfig,
name: 'Duration',
});
1 change: 1 addition & 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 DateTime = 'scalar DateTime';
export const UtcOffset = 'scalar UtcOffset';
export const Duration = 'scalar Duration';
export const ISO8601Duration = 'scalar ISO8601Duration';
export const LocalDate = 'scalar LocalDate';
export const LocalTime = 'scalar LocalTime';
Expand Down

0 comments on commit b555900

Please sign in to comment.