GatsbyJS: How to query result of transformer plugin?

Viewed 332

For learning purposes I wrote a Gatsby transformer that reverses node content. I use it with gatsby-source-filesystem, only operating on .test files:

const crypto = require(`crypto`)

module.exports = function onCreateNode({ node, loadNodeContent, boundActionCreators }) {
  const { createNode, createParentChildLink } = boundActionCreators;

  if (node.extension !== 'test') {
    return;
  }

  const nodeContent = loadNodeContent(node);
  const nodeContentRev = nodeContent.split('').reverse().join('');
  const contentDigest = crypto
      .createHash(`md5`)
      .update(JSON.stringify(nodeContent))
      .digest(`hex`);
  const RevNode = {
    'contentRev': nodeContentRev,

    id: '9087657865-just-something-here-for-now',
    parent: null,
    children: [],
    internal: {
      type: 'Rev',
      contentDigest,
      content,
    },
  };

  createNode(RevNode);
}

Unfortunately, I am still very new to Gatsby, so I am struggling to find out if this is working. Taking a look at the official transformers, they not only implement gatsby-node.js (thats what I have), but also extend-node-type.js, but I couldn't find any documentation on this.

How can I query the result of my transformer?

1 Answers

Assuming you first by verifying the following:

  1. Your code gets loaded and runs without raising an exception. If not, first verify this with console.log messages or debugger.
  2. Your source plugin is configured correctly and is picking up files on it's configured directory.

Assuming both are verified and the transformer plugin gets loaded and runs without issue, you would be creating new nodes in gatsby's data layer. The data layer can be explored using graphql.

Access locally in http://localhost:8000/___graphql after you run gatsby develop (see this gatsby tutorial)

Related