gatsby-source-datocms: createNodeField does not create a field

Hi, I am trying to add a field to an existing DatoCMS node but it doesn’t work.

I have added this code in the file gatsby-node.js:

exports.onCreateNode = ({ node, actions }) => {
  const { createNodeField } = actions
  if (node.internal.type === "DatoCmsPerson") {
    // console.log(node) // the node extists
    createNodeField({
      node,
      name: "hello",
      value: "hello!!!",
    })
  }
}

Any ideas why I don’t see the field in GraphiQL?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 31 (11 by maintainers)

Most upvoted comments

May I write down the updated code snippet in the final post so people could reference.

exports.createResolvers = ({
  actions,
  cache,
  createNodeId,
  createResolvers,
  store,
  reporter,
}) => {
  const { createNode } = actions;
  const resolvers = {
    DatoCmsVideoItem: {
      featuredImg: {
        type: `File`,
        resolve: async (source, args, context, info) => {
          return createRemoteFileNode({
            url: source.entityPayload.attributes.video_id,
            createNode,
            createNodeId,
            reporter,
            cache,
            store,
          });
        },
      },
    },
  };
  createResolvers(resolvers);
};

The query above will fetch a copy of the file (file URL specified in video_id) into your local build folder, using createRemoteFileNode, during build-time.

Reopening!

@PatrykRudzinski the caching done by gatsby-source-filesystem is here and here

As you can see, when a file is received with a status of 200, it will be stored in Gatsby’s .cache directory (in this case, .cache/caches/gatsby-source-filesystem). The headers from the file response will be stored in the node cache.

With headers cached, the next request for the same url will use If-None-Match: "<cached etag>". If the file has not changed, the server should respond with a 304. That means a network request is still going out, but it should not be downloading anything. It simply moves on, storing the cached file’s path on the file node in Gatsby’s GraphQL layer here

@PatrykRudzinski I am a free user of the datocms and I built a very small gatsby website so cache is not my priority. I guess @matjack1 would give you a perfect support on your question.

Just my two cent, I guess the caching mechanism, if there is one, should be done by Gatsby. This plugin, for my understanding, is to fetch the content from DatoCMS to your local drive for Gatsby to build.