Skip to content

Commit

Permalink
V2 - Switch Sources and Transformers to use createNodeId for ids (#3807)
Browse files Browse the repository at this point in the history
* Switch Sources and Transformers to use createNodeId for ids

* Only use createNodeId if an id isn't passed in

* Remove unused imports
  • Loading branch information
danielfarrell authored and KyleAMathews committed Feb 10, 2018
1 parent 8339263 commit 613c56b
Show file tree
Hide file tree
Showing 64 changed files with 502 additions and 685 deletions.
1 change: 1 addition & 0 deletions Breaking Changes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

- Remove postcss plugins (cssnext, cssimport) from default css loader config
- change webpack api
- 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.

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions packages/gatsby-source-contentful/src/__tests__/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ describe(`Process contentful data`, () => {

it(`creates nodes for each entry`, () => {
const createNode = jest.fn()
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
contentTypeItems.forEach((contentTypeItem, i) => {
normalize.createContentTypeNodes({
contentTypeItem,
restrictedNodeFields,
conflictFieldPrefix,
entries: entryList[i].map(normalize.fixIds),
createNode,
createNodeId,
resolvable,
foreignReferenceMap,
defaultLocale,
Expand All @@ -70,11 +73,14 @@ describe(`Process contentful data`, () => {

it(`creates nodes for each asset`, () => {
const createNode = jest.fn()
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
const assets = currentSyncData.assets
assets.forEach(assetItem => {
normalize.createAssetNodes({
assetItem,
createNode,
createNodeId,
defaultLocale,
locales,
})
Expand Down
4 changes: 3 additions & 1 deletion packages/gatsby-source-contentful/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ exports.setFieldsOnGraphQLNodeType = require(`./extend-node-type`).extendNodeTyp
*/

exports.sourceNodes = async (
{ actions, getNodes, hasNodeChanged, store },
{ actions, getNodes, createNodeId, hasNodeChanged, store },
{ spaceId, accessToken, host }
) => {
const { createNode, deleteNodes, touchNode, setPluginStatus } = actions
Expand Down Expand Up @@ -151,6 +151,7 @@ exports.sourceNodes = async (
conflictFieldPrefix,
entries: entryList[i],
createNode,
createNodeId,
resolvable,
foreignReferenceMap,
defaultLocale,
Expand All @@ -162,6 +163,7 @@ exports.sourceNodes = async (
normalize.createAssetNodes({
assetItem,
createNode,
createNodeId,
defaultLocale,
locales,
})
Expand Down
22 changes: 13 additions & 9 deletions packages/gatsby-source-contentful/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ exports.buildForeignReferenceMap = ({
return foreignReferenceMap
}

function createTextNode(node, key, text, createNode) {
function createTextNode(node, key, text, createNode, createNodeId) {
const str = _.isString(text) ? text : ` `
const textNode = {
id: `${node.id}${key}TextNode`,
id: createNodeId(`${node.id}${key}TextNode`),
parent: node.id,
children: [],
[key]: str,
Expand All @@ -171,11 +171,11 @@ function createTextNode(node, key, text, createNode) {
}
exports.createTextNode = createTextNode

function createJSONNode(node, key, content, createNode) {
function createJSONNode(node, key, content, createNode, createNodeId) {
const str = JSON.stringify(content)
const JSONNode = {
...content,
id: `${node.id}${key}JSONNode`,
id: createNodeId(`${node.id}${key}JSONNode`),
parent: node.id,
children: [],
internal: {
Expand All @@ -199,6 +199,7 @@ exports.createContentTypeNodes = ({
conflictFieldPrefix,
entries,
createNode,
createNodeId,
resolvable,
foreignReferenceMap,
defaultLocale,
Expand Down Expand Up @@ -286,7 +287,7 @@ exports.createContentTypeNodes = ({
}

let entryNode = {
id: mId(entryItem.sys.id),
id: createNodeId(mId(entryItem.sys.id)),
contentful_id: entryItem.sys.contentful_id,
createdAt: entryItem.sys.createdAt,
updatedAt: entryItem.sys.updatedAt,
Expand Down Expand Up @@ -328,7 +329,8 @@ exports.createContentTypeNodes = ({
entryNode,
entryItemFieldKey,
entryItemFields[entryItemFieldKey],
createNode
createNode,
createNodeId,
)

delete entryItemFields[entryItemFieldKey]
Expand All @@ -337,7 +339,8 @@ exports.createContentTypeNodes = ({
entryNode,
entryItemFieldKey,
entryItemFields[entryItemFieldKey],
createNode
createNode,
createNodeId,
)

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

// Create a node for each content type
const contentTypeNode = {
id: contentTypeItemId,
id: createNodeId(contentTypeItemId),
parent: null,
children: [],
name: contentTypeItem.name,
Expand All @@ -382,6 +385,7 @@ exports.createContentTypeNodes = ({
exports.createAssetNodes = ({
assetItem,
createNode,
createNodeId,
defaultLocale,
locales,
}) => {
Expand All @@ -405,7 +409,7 @@ exports.createAssetNodes = ({
: ``,
}
const assetNode = {
id: mId(localizedAsset.sys.id),
id: createNodeId(mId(localizedAsset.sys.id)),
parent: null,
children: [],
...localizedAsset.fields,
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-source-drupal/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const createContentDigest = obj =>

exports.sourceNodes = async (
{ actions, getNode, hasNodeChanged, store, cache },
{ baseUrl, apiBase }
{ baseUrl, apiBase, createNodeId }
) => {
const { createNode } = actions

Expand Down Expand Up @@ -103,7 +103,7 @@ exports.sourceNodes = async (

_.each(contentType.data, datum => {
const node = {
id: datum.id,
id: createNodeId(datum.id),
parent: null,
children: [],
...datum.attributes,
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-source-faker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-source-faker",
"version": "1.0.1",
"version": "2.0.0",
"description": "A gatsby plugin to get fake data for testing",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-source-faker/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const faker = require(`faker`)
const crypto = require(`crypto`)

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

const nodeBase = {
id: JSON.stringify(faker.random.number()),
id: createNodeId(JSON.stringify(faker.random.number())),
parent: null,
children: [],
internal: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const { createFileNode } = require(`../create-file-node`)
// FIXME: This test needs to not use snapshots because of file differences
// and locations across users and CI systems
describe(`create-file-node`, () => {
it(`creates a file node`, () =>
createFileNode(path.resolve(`${__dirname}/fixtures/file.json`), {}))
it(`creates a file node`, () => {
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
return createFileNode(path.resolve(`${__dirname}/fixtures/file.json`), createNodeId, {})
})
})
4 changes: 2 additions & 2 deletions packages/gatsby-source-filesystem/src/create-file-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const createId = path => {

exports.createId = createId

exports.createFileNode = async (pathToFile, pluginOptions = {}) => {
exports.createFileNode = async (pathToFile, createNodeId, pluginOptions = {}) => {
const slashed = slash(pathToFile)
const parsedSlashed = path.parse(slashed)
const slashedFile = {
Expand Down Expand Up @@ -56,7 +56,7 @@ exports.createFileNode = async (pathToFile, pluginOptions = {}) => {
// Don't actually make the File id the absolute path as otherwise
// people will use the id for that and ids shouldn't be treated as
// useful information.
id: createId(pathToFile),
id: createNodeId(pathToFile),
children: [],
parent: `___SOURCE___`,
internal,
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-source-filesystem/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require(`fs`)
const { createId, createFileNode } = require(`./create-file-node`)

exports.sourceNodes = (
{ actions, getNode, hasNodeChanged, reporter },
{ actions, getNode, createNodeId, hasNodeChanged, reporter },
pluginOptions
) => {
const { createNode, deleteNode } = actions
Expand Down Expand Up @@ -36,7 +36,7 @@ Please pick a path to an existing directory.
})

const createAndProcessNode = path =>
createFileNode(path, pluginOptions).then(createNode)
createFileNode(path, createNodeId, pluginOptions).then(createNode)

// For every path that is reported before the 'ready' event, we throw them
// into a queue and then flush the queue when 'ready' event arrives.
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-source-hacker-news/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-source-hacker-news",
"version": "1.0.9",
"version": "2.0.0",
"description": "Gatsby source plugin for building websites using Hacker News as a data source",
"scripts": {
"build": "babel src --out-dir . --ignore **/__tests__/**",
Expand Down
8 changes: 5 additions & 3 deletions packages/gatsby-source-hacker-news/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const get = query =>
`https://www.graphqlhub.com/graphql?query=${encodeURIComponent(query)}`
)

exports.sourceNodes = async ({ actions, getNode, hasNodeChanged }) => {
exports.sourceNodes = async ({ actions, getNode, createNodeId, hasNodeChanged }) => {
const { createNode } = actions

// Do the initial fetch
Expand Down Expand Up @@ -96,6 +96,7 @@ fragment commentsFragment on HackerNewsItem {

const storyNode = {
...kidLessStory,
id: createNodeId(kidLessStory.id),
children: kids.kids.map(k => k.id),
parent: `__SOURCE__`,
content: storyStr,
Expand Down Expand Up @@ -127,6 +128,7 @@ fragment commentsFragment on HackerNewsItem {
}
let commentNode = {
..._.omit(comment, `kids`),
id: createNodeId(comment.id),
children: comment.kids.map(k => k.id),
parent,
internal: {
Expand All @@ -150,12 +152,12 @@ fragment commentsFragment on HackerNewsItem {
createNode(commentNode)

if (comment.kids.length > 0) {
createCommentNodes(comment.kids, comment.id, depth + 1)
createCommentNodes(comment.kids, commentNode.id, depth + 1)
}
})
}

createCommentNodes(kids.kids, story.id)
createCommentNodes(kids.kids, storyNode.id)
})

return
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-source-medium/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gatsby-source-medium",
"description": "Gatsby source plugin for building websites using Medium as a data source",
"version": "1.0.11",
"version": "2.0.0",
"author": "Robert Vogt <robert@smartive.ch>",
"dependencies": {
"axios": "^0.16.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-source-medium/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const convertTimestamps = (nextObj, prevObj, prevKey) => {

const strip = payload => payload.replace(prefix, ``)

exports.sourceNodes = async ({ actions }, { username }) => {
exports.sourceNodes = async ({ actions, createNodeId }, { username }) => {
const { createNode } = actions

try {
Expand Down Expand Up @@ -84,7 +84,7 @@ exports.sourceNodes = async ({ actions }, { username }) => {
const node = Object.assign(
resource,
{
id: resource.id ? resource.id : resource.userId,
id: createNodeId(resource.id ? resource.id : resource.userId),
parent: `__SOURCE__`,
children: [],
internal: {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-source-mongodb/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-source-mongodb",
"version": "1.5.13",
"version": "2.0.0",
"description": "Stub description for gatsby-source-mongodb",
"main": "index.js",
"scripts": {
Expand Down
9 changes: 5 additions & 4 deletions packages/gatsby-source-mongodb/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const createMappingChildNodes = require(`./mapping`)
const _ = require(`lodash`)

exports.sourceNodes = (
{ actions, getNode, hasNodeChanged },
{ actions, getNode, createNodeId, hasNodeChanged },
pluginOptions,
done
) => {
Expand Down Expand Up @@ -32,10 +32,10 @@ exports.sourceNodes = (
let collection = pluginOptions.collection || `documents`
if (_.isArray(collection)) {
for (const col of collection) {
createNodes(db, pluginOptions, dbName, createNode, col, done)
createNodes(db, pluginOptions, dbName, createNode, createNodeId, col, done)
}
} else {
createNodes(db, pluginOptions, dbName, createNode, collection, done)
createNodes(db, pluginOptions, dbName, createNode, createNodeId, collection, done)
}
}
)
Expand All @@ -46,6 +46,7 @@ function createNodes(
pluginOptions,
dbName,
createNode,
createNodeId,
collectionName,
done
) {
Expand All @@ -66,7 +67,7 @@ function createNodes(
var node = {
// Data for the node.
...item,
id: `${id}`,
id: createNodeId(`${id}`),
parent: `__${collectionName}__`,
children: [],
internal: {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-transformer-csv/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-transformer-csv",
"version": "1.3.7",
"version": "2.0.0",
"description": "Gatsby transformer plugin for CSV files",
"main": "index.js",
"scripts": {
Expand Down
Loading

0 comments on commit 613c56b

Please sign in to comment.