Is it possible to use the action "createNodeField" from "sourceNodes" API?
See original GitHub issueI have a use case where I want to add information from the last commit that affected a sourced markdown file. To do this I need to fetch the git information by traversing the git history, but to avoid traversing the same history multiple times, I need to know all files for which I need to fetch the git information first, and then traverse the history only once.
To do this I tried to put my logic in “sourceNodes” like this:
// Get all markdown nodes
let mdNodes = getNodes().filter((node) => node.absolutePath && node.absolutePath.endsWith(".md"));
let gitCache = ... code to build git-cache
mdNodes.forEach(n => {
createNodeField({
n,
name: 'git',
value: gitCache.getLatestCommitInfo(n.absolutePath) //Gives last commit info for given markdown file
});
When executing the code I get this error:
TypeError: Cannot read property 'internal' of undefined
In the documentation I can´t see anywhere that the “createNodeField” should not be working from “sourceNodes”.
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (8 by maintainers)
Top Results From Across the Web
Actions | Gatsby
When you implement a Gatsby API, you are passed a collection of actions (equivalent to actions bound with bindActionCreators in Redux) which you...
Read more >Creating nodes in GatsbyJS with two data sources ... - YouTube
... GraphQL nodes from two data sources (an array and an API call). ... nodes in GatsbyJS with two data sources using the...
Read more >Gatsby - fetching remote images with createRemoteFileNode
The idea was to 1) get data from the api 2) grab those image from urls using createRemoteFileNode , so they could be...
Read more >Source remote images the right way with Gatsby v4
All node creation and mutation need to happen during sourceNodes or onCreateNode · Mutation is only allowed using the action createNodeField.
Read more >index.d.ts - UNPKG
This API will change before 2.0 as it needs still to be converted to use ... and [`createNodeField`](https://www.gatsbyjs.org/docs/actions/#createNodeField) ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@david-nossebro I’m looking at a signature of
createNodeField
and it looks like this:Internally it tries to access
internal
property of the destructurednode
but there is nonode
, there isn
instead. Which means that you cannot usen
, butnode: n
instead. Does it fix your issue?Yeah, that’s what I meant. In theory
getNodes()
should already work (see: https://www.gatsbyjs.org/docs/gatsby-repl/#getnodes) but somehow yourn
seems undefined ascreateNodeField
tries to accessnode.internal.type
.