Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
215 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'graphql-scalars': minor | ||
--- | ||
|
||
New `LocalDateTimeString` scalar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { ASTNode, GraphQLScalarType, GraphQLScalarTypeConfig, Kind } from 'graphql'; | ||
import { createGraphQLError } from '../error.js'; | ||
|
||
const LOCAL_DATE_TIME_REGEX = | ||
/^((?:(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}(?:\.\d+)?))(Z|[\+-]\d{2}:\d{2})?)$/; | ||
|
||
function validateLocalDateTime(value: any, ast?: ASTNode): string { | ||
if (typeof value !== 'string') { | ||
throw createGraphQLError( | ||
`Value is not string: ${value}`, | ||
ast | ||
? { | ||
nodes: [ast], | ||
} | ||
: undefined, | ||
); | ||
} | ||
if (!LOCAL_DATE_TIME_REGEX.test(value.toUpperCase())) { | ||
throw createGraphQLError( | ||
`LocalDateTime cannot represent an invalid local date-time-string ${value}.`, | ||
ast | ||
? { | ||
nodes: [ast], | ||
} | ||
: undefined, | ||
); | ||
} | ||
const valueAsDate = new Date(value); | ||
const isValidDate = !isNaN(valueAsDate.getTime()); | ||
if (!isValidDate) { | ||
throw createGraphQLError( | ||
`Value is not a valid LocalDateTime: ${value}`, | ||
ast | ||
? { | ||
nodes: [ast], | ||
} | ||
: undefined, | ||
); | ||
} | ||
return value; | ||
} | ||
|
||
export const LocalDateTimeConfig: GraphQLScalarTypeConfig<string, string> = /*#__PURE__*/ { | ||
name: 'LocalDateTime', | ||
description: | ||
'A local date-time string (i.e., with no associated timezone) in `YYYY-MM-DDTHH:mm:ss` format, e.g. `2020-01-01T00:00:00`.', | ||
serialize(value) { | ||
return validateLocalDateTime(value); | ||
}, | ||
parseValue(value) { | ||
return validateLocalDateTime(value); | ||
}, | ||
parseLiteral(ast) { | ||
if (ast.kind !== Kind.STRING) { | ||
throw createGraphQLError( | ||
`Can only validate strings as local date-times but got a: ${ast.kind}`, | ||
{ | ||
nodes: [ast], | ||
}, | ||
); | ||
} | ||
return validateLocalDateTime(ast.value, ast); | ||
}, | ||
extensions: { | ||
codegenScalarType: | ||
// eslint-disable-next-line no-template-curly-in-string | ||
'`${number}${number}${number}${number}-${number}${number}-${number}${number}T${number}${number}:${number}${number}:${number}${number}`', | ||
jsonSchema: { | ||
title: 'LocalDateTime', | ||
type: 'string', | ||
format: 'date-time', | ||
}, | ||
}, | ||
}; | ||
|
||
export const GraphQLLocalDateTime = /*#__PURE__*/ new GraphQLScalarType(LocalDateTimeConfig); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { Kind } from 'graphql'; | ||
import { GraphQLLocalDateTime } from '../src'; | ||
|
||
const VALID_LOCAL_DATE_TIMES = [ | ||
'2016-02-01T00:00:15Z', | ||
'2016-02-01T00:00:00-11:00', | ||
'2017-01-07T11:25:00+01:00', | ||
'2017-01-07T00:00:00+01:20', | ||
'2016-02-01T00:00:00.1Z', | ||
'2016-02-01T00:00:00.000Z', | ||
'2016-02-01T00:00:00.990Z', | ||
'2016-02-01T00:00:00.23498Z', | ||
'2017-01-07T11:25:00.450+01:00', | ||
'2016-02-01t00:00:00.000z', | ||
]; | ||
|
||
describe(`LocalDateTime`, () => { | ||
describe('valid', () => { | ||
it('serialize', () => { | ||
VALID_LOCAL_DATE_TIMES.forEach(value => { | ||
expect(GraphQLLocalDateTime.serialize(value)).toEqual(value); | ||
}); | ||
}); | ||
|
||
it('parseValue', () => { | ||
VALID_LOCAL_DATE_TIMES.forEach(value => { | ||
expect(GraphQLLocalDateTime.parseValue(value)).toEqual(value); | ||
}); | ||
}); | ||
|
||
it('parseLiteral', () => { | ||
VALID_LOCAL_DATE_TIMES.forEach(value => { | ||
expect( | ||
GraphQLLocalDateTime.parseLiteral( | ||
{ | ||
value, | ||
kind: Kind.STRING, | ||
}, | ||
{}, | ||
), | ||
).toEqual(value); | ||
}); | ||
}); | ||
}); | ||
describe('invalid', () => { | ||
describe('not string', () => { | ||
it('serialize', () => { | ||
expect(() => GraphQLLocalDateTime.serialize(123)).toThrow(/Value is not string/); | ||
expect(() => GraphQLLocalDateTime.serialize(false)).toThrow(/Value is not string/); | ||
}); | ||
it('parseValue', () => { | ||
expect(() => GraphQLLocalDateTime.parseValue(123)).toThrow(/Value is not string/); | ||
expect(() => GraphQLLocalDateTime.parseValue(false)).toThrow(/Value is not string/); | ||
}); | ||
it('parseLiteral', () => { | ||
expect(() => | ||
GraphQLLocalDateTime.parseLiteral({ value: 123 as any, kind: Kind.INT }, {}), | ||
).toThrow(/Can only validate strings as local date-times but got a: IntValue/); | ||
expect(() => | ||
GraphQLLocalDateTime.parseLiteral({ value: false, kind: Kind.BOOLEAN }, {}), | ||
).toThrow(/Can only validate strings as local date-times but got a: BooleanValue/); | ||
}); | ||
}); | ||
describe('not a valid local date time string', () => { | ||
const invalidDateTime = '2015-02-24T00:00:00.000+0100'; | ||
it('serialize', () => { | ||
expect(() => GraphQLLocalDateTime.serialize(invalidDateTime)).toThrow( | ||
/LocalDateTime cannot represent an invalid local date-time-string/, | ||
); | ||
}); | ||
it('parseValue', () => { | ||
expect(() => GraphQLLocalDateTime.parseValue(invalidDateTime)).toThrow( | ||
/LocalDateTime cannot represent an invalid local date-time-string/, | ||
); | ||
}); | ||
it('parseLiteral', () => { | ||
expect(() => | ||
GraphQLLocalDateTime.parseLiteral({ value: invalidDateTime, kind: Kind.STRING }, {}), | ||
).toThrow(/LocalDateTime cannot represent an invalid local date-time-string/); | ||
}); | ||
}); | ||
describe('invalid date', () => { | ||
const invalidDateTime = '2017-13-46T11:25:00.450+01:00'; | ||
it('serialize', () => { | ||
expect(() => GraphQLLocalDateTime.serialize(invalidDateTime)).toThrow( | ||
/Value is not a valid LocalDateTime/, | ||
); | ||
}); | ||
it('parseValue', () => { | ||
expect(() => GraphQLLocalDateTime.parseValue(invalidDateTime)).toThrow( | ||
/Value is not a valid LocalDateTime/, | ||
); | ||
}); | ||
it('parseLiteral', () => { | ||
expect(() => | ||
GraphQLLocalDateTime.parseLiteral({ value: invalidDateTime, kind: Kind.STRING }, {}), | ||
).toThrow(/Value is not a valid LocalDateTime/); | ||
}); | ||
}); | ||
}); | ||
}); |