基本上,GraphQL 游乐场 (localhost:8000/___graphql) 告诉您您没有创建 allMdx 节点。所有可用节点的列表位于左列(allFile、allDirectory 等)。
您需要设置文件系统以允许 Gatsby 通过以下方式推断该节点:
npm install gatsby-source-filesystem
那么,在你的gatsby-config.js:
module.exports = {
siteMetadata: {
title: "My First Gatsby Site",
},
plugins: [
"gatsby-plugin-gatsby-cloud",
"gatsby-plugin-image",
"gatsby-plugin-sharp",
{
resolve: "gatsby-source-filesystem",
options: {
name: `blog`,
path: `${__dirname}/blog`, // <-- the folder where you have the .mdx files
}
},
],
};
如果您在/blog 文件夹中有一些 MDX 文件,Gatsby 将生成正确的节点 (allMdx),以便您查询它们。
通过gatsby clean && gatsby develop重新启动并清理您的服务器。
接下来的步骤是通过添加一些配置来查询数据,例如:
{
resolve: `gatsby-plugin-mdx`,
options: {
defaultLayouts: {
posts: require.resolve("./src/components/blog-layout.js"),
default: require.resolve("./src/components/layout.js"),
},
},
},
注意:您需要先安装gatsby-plugin-mdx 插件
您可以查看以下教程:https://www.digitalocean.com/community/tutorials/gatsbyjs-mdx-in-gatsby