3

I am using node v16.0.0, Gatsby v3.6.1 and Yarn v1.22.10 as the dependency manager with plugins:

I have used the following function on file gatsby-node.js to create nodes in GraphQL with images that can be queried later on:

// gatsby-node.js
const { createRemoteFileNode } = module.require(`gatsby-source-filesystem`)

exports.onCreateNode = async ({   node,
  actions: { createNode },
  store,
  cache,
  createNodeId,
 }) => {
  if (node.internal.type === `API__images` && node.pk) {
    let fileNode = await createRemoteFileNode({
      url: node.url, // string that points to the URL of the image
      parentNodeId: node.id, // id of the parent node of the fileNode you are going to create
      createNode, // helper function in gatsby-node to generate the node
      createNodeId, // helper function in gatsby-node to generate the node
      cache, // Gatsby's cache
      store, // Gatsby's Redux store
    })
    // if the file was created, attach the new node to the parent node
    if (fileNode) {
      node.image___NODE = fileNode.id
    }
  }
}

This seems to work normally when running gatsby develop or gatsby build but if I try to run again any of those commands (with same source data) the following error is raised:

Missing onError handler for invocation 'building-schema', 
error was Invariant Violation: Encountered an error 
trying to infer a GraphQL type for: `image___NODE`. 
There is no corresponding node with the `id` field matching: 

"27564a59-be49-51fb-98d6-c32de4f2030c",
"379357c0-1faa-5177-806d-7f155f2e3e85",

...

Those 27564a59-b.., 379357c0-1.. are the image node ids that were created on the gatsby-node.js function.

If I run gatsby clean it will work normally, but using gatsby clean is not a good solution because cleaning cache will break incremental builds.

Does somebody know how to fix this error? should I use fixed ids for the nodes?

Traceback error:

(/my-project/node_modules/invariant/invariant.js:40:15)
    at getFieldConfigFromFieldNameConvention (/my-project/node_modules
/gatsby/src/schema/infer/add-inferred-fields.js:227:3)
    at getFieldConfig (/my-project/node_modules/gatsby/src/schema/infe
r/add-inferred-fields.js:129:19)
    at forEach (/my-project/node_modules/gatsby/src/schema/infer/add-i
nferred-fields.js:79:25)
    at Array.forEach (<anonymous>)
    at addInferredFieldsImpl (/my-project/node_modules/gatsby/src/sche
ma/infer/add-inferred-fields.js:63:28)
    at addInferredFields (/my-project/node_modules/gatsby/src/schema/i
nfer/add-inferred-fields.js:27:3)
    at addInferredType
(/my-project/node_modules/gatsby/src/schema/infer/index.js:101:3)
    at map
(/my-project/node_modules/gatsby/src/schema/infer/index.js:65:5)
    at Array.map (<anonymous>)
    at addInferredTypes
(/my-project/node_modules/gatsby/src/schema/infer/index.js:64:23)
    at updateSchemaComposer
(/my-project/node_modules/gatsby/src/schema/schema.js:169:9)
    at buildSchema
(/my-project/node_modules/gatsby/src/schema/schema.js:64:3)
    at build
(/my-project/node_modules/gatsby/src/schema/index.js:105:18)
    at buildSchema
(/my-project/node_modules/gatsby/src/services/build-schema.ts:19:3)'
Ander
  • 5,093
  • 7
  • 41
  • 70

1 Answers1

0

It seems to be a pretty fresh topic on this GitHub thread.

If cleaning the cache is not a solution for your use-case it seems that the issue can be fixed moving from npm to yarn as a dependency manager.

In addition, try also removing all your node_modules and your locked dependencies by:

rm -rf node_modules && rm -rf package-lock.json

To make a fresh install.


According to the error prompted I would try wrapping your node creation inside a try/catch:

// gatsby-node.js
const { createRemoteFileNode } = module.require(`gatsby-source-filesystem`)

exports.onCreateNode = async ({   node,
  actions: { createNode },
  store,
  cache,
  createNodeId,
 }) => {
  let fileNode;
 try{
  if (node.internal.type === `API__images` && node.pk) {
    fileNode = await createRemoteFileNode({
      url: node.url, // string that points to the URL of the image
      parentNodeId: node.id, // id of the parent node of the fileNode you are going to create
      createNode, // helper function in gatsby-node to generate the node
      createNodeId, // helper function in gatsby-node to generate the node
      cache, // Gatsby's cache
      store, // Gatsby's Redux store
    })
  } catch (e) {
      console.log(e)
    }
    // if the file was created, attach the new node to the parent node
    if (fileNode) {
      node.image___NODE = fileNode.id
    }
  }
}

I know you said that running a gatsby clean is not an option, but when you are changing the core nodes you may be forced to run one. Once your project is done (first build), the next builds will get and use your cache.

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
  • 1
    I am already using Yarn as the dependency manager @Ferran Buireu (I have updated the question). I have tried your suggestion just in case, but doesn't fix the issue. The same error raised, but thanks anyway, your comments in Stackoverflow saved my plenty of time on many ocasions. – Ander May 31 '21 at 06:12
  • Thanks for the feedback :). I will remove the answer if doesn't provide any useful workaround. Just in case... have you tried updating your Node version? It seems you are using kind of old version (the latest LTS is 14.17). – Ferran Buireu May 31 '21 at 06:14
  • yes sorry, I am using node `v16.0.0`, I put `npm --version` by mistake – Ander May 31 '21 at 06:16
  • Have you tried wrapping your node creation inside a `try`/`catch`? (because of the `onError`)? – Ferran Buireu May 31 '21 at 06:23
  • Used `createNodeId: id => 'Image${node.image_id}'` to define fixed ids but same error is thrown: `There is no corresponding node with the "id" field matching: "Image95,Image96 ...` – Ander Jun 21 '21 at 08:37