我的盖茨比onCreateNode
有一些条件逻辑,结果是这样的:
exports.onCreateNode = async ({ node, actions }) => {
// Other stuff...
/* If all `embed` results are null here,
* the field will not be created on the graphql schema
*/
const embed = await getFormEmbedForNode(node)
actions.createNodeField({
node,
name: `embed`,
value: embed
})
}
}
const getFormEmbedForNode = async node => {
/* If this test is true for all nodes, the field will not be created
* returning "" avoids this problem
*/
if (past(node.startDate)) {
return null
} else {
const embedResult = await client.getArbitrary(
node.links.embedHref
)
return embedResult
}
}
尽管在新字段null
的环境下进行测试,但gatsby构建中的所有内容都中断了-即使没有生成使用该字段的页面,因为他们的页面查询未被识别为该字段的有效部分盖茨比模式。我的解决方法是传递一个空字符串,该字符串在后台使中继编译器确信该字段是String
,但是有一种方法可以显式地执行此操作,而不是依赖于哨兵值?
我看过gqlType创建文档here,似乎有办法做到这一点,但是我对fields
与gatsby节点的多种用法感到困惑以及随附的架构。
对于后代,构建过程中产生的错误看起来像
ERROR #85907 GRAPHQL
There was an error in your GraphQL query:
- Unknown field 'embed' on type 'MyNodeTypeFields'.
答案 0 :(得分:1)
如上面@kav所建议的那样,处理此问题的正确方法是实现here中所述的createSchemaCustomization
。这样可以确保无论gatsby的中继编译器是否认为它们为空,都将设置这些字段:
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type EventFields {
slug: String!
formEmbed: String
}
type Event implements Node {
fields: EventFields
}
`
createTypes(typeDefs)
}