Skip to content

Commit dc0c2bd

Browse files
committed
Support JS Date input for DateTime scalar
1 parent 24ca4f4 commit dc0c2bd

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

src/scalars/iso-date/DateTime.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,30 @@ const config: GraphQLScalarTypeConfig<Date, Date> = {
6161
}
6262
},
6363
parseValue(value) {
64-
if (!(typeof value === 'string')) {
64+
if (value instanceof Date) {
65+
if (validateJSDate(value)) {
66+
return value;
67+
}
68+
throw new TypeError('DateTime cannot represent an invalid Date instance');
69+
}
70+
if (typeof value === 'string') {
71+
if (validateDateTime(value)) {
72+
return parseDateTime(value);
73+
}
6574
throw new TypeError(
66-
`DateTime cannot represent non string type ${JSON.stringify(value)}`,
75+
`DateTime cannot represent an invalid date-time-string ${value}.`,
6776
);
6877
}
69-
70-
if (validateDateTime(value)) {
71-
return parseDateTime(value);
72-
}
7378
throw new TypeError(
74-
`DateTime cannot represent an invalid date-time-string ${value}.`,
79+
`DateTime cannot represent non string or Date type ${JSON.stringify(
80+
value,
81+
)}`,
7582
);
7683
},
7784
parseLiteral(ast) {
7885
if (ast.kind !== Kind.STRING) {
7986
throw new TypeError(
80-
`DateTime cannot represent non string type ${
87+
`DateTime cannot represent non string or Date type ${
8188
'value' in ast && ast.value
8289
}`,
8390
);

tests/iso-date/DateTime.integration.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ it('errors if the variable value is not of type string', async () => {
206206
expect(response).toEqual({
207207
errors: [
208208
new GraphQLError(
209-
'Variable "$date" got invalid value 4; Expected type "DateTime". DateTime cannot represent non string type 4',
209+
'Variable "$date" got invalid value 4; Expected type "DateTime". DateTime cannot represent non string or Date type 4',
210210
),
211211
],
212212
});
@@ -242,7 +242,7 @@ it('errors if the literal input value in a query is not a string', async () => {
242242
expect(response).toEqual({
243243
errors: [
244244
new GraphQLError(
245-
'Expected value of type "DateTime", found 4; DateTime cannot represent non string type 4',
245+
'Expected value of type "DateTime", found 4; DateTime cannot represent non string or Date type 4',
246246
),
247247
],
248248
});

tests/iso-date/__snapshots__/DateTime.test.ts.snap

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
exports[`GraphQLDateTime has a description 1`] = `"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."`;
44

5-
exports[`GraphQLDateTime literial parsing errors when parsing invalid literal {"kind": "Document"} 1`] = `"DateTime cannot represent non string type false"`;
5+
exports[`GraphQLDateTime literial parsing errors when parsing invalid literal {"kind": "Document"} 1`] = `"DateTime cannot represent non string or Date type false"`;
66

7-
exports[`GraphQLDateTime literial parsing errors when parsing invalid literal {"kind": "FloatValue", "value": "5"} 1`] = `"DateTime cannot represent non string type 5"`;
7+
exports[`GraphQLDateTime literial parsing errors when parsing invalid literal {"kind": "FloatValue", "value": "5"} 1`] = `"DateTime cannot represent non string or Date type 5"`;
88

99
exports[`GraphQLDateTime literial parsing errors when parsing invalid literal {"kind": "StringValue", "value": "2015-02-24T00:00:00.000+0100"} 1`] = `"DateTime cannot represent an invalid date-time-string 2015-02-24T00:00:00.000+0100."`;
1010

@@ -54,12 +54,12 @@ exports[`GraphQLDateTime value parsing throws an error parsing an invalid date-s
5454

5555
exports[`GraphQLDateTime value parsing throws an error parsing an invalid date-string "Invalid date" 1`] = `"DateTime cannot represent an invalid date-time-string Invalid date."`;
5656

57-
exports[`GraphQLDateTime value parsing throws an error when parsing [] 1`] = `"DateTime cannot represent non string type []"`;
57+
exports[`GraphQLDateTime value parsing throws an error when parsing [] 1`] = `"DateTime cannot represent non string or Date type []"`;
5858

59-
exports[`GraphQLDateTime value parsing throws an error when parsing {} 1`] = `"DateTime cannot represent non string type {}"`;
59+
exports[`GraphQLDateTime value parsing throws an error when parsing {} 1`] = `"DateTime cannot represent non string or Date type {}"`;
6060

61-
exports[`GraphQLDateTime value parsing throws an error when parsing 4566 1`] = `"DateTime cannot represent non string type 4566"`;
61+
exports[`GraphQLDateTime value parsing throws an error when parsing 4566 1`] = `"DateTime cannot represent non string or Date type 4566"`;
6262

63-
exports[`GraphQLDateTime value parsing throws an error when parsing null 1`] = `"DateTime cannot represent non string type null"`;
63+
exports[`GraphQLDateTime value parsing throws an error when parsing null 1`] = `"DateTime cannot represent non string or Date type null"`;
6464

65-
exports[`GraphQLDateTime value parsing throws an error when parsing true 1`] = `"DateTime cannot represent non string type true"`;
65+
exports[`GraphQLDateTime value parsing throws an error when parsing true 1`] = `"DateTime cannot represent non string or Date type true"`;

0 commit comments

Comments
 (0)