Skip to content

Commit

Permalink
fix parsing of scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
robertherber committed Sep 13, 2023
1 parent 4909556 commit b02df6d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/graphql/UniversalDateTimeScalar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Check warning on line 7 in src/graphql/UniversalDateTimeScalar.test.ts

View workflow job for this annotation

GitHub Actions / test / lint / lint

'isValidDayjs' is defined but never used

dayjs.extend(utc)
dayjs.extend(timezone)
Expand Down Expand Up @@ -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)
})
})
2 changes: 1 addition & 1 deletion src/graphql/UniversalDateTimeScalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
},
Expand Down

0 comments on commit b02df6d

Please sign in to comment.