Document: How to preprocess data before it is store to Gatsby store ?
chawax opened this issue · 3 comments
Hi,
Is there a way to preprocess data retrieved by WpGraphQL before it is stored to Gatsby GraphQL store ? I am migrating an existing Wordpress site to Gatsby and some posts contents start with HTML code I want to remove.
I could make it in every template using this data. But I find it more efficient to have good data in Gatsby store.
Hey there 👋 this is indeed possible!
It's currently undocumented (I'm working on the docs right now) but you can use the options.type.[typename].beforeChangeNode
api. If you look here you can see an example of how it's being used internally in the plugin to modify MediaItem nodes as they're fetched so we can fetch local files for each of them.
You'll want to make a copy of remoteNode
and then modify it and return it at the end of your beforeChangeNode
function.
return { remoteNode }
Keep in mind this function is run in multiple contexts. When initially creating all nodes, when creating a single node, when updating a single node, or when deleting a single node. You can use the actionType
to determine which context you're in. See the file linked above for an example, the only actionType
not represented there is DELETE
.
Great ! Thanks a lot, I will give it a try !
I'm going to close this as we decided not to document this just yet - for those who really need it they'll be able to find it here but it's a fairly nuanced API so we aren't pushing it as the way to achieve this kind of thing. It is very roughly documented here https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-wordpress/docs/plugin-options.md#type__allbeforechangenode but without an explanation for now.