Skip to content

Commit

Permalink
type inferring - handle array of Date objects (#3955)
Browse files Browse the repository at this point in the history
* inferring types: handle array of Date objects

* add tests for inferring arrays of Date objects
  • Loading branch information
pieh authored and KyleAMathews committed Feb 9, 2018
1 parent 507d3b2 commit f759486
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
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

0 comments on commit f759486

Please sign in to comment.