@nestjs/graphql
Version:
GraphQL for NestJS: code-first and schema-first APIs, resolvers as providers, subscriptions, federation and generated SDL, on Apollo or Mercurius.
35 lines (34 loc) • 1.44 kB
JavaScript
import { GraphQLError, GraphQLScalarType, Kind, print, } from 'graphql';
// graphql-js treats only a thrown error (or "undefined") as invalid input, so
// anything this scalar cannot represent must throw rather than return "null"
// or an "Invalid Date". The rejected value is not echoed back: graphql-js
// already prefixes a truncated form of it.
function coerceDateTime(value) {
// Numbers (epoch milliseconds) are accepted for backward compatibility.
if (typeof value !== 'string' &&
typeof value !== 'number' &&
!(value instanceof Date)) {
throw new GraphQLError('DateTime cannot represent non-string value.');
}
const date = new Date(value);
if (Number.isNaN(date.getTime())) {
throw new GraphQLError('DateTime cannot represent an invalid date-time value.');
}
return date;
}
export const GraphQLISODateTime = new GraphQLScalarType({
name: 'DateTime',
description: 'A date-time string at UTC, such as 2019-12-03T09:54:33Z, compliant with the date-time format.',
parseValue(value) {
return coerceDateTime(value);
},
serialize(value) {
return value instanceof Date ? value.toISOString() : null;
},
parseLiteral(ast) {
if (ast.kind !== Kind.STRING) {
throw new GraphQLError(`DateTime cannot represent non-string value: ${print(ast)}`, { nodes: ast });
}
return coerceDateTime(ast.value);
},
});