diff --git a/packages/gatsby/src/schema/__tests__/create-key.js b/packages/gatsby/src/schema/__tests__/create-key.js index 2de5c7a3915e3..4ff52b57bf269 100644 --- a/packages/gatsby/src/schema/__tests__/create-key.js +++ b/packages/gatsby/src/schema/__tests__/create-key.js @@ -2,7 +2,7 @@ const createKey = require(`../create-key`) describe(`createKey`, () => { it(`leaves valid strings as is`, () => { - ;[`01234`, `validstring`, `_hello`, `_`].forEach(input => { + ;[`validstring`, `_hello`, `_`].forEach(input => { expect(createKey(input)).toBe(input) }) }) @@ -13,6 +13,7 @@ describe(`createKey`, () => { [`~/path/to/some/module`, `_xpathxtoxsomexmodule`], [`/*`, `_x`], [`/*.js`, `_xxjs`], + [`01234`, `_01234`], ].forEach(([input, output]) => { expect(createKey(input)).toBe(output) }) 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 315e0196f3a4f..07f354c74b97e 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js @@ -82,6 +82,7 @@ describe(`GraphQL type inferance`, () => { "with space": 1, "with-hyphen": 2, "with resolver": `1012-11-01`, + 123: 42, aBoolean: true, externalUrl: `https://example.com/awesome.jpg`, domain: `pizza.com`, @@ -102,6 +103,7 @@ describe(`GraphQL type inferance`, () => { anObjectArray: [{ anotherObjectArray: [{ baz: `quz` }] }], "with space": 3, "with-hyphen": 4, + 123: 24, frontmatter: { date: `1984-10-12`, title: `The world of slash and adventure`, @@ -235,6 +237,7 @@ describe(`GraphQL type inferance`, () => { with_space with_hyphen with_resolver(formatString:"DD.MM.YYYY") + _123 ` ) @@ -245,6 +248,8 @@ describe(`GraphQL type inferance`, () => { expect(result.data.listNode[1].with_space).toEqual(3) expect(result.data.listNode[1].with_hyphen).toEqual(4) expect(result.data.listNode[0].with_resolver).toEqual(`01.11.1012`) + expect(result.data.listNode[0]._123).toEqual(42) + expect(result.data.listNode[1]._123).toEqual(24) }) describe(`Handles dates`, () => { diff --git a/packages/gatsby/src/schema/create-key.js b/packages/gatsby/src/schema/create-key.js index ab54c3828f5b0..6a6a72360eb61 100644 --- a/packages/gatsby/src/schema/create-key.js +++ b/packages/gatsby/src/schema/create-key.js @@ -21,5 +21,10 @@ module.exports = (key: string): string => { return replaced.replace(/_/g, (char, index) => (index === 0 ? `_` : `x`)) } + // key is invalid (starts with numeric); normalize with leading underscore + if (replaced.match(/^[0-9]/)) { + return `_` + replaced + } + return replaced }