@backland/schema
Version:
TypeScript schema declaration and validation library with static type inference
36 lines (35 loc) • 1.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.GraphQLDateType = void 0;
var _graphql = require("graphql");
var _DateField = require("../fields/DateField");
const GraphQLDateType = new _graphql.GraphQLScalarType({
name: 'Date',
parseLiteral(ast) {
if (ast.kind === _graphql.Kind.INT) {
return new Date(parseInt(ast.value, 10));
}
if (ast.kind !== _graphql.Kind.STRING) {
throw new _graphql.GraphQLError(`Query error: Can only parse string or integer to Date but got a: ${ast.kind}`, [ast]);
}
const result = new Date(ast.value);
if (Number.isNaN(result.getTime())) {
throw new _graphql.GraphQLError('Query error: Invalid date', [ast]);
}
return result;
},
parseValue(value) {
const date = new Date(value);
if (Number.isNaN(date.getTime())) {
throw new TypeError('Field error: value is an invalid Date');
}
return date;
},
serialize(value) {
return _DateField.DateField.serialize(value).toJSON();
}
});
exports.GraphQLDateType = GraphQLDateType;
//# sourceMappingURL=GraphQLDateType.js.map