Skip to content

Commit 613c56b

Browse files
danielfarrellKyleAMathews
authored andcommitted
V2 - Switch Sources and Transformers to use createNodeId for ids (#3807)
* Switch Sources and Transformers to use createNodeId for ids * Only use createNodeId if an id isn't passed in * Remove unused imports
1 parent 8339263 commit 613c56b

File tree

64 files changed

+502
-685
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+502
-685
lines changed

Breaking Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11

22
- Remove postcss plugins (cssnext, cssimport) from default css loader config
33
- change webpack api
4+
- Source & transformer plugins now use UUIDs for ids. If you used glob or regex to query nodes by id then you'll need to query something else.

packages/gatsby-source-contentful/src/__tests__/__snapshots__/normalize.js.snap

Lines changed: 252 additions & 252 deletions
Large diffs are not rendered by default.

packages/gatsby-source-contentful/src/__tests__/normalize.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ describe(`Process contentful data`, () => {
5252

5353
it(`creates nodes for each entry`, () => {
5454
const createNode = jest.fn()
55+
const createNodeId = jest.fn()
56+
createNodeId.mockReturnValue(`uuid-from-gatsby`)
5557
contentTypeItems.forEach((contentTypeItem, i) => {
5658
normalize.createContentTypeNodes({
5759
contentTypeItem,
5860
restrictedNodeFields,
5961
conflictFieldPrefix,
6062
entries: entryList[i].map(normalize.fixIds),
6163
createNode,
64+
createNodeId,
6265
resolvable,
6366
foreignReferenceMap,
6467
defaultLocale,
@@ -70,11 +73,14 @@ describe(`Process contentful data`, () => {
7073

7174
it(`creates nodes for each asset`, () => {
7275
const createNode = jest.fn()
76+
const createNodeId = jest.fn()
77+
createNodeId.mockReturnValue(`uuid-from-gatsby`)
7378
const assets = currentSyncData.assets
7479
assets.forEach(assetItem => {
7580
normalize.createAssetNodes({
7681
assetItem,
7782
createNode,
83+
createNodeId,
7884
defaultLocale,
7985
locales,
8086
})

packages/gatsby-source-contentful/src/gatsby-node.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ exports.setFieldsOnGraphQLNodeType = require(`./extend-node-type`).extendNodeTyp
3030
*/
3131

3232
exports.sourceNodes = async (
33-
{ actions, getNodes, hasNodeChanged, store },
33+
{ actions, getNodes, createNodeId, hasNodeChanged, store },
3434
{ spaceId, accessToken, host }
3535
) => {
3636
const { createNode, deleteNodes, touchNode, setPluginStatus } = actions
@@ -151,6 +151,7 @@ exports.sourceNodes = async (
151151
conflictFieldPrefix,
152152
entries: entryList[i],
153153
createNode,
154+
createNodeId,
154155
resolvable,
155156
foreignReferenceMap,
156157
defaultLocale,
@@ -162,6 +163,7 @@ exports.sourceNodes = async (
162163
normalize.createAssetNodes({
163164
assetItem,
164165
createNode,
166+
createNodeId,
165167
defaultLocale,
166168
locales,
167169
})

packages/gatsby-source-contentful/src/normalize.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ exports.buildForeignReferenceMap = ({
149149
return foreignReferenceMap
150150
}
151151

152-
function createTextNode(node, key, text, createNode) {
152+
function createTextNode(node, key, text, createNode, createNodeId) {
153153
const str = _.isString(text) ? text : ` `
154154
const textNode = {
155-
id: `${node.id}${key}TextNode`,
155+
id: createNodeId(`${node.id}${key}TextNode`),
156156
parent: node.id,
157157
children: [],
158158
[key]: str,
@@ -171,11 +171,11 @@ function createTextNode(node, key, text, createNode) {
171171
}
172172
exports.createTextNode = createTextNode
173173

174-
function createJSONNode(node, key, content, createNode) {
174+
function createJSONNode(node, key, content, createNode, createNodeId) {
175175
const str = JSON.stringify(content)
176176
const JSONNode = {
177177
...content,
178-
id: `${node.id}${key}JSONNode`,
178+
id: createNodeId(`${node.id}${key}JSONNode`),
179179
parent: node.id,
180180
children: [],
181181
internal: {
@@ -199,6 +199,7 @@ exports.createContentTypeNodes = ({
199199
conflictFieldPrefix,
200200
entries,
201201
createNode,
202+
createNodeId,
202203
resolvable,
203204
foreignReferenceMap,
204205
defaultLocale,
@@ -286,7 +287,7 @@ exports.createContentTypeNodes = ({
286287
}
287288

288289
let entryNode = {
289-
id: mId(entryItem.sys.id),
290+
id: createNodeId(mId(entryItem.sys.id)),
290291
contentful_id: entryItem.sys.contentful_id,
291292
createdAt: entryItem.sys.createdAt,
292293
updatedAt: entryItem.sys.updatedAt,
@@ -328,7 +329,8 @@ exports.createContentTypeNodes = ({
328329
entryNode,
329330
entryItemFieldKey,
330331
entryItemFields[entryItemFieldKey],
331-
createNode
332+
createNode,
333+
createNodeId,
332334
)
333335

334336
delete entryItemFields[entryItemFieldKey]
@@ -337,7 +339,8 @@ exports.createContentTypeNodes = ({
337339
entryNode,
338340
entryItemFieldKey,
339341
entryItemFields[entryItemFieldKey],
340-
createNode
342+
createNode,
343+
createNodeId,
341344
)
342345

343346
delete entryItemFields[entryItemFieldKey]
@@ -356,7 +359,7 @@ exports.createContentTypeNodes = ({
356359

357360
// Create a node for each content type
358361
const contentTypeNode = {
359-
id: contentTypeItemId,
362+
id: createNodeId(contentTypeItemId),
360363
parent: null,
361364
children: [],
362365
name: contentTypeItem.name,
@@ -382,6 +385,7 @@ exports.createContentTypeNodes = ({
382385
exports.createAssetNodes = ({
383386
assetItem,
384387
createNode,
388+
createNodeId,
385389
defaultLocale,
386390
locales,
387391
}) => {
@@ -405,7 +409,7 @@ exports.createAssetNodes = ({
405409
: ``,
406410
}
407411
const assetNode = {
408-
id: mId(localizedAsset.sys.id),
412+
id: createNodeId(mId(localizedAsset.sys.id)),
409413
parent: null,
410414
children: [],
411415
...localizedAsset.fields,

packages/gatsby-source-drupal/src/gatsby-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const createContentDigest = obj =>
1313

1414
exports.sourceNodes = async (
1515
{ actions, getNode, hasNodeChanged, store, cache },
16-
{ baseUrl, apiBase }
16+
{ baseUrl, apiBase, createNodeId }
1717
) => {
1818
const { createNode } = actions
1919

@@ -103,7 +103,7 @@ exports.sourceNodes = async (
103103

104104
_.each(contentType.data, datum => {
105105
const node = {
106-
id: datum.id,
106+
id: createNodeId(datum.id),
107107
parent: null,
108108
children: [],
109109
...datum.attributes,

packages/gatsby-source-faker/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gatsby-source-faker",
3-
"version": "1.0.1",
3+
"version": "2.0.0",
44
"description": "A gatsby plugin to get fake data for testing",
55
"main": "index.js",
66
"scripts": {

packages/gatsby-source-faker/src/gatsby-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const faker = require(`faker`)
22
const crypto = require(`crypto`)
33

4-
exports.sourceNodes = ({ actions }, pluginOptions) => {
4+
exports.sourceNodes = ({ actions, createNodeId }, pluginOptions) => {
55
const { createNode } = actions
66
const { schema, count, type } = pluginOptions
77
for (let i = 0; i < count; i++) {
@@ -20,7 +20,7 @@ exports.sourceNodes = ({ actions }, pluginOptions) => {
2020
.digest(`hex`)
2121

2222
const nodeBase = {
23-
id: JSON.stringify(faker.random.number()),
23+
id: createNodeId(JSON.stringify(faker.random.number())),
2424
parent: null,
2525
children: [],
2626
internal: {

packages/gatsby-source-filesystem/src/__tests__/create-file-node.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const { createFileNode } = require(`../create-file-node`)
55
// FIXME: This test needs to not use snapshots because of file differences
66
// and locations across users and CI systems
77
describe(`create-file-node`, () => {
8-
it(`creates a file node`, () =>
9-
createFileNode(path.resolve(`${__dirname}/fixtures/file.json`), {}))
8+
it(`creates a file node`, () => {
9+
const createNodeId = jest.fn()
10+
createNodeId.mockReturnValue(`uuid-from-gatsby`)
11+
return createFileNode(path.resolve(`${__dirname}/fixtures/file.json`), createNodeId, {})
12+
})
1013
})

packages/gatsby-source-filesystem/src/create-file-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const createId = path => {
1414

1515
exports.createId = createId
1616

17-
exports.createFileNode = async (pathToFile, pluginOptions = {}) => {
17+
exports.createFileNode = async (pathToFile, createNodeId, pluginOptions = {}) => {
1818
const slashed = slash(pathToFile)
1919
const parsedSlashed = path.parse(slashed)
2020
const slashedFile = {
@@ -56,7 +56,7 @@ exports.createFileNode = async (pathToFile, pluginOptions = {}) => {
5656
// Don't actually make the File id the absolute path as otherwise
5757
// people will use the id for that and ids shouldn't be treated as
5858
// useful information.
59-
id: createId(pathToFile),
59+
id: createNodeId(pathToFile),
6060
children: [],
6161
parent: `___SOURCE___`,
6262
internal,

0 commit comments

Comments
 (0)