Skip to content

Commit

Permalink
Infer value of date objects fixes #1952 (#1964)
Browse files Browse the repository at this point in the history
* Infer value of date objects fixes #1952

* Make date utc so doesn't vary

* Stringify and parse dates to ensure we have a normalized string form

* Map gatsby-1-config-css-modules to its source
  • Loading branch information
KyleAMathews authored Aug 30, 2017
1 parent be98e73 commit aab0fe4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
],
moduleNameMapper: {
"^graphql-skip-limit$": `<rootDir>/packages/graphql-skip-limit/src/index.js`,
"^gatsby-1-config-css-modules$": `<rootDir>/packages/gatsby-1-config-css-modules/src/index.js`,
"^gatsby-plugin-sharp$": `<rootDir>/packages/gatsby-plugin-sharp/src/index.js`,
"^gatsby/dist/redux/actions$": `<rootDir>/packages/gatsby/src/redux/actions.js`,
"^highlight.js$": `<rootDir>/node_modules/highlight.js/lib/index.js`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,18 @@ Object {
},
}
`;

exports[`GraphQL type inferance handles date objects 1`] = `
Object {
"data": Object {
"listNode": Array [
Object {
"dateObject": "2012-11-05T00:00:00.000Z",
},
Object {
"dateObject": "2012-11-05T00:00:00.000Z",
},
],
},
}
`;
13 changes: 13 additions & 0 deletions packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ describe(`GraphQL type inferance`, () => {
expect(result.data.listNode[0].number).toEqual(1.1)
})

it(`handles date objects`, async () => {
let result = await queryResult(
[
{ dateObject: new Date(Date.UTC(2012, 10, 5)) },
{ dateObject: new Date(Date.UTC(2012, 10, 5)) },
],
`
dateObject
`
)
expect(result).toMatchSnapshot()
})

it(`filters out empty objects`, async () => {
let result = await queryResult(
[{ foo: {}, bar: `baz` }],
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/src/schema/data-tree-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const areAllSameType = list =>
const isEmptyObjectOrArray = (obj: any): boolean => {
if (obj === INVALID_VALUE) {
return true
} else if (_.isDate(obj)) {
return false
// Simple "is object empty" check.
} else if (_.isObject(obj) && _.isEmpty(obj)) {
return true
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/infer-graphql-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function inferGraphQLType({
},
},
resolve(object, { fromNow, difference, formatString }) {
const date = object[fieldName]
const date = JSON.parse(JSON.stringify(object[fieldName]))
if (formatString) {
return moment.utc(date, ISO_8601_FORMAT, true).format(formatString)
} else if (fromNow) {
Expand Down

0 comments on commit aab0fe4

Please sign in to comment.