【发布时间】:2019-01-25 12:06:45
【问题描述】:
我正在尝试将新模板添加到 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
文件夹结构如下:
--src
--components
--images
--pages
--templates
--CategoryTemplate.js
--NotebookTemplate.js
--PageTemplate.js
--PostTemplate.js
--theme
--utils
NotebookTemplate.js 是我添加的新模板(用于使用 Nteract 的 Gatsby 插件渲染 Jupyter 笔记本)。
我添加的模板查询的语法与其他模板相同(我在内容中确实有一个示例笔记本,在 GraphiQL 中可见)。
export const query = graphql`
query NotebookQuery($slug: String!) {
jupyterNotebook(fields: { slug: { eq: $slug } }) {
html
internal {
content
}
}
}
`
我什至尝试使用一个简单的查询来镜像其他模板之一(甚至尝试使用更改了组件名称的模板的精确副本)创建一个准系统模板,但仍然收到相同的警告(随后没有渲染笔记本. 例如,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
}
}
}
}
`;
为什么不在页面或布局文件夹中的文件中的这些查询也不会引发此错误?是否有其他文件可以解决问题? 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
}
}
}
`;
【问题讨论】:
标签: reactjs jupyter-notebook graphql graphql-js gatsby