简体   繁体   中英

Optional queries in Gatsby / GraphQL

I have a Gatsby site with a contentful source, the initial page build GraphQL looks like this:

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

this works fine when using the preview API, but when using the production one it fails unless I have at least 1 Product entry published:

There was an error in your GraphQL query:

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

I'm pretty sure that when I publish a Product things will work as expected, but is there any way to make this query optional. The query should return zero results, and thus no Product pages will be created (expected outcome if no Product entries are published)

Try to add into your gatsby-node.js file and play around that.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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