UNPKG

mgs-graphql

Version:

The simple way to generates GraphQL schemas and Sequelize models from your models definition,microservice supported

51 lines (45 loc) 1.49 kB
// @flow const {GraphQLScalarType, GraphQLError, Kind} = require('graphql') const moment = require('moment') module.exports = new GraphQLScalarType({ name: 'Date', serialize (value) { if (typeof value === 'string') { const nValue = moment(value, 'YYYY-MM-DD HH:mm:ss.SSS ZZ') if (nValue.isValid()) { value = nValue.toDate() } } if (!(value instanceof Date)) { throw new TypeError('Field error: value is not an instance of Date') } if (isNaN(value.getTime())) { throw new TypeError('Field error: value is an invalid Date') } return value.toJSON() }, parseValue (value) { if (typeof value === 'string') { const result = moment(value) if (!result.isValid()) { throw new GraphQLError('Query error: Invalid date') } return result.toDate() } else { throw new GraphQLError('Query error: Invalid date') } }, parseLiteral (ast) { if (ast.kind !== Kind.STRING) { throw new GraphQLError('Query error: Can only parse strings to dates but got a: ' + ast.kind, [ast]) } const result = moment(ast.value) if (!result.isValid()) { throw new GraphQLError('Query error: Invalid date', [ast]) } return result.toDate() // if (ast.value !== result.toJSON()) { // throw new GraphQLError('Query error: Invalid date format, only accepts: YYYY-MM-DDTHH:MM:SS.SSSZ', [ast]) // } } })