Skip to content

Commit b92521c

Browse files
RichAyottemickhansen
authored andcommitted
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.
1 parent d682a1b commit b92521c

File tree

6 files changed

+71
-13
lines changed

6 files changed

+71
-13
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ module.exports = {
1212
createConnectionResolver: require('./relay').createConnectionResolver,
1313
createNodeInterface: require('./relay').createNodeInterface,
1414
JSONType: require('./types/jsonType'),
15+
DateType: require('./types/DateType'),
1516
};

src/typeMapper.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {
2-
GraphQLInt,
3-
GraphQLString,
4-
GraphQLBoolean,
5-
GraphQLFloat,
6-
GraphQLEnumType,
7-
GraphQLList
8-
} from 'graphql';
2+
GraphQLInt,
3+
GraphQLString,
4+
GraphQLBoolean,
5+
GraphQLFloat,
6+
GraphQLEnumType,
7+
GraphQLList,
8+
} from 'graphql';
9+
10+
import DateType from './types/dateType';
911
import JSONType from './types/jsonType';
1012
import _ from 'lodash';
1113

@@ -18,7 +20,6 @@ export function mapType(mapFunc) {
1820
customTypeMapper = mapFunc;
1921
}
2022

21-
2223
/**
2324
* Checks the type of the sequelize data type and
2425
* returns the corresponding type in GraphQL
@@ -68,11 +69,14 @@ export function toGraphQL(sequelizeType, sequelizeTypes) {
6869
if (sequelizeType instanceof FLOAT ||
6970
sequelizeType instanceof DOUBLE) return GraphQLFloat;
7071

72+
if (sequelizeType instanceof DATE) {
73+
return DateType;
74+
}
75+
7176
if (sequelizeType instanceof CHAR ||
7277
sequelizeType instanceof STRING ||
7378
sequelizeType instanceof TEXT ||
7479
sequelizeType instanceof UUID ||
75-
sequelizeType instanceof DATE ||
7680
sequelizeType instanceof DATEONLY ||
7781
sequelizeType instanceof TIME ||
7882
sequelizeType instanceof BIGINT ||

src/types/dateType.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {
2+
GraphQLScalarType
3+
} from 'graphql';
4+
5+
/**
6+
* A special custom Scalar type for Dates that converts to a ISO formatted string
7+
* @param {String} options.name:
8+
* @param {String} options.description:
9+
* @param {Date} options.serialize(d)
10+
* @param {String} parseValue(value)
11+
* @param {Object} parseLiteral(ast)
12+
*/
13+
export default new GraphQLScalarType({
14+
name: 'Date',
15+
description: 'A special custom Scalar type for Dates that converts to a ISO formatted string ',
16+
/**
17+
* serialize
18+
* @param {Date} d Date obj
19+
* @return {String} Serialised date object
20+
*/
21+
serialize(d) {
22+
if (!d) {
23+
return null;
24+
}
25+
26+
if (d instanceof Date) {
27+
return d.toISOString();
28+
}
29+
return d;
30+
},
31+
/**
32+
* parseValue
33+
* @param {String} value date string
34+
* @return {Date} Date object
35+
*/
36+
parseValue(value) {
37+
try {
38+
if (!value) {
39+
return null;
40+
}
41+
return new Date(value);
42+
} catch (e) {
43+
return null;
44+
}
45+
},
46+
parseLiteral(ast) {
47+
return new Date(ast.value);
48+
}
49+
});

test/unit/attributeFields.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import {expect} from 'chai';
44
import Sequelize from 'sequelize';
55
import attributeFields from '../../src/attributeFields';
6+
import DateType from '../../src/types/dateType';
67

78
import { sequelize } from '../support/helper';
89

10+
911
import {
1012
GraphQLString,
1113
GraphQLInt,
@@ -116,7 +118,7 @@ describe('attributeFields', function () {
116118

117119
expect(fields.virtualBoolean.type).to.equal(GraphQLBoolean);
118120

119-
expect(fields.date.type).to.equal(GraphQLString);
121+
expect(fields.date.type).to.equal(DateType);
120122

121123
expect(fields.time.type).to.equal(GraphQLString);
122124

test/unit/defaultArgs.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import {expect} from 'chai';
55
import Sequelize from 'sequelize';
66
import defaultArgs from '../../src/defaultArgs';
7+
import DateType from '../../src/types/dateType';
78

89
import { sequelize } from '../support/helper';
910

@@ -76,7 +77,7 @@ describe('defaultArgs', function () {
7677
args = defaultArgs(Model);
7778

7879
expect(args.userId.type).to.equal(GraphQLInt);
79-
expect(args.timestamp.type).to.equal(GraphQLString);
80+
expect(args.timestamp.type).to.equal(DateType);
8081
});
8182

8283
describe('will have an "where" argument', function () {

test/unit/typeMapper.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expect } from 'chai';
22
import { mapType, toGraphQL } from '../../src/typeMapper';
33
import JSONType from '../../src/types/jsonType';
4+
import DateType from '../../src/types/dateType';
45

56
import Sequelize from 'sequelize';
67

@@ -87,8 +88,8 @@ describe('typeMapper', () => {
8788
});
8889

8990
describe('DATE', function () {
90-
it('should map to GraphQLString', function () {
91-
expect(toGraphQL(new DATE(), Sequelize)).to.equal(GraphQLString);
91+
it('should map to DateType', function () {
92+
expect(toGraphQL(new DATE(), Sequelize)).to.equal(DateType);
9293
});
9394
});
9495

0 commit comments

Comments
 (0)