From b02df6d6adf64b3d270285213c8013bf727d8cf0 Mon Sep 17 00:00:00 2001 From: Robert Herber Date: Wed, 13 Sep 2023 22:38:23 +0200 Subject: [PATCH] fix parsing of scalar --- src/graphql/UniversalDateTimeScalar.test.ts | 10 +++++++++- src/graphql/UniversalDateTimeScalar.ts | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/graphql/UniversalDateTimeScalar.test.ts b/src/graphql/UniversalDateTimeScalar.test.ts index ffd7486..34491e9 100644 --- a/src/graphql/UniversalDateTimeScalar.test.ts +++ b/src/graphql/UniversalDateTimeScalar.test.ts @@ -4,7 +4,7 @@ import utc from 'dayjs/plugin/utc' import { GraphQLError } from 'graphql/error' import { Kind } from 'graphql/language' -import { UniversalDateTime } from './UniversalDateTimeScalar' +import { UniversalDateTime, isValidDayjs } from './UniversalDateTimeScalar' dayjs.extend(utc) dayjs.extend(timezone) @@ -85,4 +85,12 @@ describe('UniversalDateTime', () => { expect(date).toBe(expected) }) + + test('parseLiteral should return dayjs', () => { + const expected = '2019-11-01T11:00:00Z' + const date = parseLiteral({ value: expected, kind: Kind.STRING }, {}) as dayjs.Dayjs + + expect(date.valueOf()).toBe(new Date(expected).valueOf()) + expect(date.isValid()).toBe(true) + }) }) diff --git a/src/graphql/UniversalDateTimeScalar.ts b/src/graphql/UniversalDateTimeScalar.ts index 0fb8701..d7e221e 100644 --- a/src/graphql/UniversalDateTimeScalar.ts +++ b/src/graphql/UniversalDateTimeScalar.ts @@ -59,7 +59,7 @@ export const UniversalDateTime = new GraphQLScalarType({ if (typeof ast.value === 'string' && isValidDateString(ast.value)) { return ast.value } if (isValidDayjs(ast.value)) { - return ast.value + return stringToDayjs(ast.value) } throw new GraphQLError(`Require string on format YYYY-MM-DD or iso8601/dayjs valid string, found: ${ast.value}`) },