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

Created on 13 Jan 2020  路  11Comments  路  Source: gatsbyjs/gatsby

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".

stale? bug

Most helpful comment

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.

All 11 comments

Hi, thanks for the issue! Can you link a reproduction repository?

In the meantime, could you please try using the onCreateNode API? You can check for node.internal.type === 'MarkdownRemark' to get all markdown files and then push those nodes into an array to use createNodeField.

@LekoArts, thanks for your response. I麓ll try to compile a reproduction repo this afternoon.

If I push all nodes to an array during "onCreateNode", when do I trigger the "createNodeField" functionality? I can only create the git info cache when all markdown nodes are added to the array, and there is no indication of that the "onCreateNode" is called the last time. The next step from my understanding would be "sourceNodes"? Do you mean that I should store the nodes during "onCreateNode" instead of fetching them with the "getNodes()" function?

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.

In the branch below I tried to collect the nodes in 'onCreateNode' instead, but got the same issue:
https://github.com/david-nossebro/gatsby-createNodeField-in-sourceNodes/tree/load-nodes-in-oncreatenode

However, it seems like 'sourceNodes' is called before 'onCreateNode' is finished? I had to use a sleep method, otherwise the array of nodes was not populated in 'sourceNodes'. I think this might be a bug too, or at least different from what the documentation say for 'sourceNodes':

If you define this hook in gatsby-node.js it will be called exactly once after all of your source plugins have finished creating nodes.

Hiya!

This issue has gone quiet. Spooky quiet. 馃懟

We get a lot of issues, so we currently close issues after 30 days of inactivity. It鈥檚 been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 馃挭馃挏

I still need this fix (or a workaround).

Still needed.

@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?

Hiya!

This issue has gone quiet. Spooky quiet. 馃懟

We get a lot of issues, so we currently close issues after 30 days of inactivity. It鈥檚 been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 馃挭馃挏

Hey again!

It鈥檚 been 30 days since anything happened on this issue, so our friendly neighborhood robot (that鈥檚 me!) is going to close it.
Please keep in mind that I鈥檓 only a robot, so if I鈥檝e closed this issue in error, I鈥檓 HUMAN_EMOTION_SORRY. Please feel free to reopen this issue or create a new one if you need anything else.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks again for being part of the Gatsby community! 馃挭馃挏

Was this page helpful?
0 / 5 - 0 ratings