From e58b594ea46c8e13f6702c81dd82750e206a5c7a Mon Sep 17 00:00:00 2001 From: Josh Kennedy <35467034+joshatoutthink@users.noreply.github.com> Date: Thu, 14 Oct 2021 04:15:56 -0400 Subject: [PATCH] BREAKING(gatsby-transformer-yaml): Prefix `id` and only use createNodeId (#28943) Co-authored-by: LekoArts --- packages/gatsby-transformer-yaml/README.md | 6 ++++ .../__snapshots__/gatsby-node.js.snap | 36 ++++++++++++------- .../src/gatsby-node.js | 7 ++-- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/packages/gatsby-transformer-yaml/README.md b/packages/gatsby-transformer-yaml/README.md index 6eca02e0b10ce..993a79ec44123 100644 --- a/packages/gatsby-transformer-yaml/README.md +++ b/packages/gatsby-transformer-yaml/README.md @@ -238,3 +238,9 @@ module.exports = { } } ``` + +## Troubleshooting + +### `id` and `yamlId` key + +If your data contains an `id` key the transformer will automatically convert this key to `yamlId` as `id` is a reserved internal keyword for Gatsby. diff --git a/packages/gatsby-transformer-yaml/src/__tests__/__snapshots__/gatsby-node.js.snap b/packages/gatsby-transformer-yaml/src/__tests__/__snapshots__/gatsby-node.js.snap index c5afb63dc4de7..0368a176b73b4 100644 --- a/packages/gatsby-transformer-yaml/src/__tests__/__snapshots__/gatsby-node.js.snap +++ b/packages/gatsby-transformer-yaml/src/__tests__/__snapshots__/gatsby-node.js.snap @@ -313,12 +313,13 @@ Array [ "blue": true, "children": Array [], "funny": "yup", - "id": "some", + "id": "uuid-from-gatsby", "internal": Object { "contentDigest": "contentDigest", "type": "FooBarYaml", }, "parent": "whatever", + "yamlId": "some", }, ], ] @@ -332,12 +333,13 @@ Array [ "blue": true, "children": Array [], "funny": "yup", - "id": "some", + "id": "uuid-from-gatsby", "internal": Object { "contentDigest": "contentDigest", "type": "FooBarYaml", }, "parent": "whatever", + "yamlId": "some", }, "parent": Object { "children": Array [], @@ -368,12 +370,13 @@ Array [ "blue": true, "children": Array [], "funny": "yup", - "id": "some", + "id": "uuid-from-gatsby", "internal": Object { "contentDigest": "contentDigest", "type": "fixed", }, "parent": "whatever", + "yamlId": "some", }, ], ] @@ -387,12 +390,13 @@ Array [ "blue": true, "children": Array [], "funny": "yup", - "id": "some", + "id": "uuid-from-gatsby", "internal": Object { "contentDigest": "contentDigest", "type": "fixed", }, "parent": "whatever", + "yamlId": "some", }, "parent": Object { "children": Array [], @@ -423,12 +427,13 @@ Array [ "blue": true, "children": Array [], "funny": "yup", - "id": "some", + "id": "uuid-from-gatsby", "internal": Object { "contentDigest": "contentDigest", "type": "yup", }, "parent": "whatever", + "yamlId": "some", }, ], ] @@ -442,12 +447,13 @@ Array [ "blue": true, "children": Array [], "funny": "yup", - "id": "some", + "id": "uuid-from-gatsby", "internal": Object { "contentDigest": "contentDigest", "type": "yup", }, "parent": "whatever", + "yamlId": "some", }, "parent": Object { "children": Array [], @@ -772,12 +778,13 @@ Array [ "blue": true, "children": Array [], "funny": "yup", - "id": "some", + "id": "uuid-from-gatsby", "internal": Object { "contentDigest": "contentDigest", "type": "OtherYaml", }, "parent": "whatever", + "yamlId": "some", }, ], ] @@ -791,12 +798,13 @@ Array [ "blue": true, "children": Array [], "funny": "yup", - "id": "some", + "id": "uuid-from-gatsby", "internal": Object { "contentDigest": "contentDigest", "type": "OtherYaml", }, "parent": "whatever", + "yamlId": "some", }, "parent": Object { "children": Array [], @@ -825,12 +833,13 @@ Array [ "blue": true, "children": Array [], "funny": "yup", - "id": "some", + "id": "uuid-from-gatsby", "internal": Object { "contentDigest": "contentDigest", "type": "fixed", }, "parent": "whatever", + "yamlId": "some", }, ], ] @@ -844,12 +853,13 @@ Array [ "blue": true, "children": Array [], "funny": "yup", - "id": "some", + "id": "uuid-from-gatsby", "internal": Object { "contentDigest": "contentDigest", "type": "fixed", }, "parent": "whatever", + "yamlId": "some", }, "parent": Object { "children": Array [], @@ -878,12 +888,13 @@ Array [ "blue": true, "children": Array [], "funny": "yup", - "id": "some", + "id": "uuid-from-gatsby", "internal": Object { "contentDigest": "contentDigest", "type": "yup", }, "parent": "whatever", + "yamlId": "some", }, ], ] @@ -897,12 +908,13 @@ Array [ "blue": true, "children": Array [], "funny": "yup", - "id": "some", + "id": "uuid-from-gatsby", "internal": Object { "contentDigest": "contentDigest", "type": "yup", }, "parent": "whatever", + "yamlId": "some", }, "parent": Object { "children": Array [], diff --git a/packages/gatsby-transformer-yaml/src/gatsby-node.js b/packages/gatsby-transformer-yaml/src/gatsby-node.js index 850d65277c5b1..2d9ca9064e9dc 100644 --- a/packages/gatsby-transformer-yaml/src/gatsby-node.js +++ b/packages/gatsby-transformer-yaml/src/gatsby-node.js @@ -39,6 +39,9 @@ async function onCreateNode( type, }, } + if (obj.id) { + yamlNode[`yamlId`] = obj.id + } createNode(yamlNode) createParentChildLink({ parent: node, child: yamlNode }) } @@ -52,14 +55,14 @@ async function onCreateNode( parsedContent.forEach((obj, i) => { transformObject( obj, - obj.id ? obj.id : createNodeId(`${node.id} [${i}] >>> YAML`), + createNodeId(`${node.id} [${i}] >>> YAML`), getType({ node, object: obj, isArray: true }) ) }) } else if (_.isPlainObject(parsedContent)) { transformObject( parsedContent, - parsedContent.id ? parsedContent.id : createNodeId(`${node.id} >>> YAML`), + createNodeId(`${node.id} >>> YAML`), getType({ node, object: parsedContent, isArray: false }) ) }