Gatsby 盖茨比不推断graphql字段,如果该字段上的所有节点恰好为空
我的gatsbyGatsby 盖茨比不推断graphql字段,如果该字段上的所有节点恰好为空,gatsby,Gatsby,我的gatsbyonCreateNode中有一些条件逻辑,其结果如下: 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 getFormEmbed
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 build崩溃了,即使没有生成使用该字段的页面,因为它们的页面查询没有被识别为gatsby模式的有效部分。我的解决方法是传递一个空字符串,使中继编译器在后台确信该字段是字符串
,但是有没有一种方法可以显式地执行此操作,而不是依赖于sentinel值
我看过gqlType创建文档,似乎有一种方法可以做到这一点,但我对与gatsby节点相关的字段
的多种用法感到困惑&伴随的模式
对于后代来说,在构建过程中产生的错误如下
ERROR #85907 GRAPHQL
There was an error in your GraphQL query:
- Unknown field 'embed' on type 'MyNodeTypeFields'.
正如上面@kav所建议的那样,正确的处理方法是实现
createSchemaCustomization
。这确保了无论盖茨比的中继编译器是否认为这些字段为空,都将设置这些字段:
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type EventFields {
slug: String!
formEmbed: String
}
type Event implements Node {
fields: EventFields
}
`
createTypes(typeDefs)
}
这有用吗@ksav谢谢-我来看看。盖茨比的文档既优秀又复杂,特别是当涉及到一些有点过载的术语时(
node
和模式创建与gqlType创建现在就在我的脑海中出现)(&当我把它们放在一起时,我会在这里更新一个答案)。