【问题标题】:Optional queries in Gatsby / GraphQLGatsby / GraphQL 中的可选查询
【发布时间】:2019-11-22 02:47:16
【问题描述】:

我有一个内容丰富的 Gatsby 网站,初始页面构建 GraphQL 如下所示:

{
    allContentfulProduct {
        edges {
            node {
                slug
                contentfulid
            }
        }
    }
}

这在使用预览 API 时可以正常工作,但在使用生产 API 时它会失败,除非我至少发布了 1 个 Product 条目:

There was an error in your GraphQL query:

Cannot query field "allContentfulProduct" on type "Query". Did you mean ... [suggested entry names] ?

我很确定当我发布Product 时,事情会按预期工作,但是有什么方法可以使这个查询成为可选的。查询应该返回零个结果,因此不会创建 Product 页面(如果没有发布 Product 条目,则为预期结果)

【问题讨论】:

    标签: graphql gatsby


    【解决方案1】:

    尝试将其添加到您的 gatsby-node.js 文件中并进行尝试。

    exports.createSchemaCustomization = ({ actions }) => {
      const { createTypes } = actions
      const typeDefs = `
           type allContentfulProduct implements Node{
           slug: String,
           contentfulid: String
      }`
    

    【讨论】:

      猜你喜欢
      • 2020-03-23
      • 2019-04-02
      • 1970-01-01
      • 2021-05-28
      • 2020-08-17
      • 2020-02-05
      • 2018-06-16
      • 2020-11-07
      • 2021-07-16
      相关资源
      最近更新 更多