graphql-extra-scalars
Version:
Collection of extra GraphQL scalar types like Email, URL, Password and more
29 lines (28 loc) • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const luxon_1 = require("luxon");
const graphql_1 = require("graphql");
const literalParser_1 = require("../literalParser");
const coerceType = (value) => {
if (typeof value !== 'string') {
throw new TypeError(`DateTime cannot represent a non string value: [${String(value)}]`);
}
const datetime = luxon_1.DateTime.fromISO(value);
if (!datetime.isValid && datetime.invalidExplanation != null) {
throw new TypeError(datetime.invalidExplanation);
}
return datetime;
};
const GraphQLDateTime = new graphql_1.GraphQLScalarType({
name: 'DateTime',
description: 'The DateTime scalar type represents date time strings complying to ISO-8601.',
serialize: (value) => {
if (!luxon_1.DateTime.isDateTime(value)) {
throw new TypeError(`DateTime cannot represent a non DateTime value: [${String(typeof value)}] given`);
}
return value.toISO();
},
parseValue: coerceType,
parseLiteral: literalParser_1.createParseLiteral(coerceType),
});
exports.GraphQLDateTime = GraphQLDateTime;