在 netlify cms 和 gatsby js 中使图像可选

use*_*765 5 javascript graphql gatsby netlify-cms gatsby-image

我有一个相当简单的 Gatsby & Netlify CMS 站点。我无法应付使图像可选。在 Netlify CMS 的情况下,它只是设置一个字段的问题required: false。我如何为 Gatsby 编写查询,以便我不会收到错误“GraphQL 错误字段“图像”必须没有选择,因为“字符串”类型没有子字段。当图像实际上是一个空字符串时,因为它在我的应用程序中不是强制性的?有没有办法解决?

GraphQL 查询图像:

image {
  childImageSharp {
    fluid(maxWidth: 2048) 
      ...GatsbyImageSharpFluid
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 2

Into your gatsby-node.js, you have to define a graphql type to handle this scenario.

exports.createSchemaCustomization = ({ actions }) => {
  const { createTypes } = actions;
  const typeDefs = `
    type MarkdownRemark implements Node {
      frontmatter: Frontmatter
    }
    
    type Frontmatter @infer {
      yourimage: File @fileByRelativePath,
    }
  `;
  createTypes(typeDefs);
};
Run Code Online (Sandbox Code Playgroud)