diff --git a/packages/gatsby/src/schema/__tests__/__snapshots__/infer-graphql-type-test.js.snap b/packages/gatsby/src/schema/__tests__/__snapshots__/infer-graphql-type-test.js.snap index f3fb4e067e9cc..a41450342d450 100644 --- a/packages/gatsby/src/schema/__tests__/__snapshots__/infer-graphql-type-test.js.snap +++ b/packages/gatsby/src/schema/__tests__/__snapshots__/infer-graphql-type-test.js.snap @@ -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 [ @@ -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 { diff --git a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js index b6c3f7002bb1d..d20325ae4fb47 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js @@ -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)) }, @@ -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` }], diff --git a/packages/gatsby/src/schema/data-tree-utils.js b/packages/gatsby/src/schema/data-tree-utils.js index ec8ab5f8ee9d6..601a4c46cdf6a 100644 --- a/packages/gatsby/src/schema/data-tree-utils.js +++ b/packages/gatsby/src/schema/data-tree-utils.js @@ -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 } diff --git a/packages/gatsby/src/schema/infer-graphql-input-fields.js b/packages/gatsby/src/schema/infer-graphql-input-fields.js index d1c55c471a3fb..2e4aaa3c9189b 100644 --- a/packages/gatsby/src/schema/infer-graphql-input-fields.js +++ b/packages/gatsby/src/schema/infer-graphql-input-fields.js @@ -79,6 +79,7 @@ function inferGraphQLInputFields({ case `float`: inType = GraphQLFloat break + case `date`: case `string`: inType = GraphQLString break @@ -129,6 +130,7 @@ function inferGraphQLInputFields({ }), } } + case `date`: case `string`: { return { type: new GraphQLInputObjectType({