【发布时间】:2018-07-08 12:46:09
【问题描述】:
createNode 的值为 null 时不会创建字段。我们有一个短暂的数据馈送,有时会返回某些字段为空的对象,尽管这种情况很少见。
我们想使用这个查询,但是因为有时name 为空,gatsby 会删除它并导致这个查询出错。
{
allMyType {
edges {
node {
id
name
}
}
}
}
有没有办法解决这个问题?
这段代码模拟了用例:
const crypto = require('crypto')
exports.createPages = ({ boundActionCreators, graphql }) => {
const { createNode } = boundActionCreators
// Simulate an ephemeral data source that sometimes doesn't have all properties set
const data = {
id: '1',
name: Math.random() < 0.5 ? null : 'Paul Serby'
}
createNode({
...data,
id: data.id,
children: [],
parent: null,
internal: {
type: 'MyType',
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(data))
.digest(`hex`)
}
})
}
【问题讨论】:
标签: javascript node.js graphql gatsby