【问题标题】:Gatsby and WPGraphQL - Build Error "Cannot query field "pages" on type "Query""Gatsby 和 WPGraphQL - 构建错误“无法在类型“查询”上查询字段“页面””
【发布时间】:2021-12-09 05:26:03
【问题描述】:

我对 WPGraphQL 完全陌生,并且在从我的新 Wordpress 端点构建 Gatsby 站点时遇到了一些麻烦。 当我在 Postman 中对我的端点执行以下查询时,我成功地取回了页面数据:

 query MyQuery {
     pages {
        edges {
            node {
                id
                slug
                status
            }
        }
     }
  }

但是,当我尝试构建 Gatsby 网站时,我得到以下信息:

错误 #85923 GRAPHQL

您的 GraphQL 查询中有错误:

无法在“查询”类型上查询字段“页面”。

gatsby-node.js中的相关代码为:

const _ = require('lodash')
const path = require('path')
const { createFilePath } = require('gatsby-source-filesystem')
const { paginate } = require('gatsby-awesome-pagination')

const getOnlyPublished = edges =>
  _.filter(edges, ({ node }) => node.status === 'publish')

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

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

盖茨比配置:

module.exports = {
  siteMetadata: {
    siteUrl: "https://www.yourdomain.tld",
    title: "graphql-test",
  },
  plugins: [
    {
      resolve: "gatsby-source-wordpress",
      options: {
        url: "https://*********/graphql",
      },
    },
    "gatsby-plugin-sass",
    "gatsby-plugin-react-helmet",
    "gatsby-plugin-sitemap",
  ],
};

这是一个全新的 Gatsby 测试站点,所有插件都是最新版本。

非常感谢任何帮助!

【问题讨论】:

    标签: javascript reactjs build graphql gatsby


    【解决方案1】:

    事实证明,Gatsby 对其 graphql 查询使用了稍微不同的格式。我应该在 gatsby-node.js 中拥有的是这样的:

      return graphql(`
        {
          allWpPage {
              nodes {
                id
                slug
                status
            }
          }
        }
      `)
    

    【讨论】:

      猜你喜欢
      • 2021-06-03
      • 2021-11-13
      • 2021-09-02
      • 1970-01-01
      • 2020-10-29
      • 2021-10-09
      • 2018-10-30
      • 2021-07-26
      • 2020-09-10
      相关资源
      最近更新 更多