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



我有一个相当简单的Gatsby&Netlify CMS网站。我无法应付使图像可选。在Netlify CMS的情况下,只需设置一个字段required: false即可。如何为 Gatsby 编写查询,这样我就不会收到错误"GraphQL 错误字段"图像"不得有选择,因为类型"字符串"没有子字段",当图像实际上是空字符串时,因为它在我的应用程序中不是强制性的?有什么办法吗?

图像的 GraphQL 查询:

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

在你的 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);
};

最新更新