Skip to content

Commit

Permalink
ISO date format support (mickhansen#621)
Browse files Browse the repository at this point in the history
* ISO date format support

* Remove yarn.lock

* Revert DATEONLY to String and add DateType to src/index

* Missed the DATEONLY test description on revert.
  • Loading branch information
RichAyotte authored and mickhansen committed Jul 17, 2018
1 parent d682a1b commit b92521c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ module.exports = {
createConnectionResolver: require('./relay').createConnectionResolver,
createNodeInterface: require('./relay').createNodeInterface,
JSONType: require('./types/jsonType'),
DateType: require('./types/DateType'),
};
22 changes: 13 additions & 9 deletions src/typeMapper.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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
Expand Down Expand Up @@ -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 ||
Expand Down
49 changes: 49 additions & 0 deletions src/types/dateType.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
4 changes: 3 additions & 1 deletion test/unit/attributeFields.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion test/unit/defaultArgs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 () {
Expand Down
5 changes: 3 additions & 2 deletions test/unit/typeMapper.test.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
});
});

Expand Down

0 comments on commit b92521c

Please sign in to comment.