Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL query complaints in non-page component for GatsbyJS website

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
      }
    }
  }
`;
like image 971
stagermane Avatar asked Aug 18 '18 20:08

stagermane


People also ask

How to import GraphQL data from Gatsby to page component?

Page components inside of /pages directory or templates rendered by the createPage API action can import graphql from the gatsby module and export a pageQuery. In turn, Gatsby would inject a new prop data into the props of the page component containing the resolved data.

How do I run GraphQL queries in non-page components?

Note: To run GraphQL queries in non-page components you’ll need to use Gatsby’s Static Query feature. The following diagram shows a GraphQL query, with each word highlighted in a color corresponding to its name on the legend:

Why doesn't Gatsby have a GraphQL server?

To avoid the overhead of having a server/service that serves data that GraphQL can transform, Gatsby executes GraphQL queries at build time. Data is provided to the components during the build process, making them readily available inside the browser without a server.

What is GraphQL and how does it work?

GraphQL was invented at Facebook to help product engineers pull needed data into React components. GraphQL is a q uery l anguage (the QL part of its name). If you’re familiar with SQL, it works in a very similar way. Using a special syntax, you describe the data you want in your component and then that data is given to you.


1 Answers

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.

like image 86
stagermane Avatar answered Oct 26 '22 09:10

stagermane