GatsbyJS 网站非页面组件中的 GraphQL 查询投诉

sta*_*ane 4 reactjs graphql graphql-js jupyter-notebook gatsby

我正在尝试向gatsby-starter-hero-blog添加新模板,但我对新模板的 GraphQL 查询被拒绝:

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
Run Code Online (Sandbox Code Playgroud)

文件夹结构如下:

--src
  --components
  --images
  --pages
  --templates
    --CategoryTemplate.js
    --NotebookTemplate.js
    --PageTemplate.js
    --PostTemplate.js     
  --theme
  --utils
Run Code Online (Sandbox Code Playgroud)

NotebookTemplate.js 是我添加的新模板(用于使用 Nteract 的 Gatsby 插件渲染 Jupyter 笔记本)。

我添加的模板查询的语法与其他模板相同(我在 GraphiQL 中可见的内容中有一个示例笔记本)。

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

我什至尝试使用镜像其他模板之一的简单查询创建准系统模板(甚至尝试使用已更改组件名称的模板的精确副本),但仍然收到相同的警告(随后没有渲染笔记本。例如,PageTemplate.js 具有以下查询(对 gatsby 构建没有任何抱怨)。

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

为什么不在页面或布局文件夹中的文件中的这些查询也不会引发此错误?是否有其他文件允许解决方法?FWIW,这是我试图实现的实际模板。

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
      }
    }
  }
`;
Run Code Online (Sandbox Code Playgroud)

sta*_*ane 10

我能够根据此讨论修复此错误

问题出在gatsby-node.js. 按照exports.createPages设置的方式,永远不会为笔记本创建页面。

我确实发现这个特定的错误消息非常具有误导性,而且 GraphQL 片段的文档对于 Gatsby 来说非常混乱,考虑到大多数入门博客都设置了模板,这些模板不在具有完整 GraphQL 片段的页面/布局文件夹中。

  • 即使模板按预期工作,我也收到此错误。确实是误导! (3认同)
  • 现在讨论是 404。请澄清这个答案。 (3认同)