在我的API中,我有 layout
支持动态模式,所以我把这个添加到 sourceNode
作为字符串化的JSON。
for (const { path } of nodesList) {
/* ... */
const node = {
id: createNodeId(`cms-${cmsNode.id}`),
/* this prop */
layout: JSON.stringify(layout),
parent: null,
children: [],
internal: {
content: JSON.stringify(cmsNode),
type: 'CmsNode',
},
}
node.internal.contentDigest = createContentDigest(node)
createNode(node)
}
如何自定义这个字段,在查询层面上解析JSON,而不是。JSON.parse
在页面组件中?
我应该要添加自定义类型。
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type CmsNode implements Node @dontInfer {
id: ID!
parent: Node
children: [Node!]!
...
layout: JSON
}
`
createTypes(typeDefs)
}
并且在使用过程中不要将这个道具字符串化。createNode
.