Skip to content

Commit

Permalink
BREAKING(gatsby-transformer-json): Prefix id and only use createNod…
Browse files Browse the repository at this point in the history
…eId (gatsbyjs#28942)

Co-authored-by: LekoArts <lekoarts@gmail.com>
  • Loading branch information
2 people authored and wardpeet committed Oct 29, 2021
1 parent 244b145 commit 6a0c22f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 66 deletions.
6 changes: 5 additions & 1 deletion packages/gatsby-transformer-json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ arrays of objects and single objects.

`npm install gatsby-transformer-json`

If you want to transform json files, you also need to have `gatsby-source-filesystem` installed and configured so it
If you want to transform JSON files, you also need to have `gatsby-source-filesystem` installed and configured so it
points to your files.

## How to use
Expand Down Expand Up @@ -255,3 +255,7 @@ If you can rewrite your data with objects, you should be good to go:
```

Else, if your data doesn't have a consistent schema, like [TopoJSON files](https://en.wikipedia.org/wiki/GeoJSON#TopoJSON), or you can't rewrite it, consider placing the JSON file inside the [`static` folder](/docs/static-folder/#when-to-use-the-static-folder) and use the dynamic import syntax (`import('/static/myjson.json')`) within the `componentDidMount` lifecycle or the `useEffect` hook.

### `id` and `jsonId` key

If your data contains an `id` key the transformer will automatically convert this key to `jsonId` as `id` is a reserved internal keyword for Gatsby.
Original file line number Diff line number Diff line change
@@ -1,68 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Process JSON nodes correctly coerces an id field to always be a String 1`] = `
Array [
Array [
Object {
"blue": true,
"children": Array [],
"funny": "yup",
"id": "12345",
"internal": Object {
"contentDigest": "contentDigest",
"type": "FooJson",
},
"parent": "whatever",
},
],
]
`;

exports[`Process JSON nodes correctly coerces an id field to always be a String 2`] = `
Array [
Array [
Object {
"child": Object {
"blue": true,
"children": Array [],
"funny": "yup",
"id": "12345",
"internal": Object {
"contentDigest": "contentDigest",
"type": "FooJson",
},
"parent": "whatever",
},
"parent": Object {
"children": Array [],
"content": "{\\"id\\":12345,\\"blue\\":true,\\"funny\\":\\"yup\\"}",
"dir": "<TEMP_DIR>/foo/",
"id": "whatever",
"internal": Object {
"contentDigest": "whatever",
"mediaType": "application/json",
"type": "File",
},
"name": "nodeName",
"parent": null,
},
},
],
]
`;

exports[`Process JSON nodes correctly correctly creates a node from JSON which is a single object 1`] = `
Array [
Array [
Object {
"blue": true,
"children": Array [],
"funny": "yup",
"id": "foo",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "contentDigest",
"type": "FooJson",
},
"jsonId": "foo",
"parent": "whatever",
},
],
Expand All @@ -77,11 +27,12 @@ Array [
"blue": true,
"children": Array [],
"funny": "yup",
"id": "foo",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "contentDigest",
"type": "FooJson",
},
"jsonId": "foo",
"parent": "whatever",
},
"parent": Object {
Expand Down Expand Up @@ -109,11 +60,12 @@ Array [
"blue": true,
"children": Array [],
"funny": "yup",
"id": "foo",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "contentDigest",
"type": "NotFileJson",
},
"jsonId": "foo",
"parent": "whatever",
},
],
Expand All @@ -128,11 +80,12 @@ Array [
"blue": true,
"children": Array [],
"funny": "yup",
"id": "foo",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "contentDigest",
"type": "NotFileJson",
},
"jsonId": "foo",
"parent": "whatever",
},
"parent": Object {
Expand All @@ -158,11 +111,12 @@ Array [
"blue": true,
"children": Array [],
"funny": "yup",
"id": "foo",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "contentDigest",
"type": "NodeNameJson",
},
"jsonId": "foo",
"parent": "whatever",
},
],
Expand Down Expand Up @@ -190,11 +144,12 @@ Array [
"blue": true,
"children": Array [],
"funny": "yup",
"id": "foo",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "contentDigest",
"type": "NodeNameJson",
},
"jsonId": "foo",
"parent": "whatever",
},
"parent": Object {
Expand Down Expand Up @@ -250,11 +205,12 @@ Array [
"blue": true,
"children": Array [],
"funny": "yup",
"id": "foo",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "contentDigest",
"type": "NotFileJson",
},
"jsonId": "foo",
"parent": "whatever",
},
],
Expand Down Expand Up @@ -282,11 +238,12 @@ Array [
"blue": true,
"children": Array [],
"funny": "yup",
"id": "foo",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "contentDigest",
"type": "NotFileJson",
},
"jsonId": "foo",
"parent": "whatever",
},
"parent": Object {
Expand Down Expand Up @@ -330,3 +287,56 @@ Array [
],
]
`;

exports[`Process JSON nodes correctly creates a jsonId key if id was given 1`] = `
Array [
Array [
Object {
"blue": true,
"children": Array [],
"funny": "yup",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "contentDigest",
"type": "FooJson",
},
"jsonId": "123",
"parent": "whatever",
},
],
]
`;

exports[`Process JSON nodes correctly creates a jsonId key if id was given 2`] = `
Array [
Array [
Object {
"child": Object {
"blue": true,
"children": Array [],
"funny": "yup",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "contentDigest",
"type": "FooJson",
},
"jsonId": "123",
"parent": "whatever",
},
"parent": Object {
"children": Array [],
"content": "{\\"id\\":\\"123\\",\\"blue\\":true,\\"funny\\":\\"yup\\"}",
"dir": "<TEMP_DIR>/foo/",
"id": "whatever",
"internal": Object {
"contentDigest": "whatever",
"mediaType": "application/json",
"type": "File",
},
"name": "nodeName",
"parent": null,
},
},
],
]
`;
4 changes: 2 additions & 2 deletions packages/gatsby-transformer-json/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ describe(`Process JSON nodes correctly`, () => {
})
})

it(`coerces an id field to always be a String`, async () => {
const data = { id: 12345, blue: true, funny: `yup` }
it(`creates a jsonId key if id was given`, async () => {
const data = { id: `123`, blue: true, funny: `yup` }
const node = {
...baseFileNode,
content: JSON.stringify(data),
Expand Down
9 changes: 5 additions & 4 deletions packages/gatsby-transformer-json/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ async function onCreateNode(
type,
},
}
if (obj.id) {
jsonNode[`jsonId`] = obj.id
}
createNode(jsonNode)
createParentChildLink({ parent: node, child: jsonNode })
}
Expand All @@ -60,16 +63,14 @@ async function onCreateNode(
parsedContent.forEach((obj, i) => {
transformObject(
obj,
obj.id ? String(obj.id) : createNodeId(`${node.id} [${i}] >>> JSON`),
createNodeId(`${node.id} [${i}] >>> JSON`),
getType({ node, object: obj, isArray: true })
)
})
} else if (_.isPlainObject(parsedContent)) {
transformObject(
parsedContent,
parsedContent.id
? String(parsedContent.id)
: createNodeId(`${node.id} >>> JSON`),
createNodeId(`${node.id} >>> JSON`),
getType({ node, object: parsedContent, isArray: false })
)
}
Expand Down

0 comments on commit 6a0c22f

Please sign in to comment.