繁体   English   中英

Gatsby GraphQL 中用于自定义查询的未知字段错误

[英]Unknow field error in Gatsby GraphQL for custom query

我正在使用带有 GraphQL 的 Gatsby。 获取未知字段错误如下:

GraphQL Error Encountered 2 error(s):
- Unknown field 'allBlogPost' on type 'Query'. Source: document `homeAymanDesktopFirstSrcPagesIndexJs2258072279` file: `GraphQL request`
  
  GraphQL request:3:3
  2 |   {
  3 |       posts: allBlogPost {
    |   ^
  4 |           nodes {
- Unknown field 'blogPost' on type 'Query'.

      file: /home/ayman/Desktop/first/src/templates/post.js

这是我的post.js模板文件:

import React from "react"
import { graphql } from "gatsby"

export default ({ data }) => {
    console.log(data)
    return (
        <div>
            <h1>{data.post.title}</h1>
            <div dangerouslySetInnerHTML={{ __html: data.post.html }} />
        </div>
    )
}

export const query = graphql`
    query($id: String!) {
        post: blogPost(id: { eq: $id }) {
            title
            html
        }
    }
`

我的gatsby-node.js配置可以在这个pastebin中找到

我的 GraphiQL:

在此处输入图像描述


更新

content/postscontent/articles添加 Asciidoc 文件后,出现此错误:

Cannot query field "slug" on type "BlogPost".

File: gatsby-node.js:89:16


 ERROR #11321  PLUGIN

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

Cannot read property 'posts' of undefined



  TypeError: Cannot read property 'posts' of undefined
  
  - gatsby-node.js:99 


您的代码在gatsby-node.js中中断,而不是在模板中(至少目前如此)。

尝试在您的createPage API 中使用async / await方法,例如:

exports.createPages = async ({ graphql, actions }) => {
    const { createPage } = actions
    const postTemplate = path.resolve(`./src/templates/post.js`)
 
    const postsQuery = await graphql(`
        {
            posts: allBlogPost {
                nodes {
                    id
                    slug
                }
            }
        }
    `);

  if (postsQuery.errors) {
    reporter.panicOnBuild(`Error while running GraphQL query.`);

    return;
  }

        postsQuery.data.posts.nodes.forEach( node => {
            createPage({
                path: node.slug,
                component: postTemplate,
                context: {
                    id: node.id,
                },
            })
        })

您似乎无法正确创建自己的架构。 确保您的验证是正确的:

if (node.internal.type === `Asciidoc`)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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