From b92521c1aa002e0a82b5101b46b3d0d90af0e404 Mon Sep 17 00:00:00 2001 From: Richard Ayotte Date: Tue, 17 Jul 2018 06:57:53 -0400 Subject: [PATCH] ISO date format support (#621) * ISO date format support * Remove yarn.lock * Revert DATEONLY to String and add DateType to src/index * Missed the DATEONLY test description on revert. --- src/index.js | 1 + src/typeMapper.js | 22 ++++++++------ src/types/dateType.js | 49 +++++++++++++++++++++++++++++++ test/unit/attributeFields.test.js | 4 ++- test/unit/defaultArgs.test.js | 3 +- test/unit/typeMapper.test.js | 5 ++-- 6 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 src/types/dateType.js diff --git a/src/index.js b/src/index.js index fbf9dd11..da949697 100644 --- a/src/index.js +++ b/src/index.js @@ -12,4 +12,5 @@ module.exports = { createConnectionResolver: require('./relay').createConnectionResolver, createNodeInterface: require('./relay').createNodeInterface, JSONType: require('./types/jsonType'), + DateType: require('./types/DateType'), }; diff --git a/src/typeMapper.js b/src/typeMapper.js index 0a9d2a2e..c688c963 100644 --- a/src/typeMapper.js +++ b/src/typeMapper.js @@ -1,11 +1,13 @@ import { - GraphQLInt, - GraphQLString, - GraphQLBoolean, - GraphQLFloat, - GraphQLEnumType, - GraphQLList - } from 'graphql'; + GraphQLInt, + GraphQLString, + GraphQLBoolean, + GraphQLFloat, + GraphQLEnumType, + GraphQLList, +} from 'graphql'; + +import DateType from './types/dateType'; import JSONType from './types/jsonType'; import _ from 'lodash'; @@ -18,7 +20,6 @@ export function mapType(mapFunc) { customTypeMapper = mapFunc; } - /** * Checks the type of the sequelize data type and * returns the corresponding type in GraphQL @@ -68,11 +69,14 @@ export function toGraphQL(sequelizeType, sequelizeTypes) { if (sequelizeType instanceof FLOAT || sequelizeType instanceof DOUBLE) return GraphQLFloat; + if (sequelizeType instanceof DATE) { + return DateType; + } + if (sequelizeType instanceof CHAR || sequelizeType instanceof STRING || sequelizeType instanceof TEXT || sequelizeType instanceof UUID || - sequelizeType instanceof DATE || sequelizeType instanceof DATEONLY || sequelizeType instanceof TIME || sequelizeType instanceof BIGINT || diff --git a/src/types/dateType.js b/src/types/dateType.js new file mode 100644 index 00000000..e77da5a9 --- /dev/null +++ b/src/types/dateType.js @@ -0,0 +1,49 @@ +import { + GraphQLScalarType +} from '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) + */ +export default new 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(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(value) { + try { + if (!value) { + return null; + } + return new Date(value); + } catch (e) { + return null; + } + }, + parseLiteral(ast) { + return new Date(ast.value); + } +}); diff --git a/test/unit/attributeFields.test.js b/test/unit/attributeFields.test.js index bf88e29f..54bf410f 100644 --- a/test/unit/attributeFields.test.js +++ b/test/unit/attributeFields.test.js @@ -3,9 +3,11 @@ import {expect} from 'chai'; import Sequelize from 'sequelize'; import attributeFields from '../../src/attributeFields'; +import DateType from '../../src/types/dateType'; import { sequelize } from '../support/helper'; + import { GraphQLString, GraphQLInt, @@ -116,7 +118,7 @@ describe('attributeFields', function () { expect(fields.virtualBoolean.type).to.equal(GraphQLBoolean); - expect(fields.date.type).to.equal(GraphQLString); + expect(fields.date.type).to.equal(DateType); expect(fields.time.type).to.equal(GraphQLString); diff --git a/test/unit/defaultArgs.test.js b/test/unit/defaultArgs.test.js index f73b9946..694f5232 100644 --- a/test/unit/defaultArgs.test.js +++ b/test/unit/defaultArgs.test.js @@ -4,6 +4,7 @@ import {expect} from 'chai'; import Sequelize from 'sequelize'; import defaultArgs from '../../src/defaultArgs'; +import DateType from '../../src/types/dateType'; import { sequelize } from '../support/helper'; @@ -76,7 +77,7 @@ describe('defaultArgs', function () { args = defaultArgs(Model); expect(args.userId.type).to.equal(GraphQLInt); - expect(args.timestamp.type).to.equal(GraphQLString); + expect(args.timestamp.type).to.equal(DateType); }); describe('will have an "where" argument', function () { diff --git a/test/unit/typeMapper.test.js b/test/unit/typeMapper.test.js index 1642ca74..490be1c1 100644 --- a/test/unit/typeMapper.test.js +++ b/test/unit/typeMapper.test.js @@ -1,6 +1,7 @@ import { expect } from 'chai'; import { mapType, toGraphQL } from '../../src/typeMapper'; import JSONType from '../../src/types/jsonType'; +import DateType from '../../src/types/dateType'; import Sequelize from 'sequelize'; @@ -87,8 +88,8 @@ describe('typeMapper', () => { }); describe('DATE', function () { - it('should map to GraphQLString', function () { - expect(toGraphQL(new DATE(), Sequelize)).to.equal(GraphQLString); + it('should map to DateType', function () { + expect(toGraphQL(new DATE(), Sequelize)).to.equal(DateType); }); });