【问题标题】:Make an image optional in netlify cms and gatsby js在 netlify cms 和 gatsby js 中使图像可选
【发布时间】:2020-05-06 14:59:00
【问题描述】:

我有一个相当简单的 Gatsby & Netlify CMS 网站。我无法应付使图像可选的问题。对于 Netlify CMS,只需设置一个字段 required: false。如何为 Gatsby 编写查询,以免出现错误“GraphQL 错误字段“图像”不能选择,因为“字符串”类型没有子字段。当图像实际上是一个空字符串,因为它在我的应用程序中不是强制性的?有没有办法解决这个问题?

GraphQL 图像查询:

image {
  childImageSharp {
    fluid(maxWidth: 2048) 
      ...GatsbyImageSharpFluid
    }
  }
}

【问题讨论】:

    标签: javascript graphql gatsby netlify-cms gatsby-image


    【解决方案1】:

    在您的 gatsby-node.js 中,您必须定义一个 graphql 类型来处理这种情况。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-26
      • 2019-04-18
      • 2020-04-01
      • 2019-07-14
      • 2021-08-29
      • 2020-03-13
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多