Skip to content
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
3 changes: 2 additions & 1 deletion packages/gatsby/src/schema/__tests__/create-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
Expand All @@ -13,6 +13,7 @@ describe(`createKey`, () => {
[`~/path/to/some/module`, `_xpathxtoxsomexmodule`],
[`/*`, `_x`],
[`/*.js`, `_xxjs`],
[`01234`, `_01234`],
].forEach(([input, output]) => {
expect(createKey(input)).toBe(output)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand All @@ -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`,
Expand Down Expand Up @@ -235,6 +237,7 @@ describe(`GraphQL type inferance`, () => {
with_space
with_hyphen
with_resolver(formatString:"DD.MM.YYYY")
_123
`
)

Expand All @@ -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`, () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/gatsby/src/schema/create-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}