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

type inferring - handle array of Date objects #3955

Merged
merged 2 commits into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`GraphQL type inferance Handles dates Date type inference 1`] = `
exports[`GraphQL type inferance Handles dates Infers from Date objects 1`] = `
Object {
"data": Object {
"listNode": Array [
Expand All @@ -15,6 +15,26 @@ Object {
}
`;

exports[`GraphQL type inferance Handles dates Infers from array of Date objects 1`] = `
Object {
"data": Object {
"listNode": Array [
Object {
"dateObject": Array [
"2012-11-05T00:00:00.000Z",
"2012-11-06T00:00:00.000Z",
],
},
Object {
"dateObject": Array [
"2012-11-05T00:00:00.000Z",
],
},
],
},
}
`;

exports[`GraphQL type inferance Infers graphql type from array of nodes 1`] = `
Object {
"data": Object {
Expand Down
20 changes: 19 additions & 1 deletion packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe(`GraphQL type inferance`, () => {
expect(result.data.listNode[0].number).toEqual(2018)
})

it(`Date type inference`, async () => {
it(`Infers from Date objects`, async () => {
let result = await queryResult(
[
{ dateObject: new Date(Date.UTC(2012, 10, 5)) },
Expand All @@ -212,6 +212,24 @@ describe(`GraphQL type inferance`, () => {
expect(result).toMatchSnapshot()
})

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

it(`Infers from date strings`, async () => {
let result = await queryResult(
[{ date: `1012-11-01` }],
Expand Down
6 changes: 4 additions & 2 deletions packages/gatsby/src/schema/data-tree-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ const extractFieldExamples = (nodes: any[]) =>
return array
}

// primitive values don't get merged further, just take the first item
if (!_.isObject(array[0])) return array.slice(0, 1)
// primitive values and dates don't get merged further, just take the first item
if (!_.isObject(array[0]) || array[0] instanceof Date) {
return array.slice(0, 1)
}
let merged = extractFieldExamples(array)
return isDefined(merged) ? [merged] : null
}
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/src/schema/infer-graphql-input-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function inferGraphQLInputFields({
case `float`:
inType = GraphQLFloat
break
case `date`:
case `string`:
inType = GraphQLString
break
Expand Down Expand Up @@ -129,6 +130,7 @@ function inferGraphQLInputFields({
}),
}
}
case `date`:
case `string`: {
return {
type: new GraphQLInputObjectType({
Expand Down