UNPKG

@backland/schema

Version:

TypeScript schema declaration and validation library with static type inference

36 lines (35 loc) 1.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GraphQLDateType = void 0; var _graphql = require("graphql"); var _DateField = require("../fields/DateField"); var GraphQLDateType = new _graphql.GraphQLScalarType({ name: 'Date', parseLiteral: function 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: ".concat(ast.kind), [ast]); } var result = new Date(ast.value); if (Number.isNaN(result.getTime())) { throw new _graphql.GraphQLError('Query error: Invalid date', [ast]); } return result; }, parseValue: function parseValue(value) { var date = new Date(value); if (Number.isNaN(date.getTime())) { throw new TypeError('Field error: value is an invalid Date'); } return date; }, serialize: function serialize(value) { return _DateField.DateField.serialize(value).toJSON(); } }); exports.GraphQLDateType = GraphQLDateType; //# sourceMappingURL=GraphQLDateType.js.map