Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer value of date objects fixes #1952 #1964

Merged
merged 4 commits into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "Sun Nov 04 2012 16:00:00 GMT-0800 (PST)",
},
Object {
"dateObject": "Sun Nov 04 2012 16:00:00 GMT-0800 (PST)",
},
],
},
}
`;
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the below branch not catch this already?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it's reported as an empty object

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weeiiiird

// 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 = object[fieldName].toString()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does toISOString() make more sense as a default? its a bit less culture dependent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh for sure!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix later

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to do JSON.parse(JSON.stringify(date)) as date could just be a string so this seemed simplest. The internets say that JSON.stringify uses Date.toISOString under the hood anyways.

if (formatString) {
return moment.utc(date, ISO_8601_FORMAT, true).format(formatString)
} else if (fromNow) {
Expand Down