简体   繁体   中英

How to validate graphql in gatsby blog site

I am trying to build a blog site in Gatsby and everything was working perfectly until I delete my fake tester blog posts.

My error is :

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

So my question is how do I make sure this is valid before continuing?

    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
            });
        });
    });

Make sure you are telling gatsby where your markdown content files live

in your gatsby-config.js , make sure the plugins array contains the following:

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

which is just an example if your markdown lives in a content directory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM