UNPKG

simple-graphql

Version:

The simple way to generates GraphQL schemas and Sequelize models from your models definition.

51 lines 1.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const graphql_1 = require("graphql"); exports.default = new graphql_1.GraphQLScalarType({ name: 'Date', serialize(value) { if (typeof value === 'string') { const dataTime = Date.parse(value); if (isNaN(dataTime)) { throw new TypeError('value is not an instance of Date'); } else { value = new Date(dataTime); } } if (!(value instanceof Date)) { throw new TypeError('value is not an instance of Date'); } if (isNaN(value.getTime())) { throw new TypeError('value is an invalid Date'); } return value.toJSON(); }, parseValue(value) { if (typeof value === 'string') { const dataTime = Date.parse(value); if (isNaN(dataTime)) { throw new TypeError('Invalid date'); } else { return new Date(dataTime); } } else { throw new graphql_1.GraphQLError('Invalid date'); } }, parseLiteral(ast) { if (ast.kind !== graphql_1.Kind.STRING) { throw new graphql_1.GraphQLError('Can only parse strings to dates but got a: ' + ast.kind, [ast]); } const dataTime = Date.parse(ast.value); if (isNaN(dataTime)) { throw new TypeError('Invalid date'); } else { return new Date(dataTime); } } }); //# sourceMappingURL=Date.js.map