Hello,
i am trying to add a custom field in the gatsby-node.js to a node-type created by the source-plugin and the wp-graphql plugin for CPT UI.
Any clue what i am doing wrong here, or what to change?
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `WpEvent`) {
createNodeField({
node,
name: "someFieldName",
value: "someValue",
})
}
}
Thanks in advance!
@vladar is this possible when using schema customization and fully turning off inference?
Nope 馃槵 But we should probably support inference for fields even when it is disabled for the node type as this is a legit way to extend existing node.
I think the workaround is to declare a type like this in your gatsby-node:
exports.createSchemaCustomization = ({ actions }) => {
actions.createTypes(`
type WpEventFields {
someFieldName: String
}
`)
}
I ran into this problem and was able to resolve it with the workaround @vladar suggested. The important thing to know is that you don't need to use createNodeField and can instead directly update the node. See this simple code example:
exports.onCreateNode = async ({ node, actions }) => {
if (node.internal.type == `WpMyPostType`) {
// Save the custom content to the field on the node.
node.myCustomField = 'test'
}
}
exports.createSchemaCustomization = ({ actions }) => {
actions.createTypes(`
type WpMyPostType {
myCustomField: String
}
`)
}
Sorry, forgot to reply. Thanks to you both @vladar @smthomas , this totally worked for me.
I had the use case where i got provided with poorly formated "dates" for events out of the database.
exports.onCreateNode = ({ node }) => {
if (node.internal.type === `WpEvent`) {
node.startDate = parse(node.eventFields.startDate, "d.M.y", new Date())
}
}
exports.createSchemaCustomization = ({ actions }) => {
actions.createTypes(`
type WpEvent {
startDate: Date
}
`)
}
Glad to hear this solved your issue. Thanks everyone!