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-source-wordpress): allow path to js file for beforeChangeNode option #32901

Merged
merged 4 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -4380,6 +4380,7 @@ Array [
"title",
"uri",
"nodeType",
"beforeChangeNodeTest",
"parent",
"children",
"internal",
Expand Down
1 change: 1 addition & 0 deletions integration-tests/gatsby-source-wordpress/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const wpPluginOptions = !process.env.DEFAULT_PLUGIN_OPTIONS
},
Page: {
excludeFieldNames: [`enclosure`],
beforeChangeNode: `./src/before-change-page.js`,
},
DatabaseIdentifier: {
exclude: true,
Expand Down
9 changes: 9 additions & 0 deletions integration-tests/gatsby-source-wordpress/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type WpPage {
beforeChangeNodeTest: String
}
`
createTypes(typeDefs)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = ({ remoteNode }) => {
remoteNode.beforeChangeNodeTest = `TEST-${remoteNode.id}`

return remoteNode
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,26 @@ describe(`data resolution`, () => {
expect(result.data.testUser.name).toEqual(`admin`)
})

it(`resolves data added via a fn file in onBeforeChangeNode type option`, async () => {
const result = await fetchGraphql({
url,
query: /* GraphQL */ `
{
allWpPage {
nodes {
id
beforeChangeNodeTest
}
}
}
`,
})

result.data.allWpPage.nodes.forEach(node => {
expect(node.beforeChangeNodeTest).toBe(`TEST-${node.id}`)
})
})

it(`resolves root fields`, async () => {
const result = await fetchGraphql({
url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ describe(`pluginOptionsSchema`, () => {
MenuItem: {
beforeChangeNode: null,
},
Page: {
beforeChangeNode: `./docs-generation.test.js`,
},
EnqueuedScript: {
exclude: true,
},
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-source-wordpress/docs/plugin-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1107,9 +1107,9 @@ Determines whether or not this type will be treated as an interface comprised en

#### type.\_\_all.beforeChangeNode

A function which is invoked before a node is created, updated, or deleted. This is a hook in point to modify the node or perform side-effects related to it.
A function which is invoked before a node is created, updated, or deleted. This is a hook in point to modify the node or perform side-effects related to it. This option should be a path to a JS file where the default export is the beforeChangeNode function. The path can be relative to your gatsby-node.js or absolute. Currently you can inline a function by writing it out directly in this option but starting from Gatsby v4 only a path to a function file will work.
TylerBarnes marked this conversation as resolved.
Show resolved Hide resolved

**Field type**: `Function`
**Field type**: `String`

### type.RootQuery

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,11 @@ const pluginOptionsSchema = ({ Joi }) => {
}
`),
}),
beforeChangeNode: Joi.any()
beforeChangeNode: Joi.string()
.allow(null)
.allow(false)
.meta({
trueType: `function`,
})
.description(
`A function which is invoked before a node is created, updated, or deleted. This is a hook in point to modify the node or perform side-effects related to it.`
`A function which is invoked before a node is created, updated, or deleted. This is a hook in point to modify the node or perform side-effects related to it. This option should be a path to a JS file where the default export is the beforeChangeNode function. The path can be relative to your gatsby-node.js or absolute. Currently you can inline a function by writing it out directly in this option but starting from Gatsby v4 only a path to a function file will work.`
TylerBarnes marked this conversation as resolved.
Show resolved Hide resolved
),
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "path"
import { formatLogMessage } from "~/utils/format-log-message"
import isInteger from "lodash/isInteger"
import { IPluginOptions } from "~/models/gatsby-api"
Expand Down Expand Up @@ -46,6 +47,51 @@ const optionsProcessors: Array<IOptionsProcessor> = [

delete userPluginOptions.schema.queryDepth

return userPluginOptions
},
},
{
name: `Require beforeChangeNode type setting functions by absolute or relative path`,
test: ({ userPluginOptions }: IProcessorOptions): boolean =>
!!userPluginOptions?.type,
processor: ({
helpers,
userPluginOptions,
}: IProcessorOptions): IPluginOptions => {
const gatsbyStore = helpers.store.getState()
const typeSettings = Object.entries(userPluginOptions.type)

typeSettings.forEach(([typeName, settings]) => {
const beforeChangeNodePath = settings?.beforeChangeNode

if (!beforeChangeNodePath || typeof beforeChangeNodePath !== `string`) {
return
}

try {
const absoluteRequirePath: string | undefined = path.isAbsolute(
beforeChangeNodePath
)
? beforeChangeNodePath
: require.resolve(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wardpeet you mentioned gatsby-fs. Is that available now or will this need to be updated in the future?

path.join(gatsbyStore.program.directory, beforeChangeNodePath)
)

const beforeChangeNodeFn = require(absoluteRequirePath)

if (beforeChangeNodeFn) {
userPluginOptions.type[typeName].beforeChangeNode =
beforeChangeNodeFn
}
} catch (e) {
helpers.reporter.panic(
formatLogMessage(
`beforeChangeNode type setting for ${typeName} threw error:\n${e.message}`
)
)
}
})

return userPluginOptions
},
},
Expand Down