繁体   English   中英

未处理的拒绝(错误):预期未定义为 GraphQL 架构

[英]Unhandled Rejection (Error): Expected undefined to be a GraphQL schema

我正在尝试构建从 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。

我认为您的查询在gatsby-node.js文件中的格式不正确。 应该如下所示:

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

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

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

导致错误的原因:

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

如何修复:

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

暂无
暂无

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

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