4
votes

I'm trying to add a new template to the gatsby-starter-hero-blog, but my GraphQL query for the new template is being rejected:

warning The GraphQL query in the non-page component 
"/Users/mc/workspaces/mc/src/templates/NoteBookTemplate.js" will not be run.
Queries are only executed for Page or Layout components. Instead of a query,
co-locate a GraphQL fragment and compose that fragment into the query (or other
fragment) of the top-level page or layout that renders this component. 
For more
info on fragments and composition see: 
http://graphql.org/learn/queries/#fragments

The folder structure is as so:

--src
  --components
  --images
  --pages
  --templates
    --CategoryTemplate.js
    --NotebookTemplate.js
    --PageTemplate.js
    --PostTemplate.js     
  --theme
  --utils

NotebookTemplate.js is the new template I'm adding (for rendering Jupyter notebooks using Nteract's Gatsby plugin).

The syntax of my added template query is identical to the other templates (and I do have a sample notebook in the content which is visible in GraphiQL).

export const query = graphql`
  query NotebookQuery($slug: String!) {
    jupyterNotebook(fields: { slug: { eq: $slug } }) {
      html
      internal {
        content
      }
    }
  }
` 

I even tried creating a barebones template with a simple query mirroring one of the other templates (even trying an exact copy of a template with the component names changed) and still am getting the same warning (and subsequently no rendering of the notebook. For example, the PageTemplate.js has the following query (which gives no complaints on gatsby build).

export const pageQuery = graphql`
  query PageByPath($slug: String!) {
    page: markdownRemark(fields: { slug: { eq: $slug } }) {
      id
      html
      frontmatter {
        title
      }
    }
    site {
      siteMetadata {
        facebook {
          appId
        }
      }
    }
  }
`;

Why are these queries in files not in the pages or layout folder not also throwing this error? Is there some other file that allows a workaround? FWIW, This is the actual template I'm trying to implement.

import React from 'react'
import PropTypes from "prop-types";
import 'katex/dist/katex.min.css'
import { ThemeContext } from "../layouts";

const NotebookTemplate = ({ data }) => {
  const post = data.jupyterNotebook
  const notebookJSON = JSON.parse(post.internal.content)
  return (
    <React.Fragment>
        <p>
          This notebook is displayed in the <strong>client-side</strong> using
          react component
          <code>NotebookPreview</code>
          from
          <a href="https://github.com/nteract/nteract/tree/master/packages/notebook-preview">
            <code>@nteract/notebook-preview</code>.
          </a>
        </p>
        <NotebookPreview notebook={notebookJSON} />
    </React.Fragment>
  );
};

NotebookTemplate.propTypes = {
  data: PropTypes.object.isRequired
};

export default NotebookTemplate;

export const query = graphql`
  query NotebookQuery($slug: String!) {
    jupyterNotebook(fields: { slug: { eq: $slug } }) {
      html
      internal {
        content
      }
    }
  }
`;
4

4 Answers

10
votes

I was able to fix this error based on this discussion.

The problem was in gatsby-node.js. The way that exports.createPages was set up, pages were never being created for the notebooks.

I do find this particular error message very misleading, and the documentation for GraphQL fragments is very confusing for Gatsby, considering most of the starter blogs are set up with templates that are not in page/layout folders that have intact GraphQL fragments.

3
votes

I know that this is old, but I also ran into this same issue, and the answers here did not help me.

Since Gatsby uses the debug package, what helped me track it down was by setting debug's Node logging namespace environment to gatsby:query-watcher as specified in query-watcher.ts.

If anyone else stubmles on this just run your develop server like so and it should help you track it down easier:

env "NODE_ENV=development" "DEBUG=gatsby:query-watcher" npm run dev

0
votes

I was following this Gatsby Blog tutorial when I ran into a similar issue. I got to the part where I added code to gatsby-node.js. After saving the file, I was expecting to be able to browse to my blog posts, but instead I got the warning below:

The GraphQL query in the non-page component "C:/GitRepos/gatsby-blog-starter/src/templates/blogPost.js" will not be run.

In my case, the solution does not make any sense, but I'm sharing it here because it did fix the issue for me.

What I did was comment out the whole code for postQuery, which has the graphql query, in the blogPost.js file. After saving the file and running gatsby develop, I no longer got the warning above. But when I tried to navigate to the blog post, Gatsby said that it could not access the value for the property markdownRemark.

So, I uncommented the whole code for postQuery in the blogPost.js file. Then after saving the file, everything just worked. The warning no longer shows up and I could navigate to the blog posts I created without any issues.

I know, it doesn't make sense. But my issue is fixed after commenting and uncommenting the code that had the graphql query in it.

0
votes

For me the problem was indeed in gatsby-node.js (the createPages function) as referred to by stagermane, however the reason mine was failing is that the allMarkdownRemark had a limit of 1000 and I now have more than 1000 pages.

I simply changed it to a higher number, in this case 5000 as shown below and it worked!

allMarkdownRemark(limit: 5000) {...}