【问题标题】:How to validate graphql in gatsby blog site如何在 gatsby 博客站点中验证 graphql
【发布时间】:2019-12-25 13:35:55
【问题描述】:

我正在尝试在 Gatsby 中建立一个博客网站,在我删除我的虚假测试人员博客文章之前,一切都运行良好。

我的错误是:

gatsby-node.js" threw an error while running the createPages lifecycle:

Cannot query field "frontmatter" on type "MarkdownRemark".

GraphQL request:8:13
7 |             id
8 |             frontmatter {
  |             ^
9 |               path

所以我的问题是如何在继续之前确保它是有效的?

    const { createPage } = actions;

    const postTemplate = path.resolve('src/templates/blog-post.js');

    return graphql(`
    {
      allMarkdownRemark {
        edges {
          node {
            html
            id
            frontmatter {
              path
              title
              date
              featuredImage
            }
          }
        }
      }
    }
  `).then((res) => {
        if (res.errors) {
            return Promise.reject(res.errors);
        }

        if (res.data === undefined) {//Validation attempt
            return;
        }
        res.data.allMarkdownRemark.edges.forEach(({ node }) => {
            createPage({
                path      : node.frontmatter.path,
                component : postTemplate
            });
        });
    });

【问题讨论】:

  • 你有markdown文件吗?
  • 我确实有,而且效果很好。但它们是测试人员的博客文章。如果我 没有 有任何降价文件,我正在尝试找出跳过 createpages 代码的位置。
  • 这似乎是code challenges 之一,您花时间在上面,然后再也不需要了。务实。一旦你写了你的第一篇博文,这个错误是否会再次发生?

标签: javascript graphql gatsby


【解决方案1】:

确保你告诉 gatsby 你的 markdown 内容文件在哪里

在您的 gatsby-config.js 中,确保 plugins 数组包含以下内容:

    {
      resolve: 'gatsby-source-filesystem',
      options: {
        path: `${__dirname}/content`,
        name: 'content',
      },
    },

如果您的降价位于内容目录中,这只是一个示例

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 2021-10-30
    • 2020-04-16
    • 2016-05-18
    • 2019-07-05
    • 1970-01-01
    • 2016-03-18
    相关资源
    最近更新 更多