question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to use the action "createNodeField" from "sourceNodes" API?

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
jlkiricommented, Feb 13, 2020

@david-nossebro I’m looking at a signature of createNodeField and it looks like this:

  { node, name, value, fieldName, fieldValue }: CreateNodeInput,
  plugin: Plugin,
  actionOptions?: ActionOptions

Internally it tries to access internal property of the destructured node but there is no node, there is n instead. Which means that you cannot use n, but node: n instead. Does it fix your issue?

2reactions
LekoArtscommented, Jan 13, 2020

Yeah, that’s what I meant. In theory getNodes() should already work (see: https://www.gatsbyjs.org/docs/gatsby-repl/#getnodes) but somehow your n seems undefined as createNodeField tries to access node.internal.type.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found