【问题标题】:Unhandled Rejection (Error): Expected undefined to be a GraphQL schema未处理的拒绝(错误):预期未定义为 GraphQL 模式
【发布时间】:2021-12-30 00:27:37
【问题描述】:

我正在尝试构建从 WordPress 导入的帖子页面,我运行“gatsby develop”并前往 URL。 首页一闪而过,然后我得到这个错误:

Unhandled Rejection (Error): Expected undefined to be a GraphQL schema.
invariant
C:/Users/Phil/Repositories/Zym/node_modules/graphql/jsutils/invariant.mjs:12
assertSchema
C:/Users/Phil/Repositories/Zym/node_modules/graphql/type/schema.mjs:25
validateSchema
C:/Users/Phil/Repositories/Zym/node_modules/graphql/type/validate.mjs:31
graphqlImpl
C:/Users/Phil/Repositories/Zym/node_modules/graphql/graphql.mjs:44
(anonymous function)
C:/Users/Phil/Repositories/Zym/node_modules/graphql/graphql.mjs:20
graphql
C:/Users/Phil/Repositories/Zym/node_modules/graphql/graphql.mjs:18

在我的“PostTemplate.js”中突出显示的查询:


export const query = graphql`
  query($id: String!) {
     wordpressPost(id: { eq: $id }) {
      date
      title
      slug
      content
      categories {
        name
      }
    }    
  }
`;

我通过 GraphiQL 界面运行相同的查询,它会向我发送数据?

真的不确定我在这里做错了什么,请参阅 gatsby-node.js 中的以下代码

exports.createPages = ({ actions, graphql }) => {
  const { createPage } = actions


      return graphql(`
        {
          allWordpressPost {
            edges {
              node {
                id
                slug
                status
              }
            }
          }
        }
      `)
    .then(result => {
      if (result.errors) {
        result.errors.forEach(e => console.error(e.toString()))
        return Promise.reject(result.errors)
      }

      const postTemplate = path.resolve(`./src/templates/PostTemplate.js`)


      const posts = result.data.allWordpressPost.edges

      _.each(posts, ({ node: post }) => {
        createPage({
          path: `/${post.slug}/`,
          component: postTemplate,
          context: {
            id: post.id,
            slug: post.slug
          },
        })
      })
   })
})

我已经尝试更新 gatsby-cli -g,并卸载了 node_modules。

【问题讨论】:

    标签: javascript graphql gatsby


    【解决方案1】:

    我认为您的gatsby-node.js 文件中的查询格式不正确。应该是这样的:

    return graphql(`
        query {
            allWordpressPost {
                edges {
                    node {
                        id
                        slug
                        status
                    }
                }
            }
        }
    `);
    

    试一试,让我知道这是否适合你。

    【讨论】:

      【解决方案2】:

      我遇到了同样的错误,并且能够通过确保我直接从 gatsby 导入 graphql 来解决它:

      导致错误的原因:

      // template file
      import { graphql } from 'graphql'
      

      如何解决:

      // template file
      import { graphql } from 'gatsby'
      

      【讨论】:

        猜你喜欢
        • 2021-12-15
        • 2021-12-23
        • 2020-06-01
        • 2019-07-09
        • 2019-04-11
        • 2020-09-24
        • 2020-06-19
        • 2022-01-03
        • 2023-04-06
        相关资源
        最近更新 更多