Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby): Remove old touchNode signature #29245

Merged
merged 9 commits into from
Feb 10, 2021
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
4 changes: 2 additions & 2 deletions docs/docs/creating-a-source-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ exports.createSchemaCustomization = ({ actions }) => {
author: Author @link(from: "author.name" by: "name") // highlight-line
# ... other fields
}

type Author implements Node {
name: String!
post: Post @link // highlight-line
Expand Down Expand Up @@ -437,7 +437,7 @@ exports.sourceNodes = async ({ actions, getNodesByType }, pluginOptions) => {

// highlight-start
// touch nodes to ensure they aren't garbage collected
getNodesByType(`YourSourceType`).forEach(node => touchNode({ nodeId: node.id }))
getNodesByType(`YourSourceType`).forEach(node => touchNode(node)

// ensure a plugin is in a preview mode and/or supports listening
if (pluginOptions.preview) {
Expand Down
16 changes: 8 additions & 8 deletions docs/docs/how-to/plugins-and-themes/creating-a-source-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -764,9 +764,9 @@ exports.sourceNodes = async (

// highlight-start
// touch nodes to ensure they aren't garbage collected
getNodesByType(POST_NODE_TYPE).forEach(node => touchNode({ nodeId: node.id }))
getNodesByType(POST_NODE_TYPE).forEach(node => touchNode(node)
getNodesByType(AUTHOR_NODE_TYPE).forEach(node =>
touchNode({ nodeId: node.id })
touchNode(node)
)
// highlight-end

Expand All @@ -786,9 +786,9 @@ exports.sourceNodes = async (
const { createNode, touchNode, deleteNode } = actions

// touch nodes to ensure they aren't garbage collected
getNodesByType(POST_NODE_TYPE).forEach(node => touchNode({ nodeId: node.id }))
getNodesByType(POST_NODE_TYPE).forEach(node => touchNode(node)
getNodesByType(AUTHOR_NODE_TYPE).forEach(node =>
touchNode({ nodeId: node.id })
touchNode(node)
)

// highlight-start
Expand All @@ -811,9 +811,9 @@ exports.sourceNodes = async (
const { createNode, touchNode, deleteNode } = actions

// touch nodes to ensure they aren't garbage collected
getNodesByType(POST_NODE_TYPE).forEach(node => touchNode({ nodeId: node.id }))
getNodesByType(POST_NODE_TYPE).forEach(node => touchNode(node)
getNodesByType(AUTHOR_NODE_TYPE).forEach(node =>
touchNode({ nodeId: node.id })
touchNode(node)
)

if (pluginOptions.previewMode) {
Expand Down Expand Up @@ -854,9 +854,9 @@ exports.sourceNodes = async (
const { createNode, touchNode, deleteNode } = actions

// touch nodes to ensure they aren't garbage collected
getNodesByType(POST_NODE_TYPE).forEach(node => touchNode({ nodeId: node.id }))
getNodesByType(POST_NODE_TYPE).forEach(node => touchNode(node)
getNodesByType(AUTHOR_NODE_TYPE).forEach(node =>
touchNode({ nodeId: node.id })
touchNode(node)
)

if (pluginOptions.previewMode) {
Expand Down
4 changes: 2 additions & 2 deletions examples/creating-source-plugins/source-plugin/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ exports.sourceNodes = async function sourceNodes(
console.log(await cache.get(`hello`))

// touch nodes to ensure they aren't garbage collected
getNodesByType(POST_NODE_TYPE).forEach(node => touchNode({ nodeId: node.id }))
getNodesByType(POST_NODE_TYPE).forEach(node => touchNode(node)
getNodesByType(AUTHOR_NODE_TYPE).forEach(node =>
touchNode({ nodeId: node.id })
touchNode(node)
)

// listen for updates using subscriptions from the API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const downloadContentfulAssets = async gatsbyFunctions => {
getNodesByType,
reporter,
assetDownloadWorkers,
getNode,
} = gatsbyFunctions

// Any ContentfulAsset nodes will be downloaded, cached and copied to public/static
Expand Down Expand Up @@ -75,7 +76,7 @@ const downloadContentfulAssets = async gatsbyFunctions => {
// to compare a modified asset to a cached version?
if (cacheRemoteData) {
fileNodeID = cacheRemoteData.fileNodeID // eslint-disable-line prefer-destructuring
touchNode({ nodeId: cacheRemoteData.fileNodeID })
touchNode(getNode(cacheRemoteData.fileNodeID))
}

// If we don't have cached data, download the file
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-source-contentful/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ exports.sourceNodes = async (
store,
cache,
getCache,
getNode,
getNodesByType,
reporter,
assetDownloadWorkers: pluginConfig.get(`assetDownloadWorkers`),
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-source-drupal/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ exports.sourceNodes = async (
// Touch nodes so they are not garbage collected by Gatsby.
getNodes().forEach(node => {
if (node.internal.owner === `gatsby-source-drupal`) {
touchNode({ nodeId: node.id })
touchNode(node)
}
})

Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-source-shopify/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const sourceNodes = async (
cache,
getCache,
reporter,
getNode,
},
{
shopName,
Expand Down Expand Up @@ -89,6 +90,7 @@ export const sourceNodes = async (
store,
cache,
getCache,
getNode,
reporter,
downloadImages,
}
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby-source-shopify/src/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const downloadImageAndCreateFileNode = async (
store,
cache,
getCache,
getNode,
reporter,
downloadImages,
}
Expand All @@ -42,7 +43,7 @@ const downloadImageAndCreateFileNode = async (

if (cacheMediaData) {
const fileNodeID = cacheMediaData.fileNodeID
touchNode({ nodeId: fileNodeID })
touchNode(getNode(fileNodeID))
return fileNodeID
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const persistPreviouslyCachedImages: Step = async (): Promise<void> => {

// and touch them so they aren't garbage collected.
// we will remove them as needed when receiving DELETE events from WP

// TODO: In order to be able to remove the ts-ignore, update to new touchNode signature
// @ts-ignore
mediaItemNodes.forEach(({ id }) => helpers.actions.touchNode({ nodeId: id }))

const imageNodeMetaByUrl = await getPersistentCache({
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby-transformer-screenshot/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ exports.onPreBootstrap = (
actions,
createNodeId,
getCache,
getNode,
getNodesByType,
createContentDigest,
reporter,
Expand Down Expand Up @@ -61,7 +62,7 @@ exports.onPreBootstrap = (
} else {
// Screenshot hasn't yet expired, touch the image node
// to prevent garbage collection
touchNode({ nodeId: n.screenshotFile___NODE })
touchNode(getNode(n.screenshotFile___NODE))
}
})

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ export interface Actions {
): void

/** @see https://www.gatsbyjs.org/docs/actions/#touchNode */
touchNode(node: { nodeId: string }, plugin?: ActionPlugin): void
touchNode(node: NodeInput, plugin?: ActionPlugin): void

/** @see https://www.gatsbyjs.org/docs/actions/#createNodeField */
createNodeField(
Expand Down
34 changes: 34 additions & 0 deletions packages/gatsby/src/db/__tests__/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,40 @@ describe(`nodes db tests`, () => {
store.dispatch({ type: `DELETE_CACHE` })
})

it(`warns when using old touchNode signature `, () => {
store.dispatch(
actions.createNode(
{
id: `hi`,
children: [],
parent: `test`,
internal: {
contentDigest: `hasdfljds`,
type: `Test`,
},
},
{
name: `tests`,
}
)
)
expect(getNode(`hi`)).toMatchObject({ id: `hi` })
store.dispatch(
actions.touchNode(
{ nodeId: `hi` },
{
name: `tests`,
}
)
)
expect(getNode(`hi`)).toBeDefined()
const deprecationNotice =
`Calling "touchNode" with an object containing the nodeId is deprecated. Please pass ` +
`the node directly to the function: touchNode(node) ` +
`"touchNode" was called by tests`
expect(report.warn).toHaveBeenCalledWith(deprecationNotice)
})

it(`deletes previously transformed children nodes when the parent node is updated`, () => {
store.dispatch(
actions.createNode(
Expand Down
28 changes: 13 additions & 15 deletions packages/gatsby/src/redux/actions/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,36 +836,34 @@ actions.createNode = (...args) => dispatch => {
* nodes from a remote system that can return only nodes that have
* updated. The source plugin then touches all the nodes that haven't
* updated but still exist so Gatsby knows to keep them.
* @param {Object} $0
* @param {string} $0.nodeId The id of a node
* @param {Object} node A node object. See the "createNode" action for more information about the node object details.
* @example
* touchNode({ nodeId: `a-node-id` })
* touchNode(node)
*/
actions.touchNode = (options: any, plugin?: Plugin) => {
let nodeId = _.get(options, `nodeId`)

// Check if using old method signature. Warn about incorrect usage
if (typeof options === `string`) {
console.warn(
`Calling "touchNode" with a nodeId is deprecated. Please pass an object containing a nodeId instead: touchNode({ nodeId: 'a-node-id' })`
)
actions.touchNode = (node: any, plugin?: Plugin) => {
// TODO(v4): Remove this deprecation warning and only allow touchNode(node)
if (node && node.nodeId) {
let msg =
`Calling "touchNode" with an object containing the nodeId is deprecated. Please pass ` +
`the node directly to the function: touchNode(node)`

if (plugin && plugin.name) {
console.log(`"touchNode" was called by ${plugin.name}`)
msg = msg + ` "touchNode" was called by ${plugin.name}`
}

nodeId = options
report.warn(msg)

node = getNode(node.nodeId)
}

const node = getNode(nodeId)
if (node && !typeOwners[node.internal.type]) {
typeOwners[node.internal.type] = node.internal.owner
}

return {
type: `TOUCH_NODE`,
plugin,
payload: nodeId,
payload: node.id,
}
}

Expand Down