我试图为一个Contentful数据源添加 createSchemaCustomization
,但这个数据源可能没有我感兴趣的字段,所以Gatsby无法推断类型。
我已经创建了自定义设置,以便我的模板查询不会出错,但如果有条目,它也没有被选中。我确定我可能没有正确设置自定义配置。以下是我拥有的内容(@infer
标志似乎也没有什么作用):
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type ContentfulUniversalProduct implements Node @infer {
datesAndPricesSnippets: [DatesAndPricesSnippet]
}
type DatesAndPricesSnippet implements Node @infer {
id: String
title: String
icon: String
iconColor: String
body: WithChildMarkdownRemark
}
type WithChildMarkdownRemark implements Node @infer {
childMarkdownRemark: MarkdownRemark
}
`;
createTypes(typeDefs);
};
我的查询。
如果Contentful中有datesAndPricesSnippets
,且我没有进行上述的schemaCustomization,则此查询有效。
如果我进行了自定义操作,那么无论内容是否在Contentful中,此查询的输出结果都是null
。
export const query = graphql`
query($slug: String!, $id: String!) {
contentfulUniversalProduct {
datesAndPricesSnippets {
id
title
body {
childMarkdownRemark {
html
}
}
icon
iconColor
}
}
}
`
GATSBY_UPDATE_SCHEMA_SNAPSHOT=y
(或任何您想要的内容)添加到.env.development
和.env.production
中,则无需将其添加到#2命令中。 - ProGrammar