@prodigyems/graphql-sequelize
Version:
GraphQL & Relay for MySQL & Postgres via Sequelize
54 lines (49 loc) • 1.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _graphql = require('graphql');
/**
* A special custom Scalar type for Dates that converts to a ISO formatted string
* @param {String} options.name:
* @param {String} options.description:
* @param {Date} options.serialize(d)
* @param {String} parseValue(value)
* @param {Object} parseLiteral(ast)
*/
exports.default = new _graphql.GraphQLScalarType({
name: 'Date',
description: 'A special custom Scalar type for Dates that converts to a ISO formatted string ',
/**
* serialize
* @param {Date} d Date obj
* @return {String} Serialised date object
*/
serialize: function serialize(d) {
if (!d) {
return null;
}
if (d instanceof Date) {
return d.toISOString();
}
return d;
},
/**
* parseValue
* @param {String} value date string
* @return {Date} Date object
*/
parseValue: function parseValue(value) {
try {
if (!value) {
return null;
}
return new Date(value);
} catch (e) {
return null;
}
},
parseLiteral: function parseLiteral(ast) {
return new Date(ast.value);
}
});