如何向Gatsby sourceNode添加JSON字段?

问题描述 投票:0回答:1

在我的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 在页面组件中?

javascript json graphql gatsby
1个回答
0
投票

我应该要添加自定义类型。

exports.createSchemaCustomization = ({ actions }) => {
  const { createTypes } = actions
  const typeDefs = `
    type CmsNode implements Node @dontInfer {
      id: ID!
      parent: Node
      children: [Node!]!
      ...
      layout: JSON
    }
  `
  createTypes(typeDefs)
}

并且在使用过程中不要将这个道具字符串化。createNode.

© www.soinside.com 2019 - 2024. All rights reserved.