【问题标题】:Customizing Gatsby / Contentful Schema自定义 Gatsby / Contentful Schema
【发布时间】:2020-08-18 13:56:00
【问题描述】:

我正在尝试为 Contentful 源添加 createSchemaCustomization,该源可能没有我感兴趣的字段,因此 Gatsby 无法推断类型。

我已经创建了自定义,这样我的模板查询就不会抱怨,但如果有一个条目,它也不会被拾取。我确定我可能没有正确设置自定义。这就是我所拥有的(@infer 标志似乎并没有起到多大作用):

exports.createSchemaCustomization = ({ actions }) => {
  const { createTypes } = actions;
  const typeDefs = `
    type ContentfulUniversalProduct implements Node @infer {
      datesAndPricesSnippets: [DatesAndPricesSnippet]
    }
    type DatesAndPricesSnippet implements Node @infer {
      id: String
      title: String
      icon: String
      iconColor: String
      body: WithChildMarkdownRemark
    }
    type WithChildMarkdownRemark implements Node @infer {
      childMarkdownRemark: MarkdownRemark
    }
  `;
  createTypes(typeDefs);
};

我的查询。

如果 Contentful 中有 datesAndPricesSnippets,并且我没有上述 schemaCustomization,则此查询有效。

如果我确实进行了自定义,则此查询的输出为 null - 无论内容是否为 Contentful。

export const query = graphql`
  query($slug: String!, $id: String!) {
    contentfulUniversalProduct {
      datesAndPricesSnippets {
        id
        title
        body {
          childMarkdownRemark {
            html
          }
        }
        icon
        iconColor
      }
    }
  }
`

【问题讨论】:

    标签: graphql gatsby


    【解决方案1】:

    我遇到了类似的问题,以下帮助了我:

    1. 安装gatsby-plugin-schema-snapshot
    2. 生成schema.gql 文件,GATSBY_UPDATE_SCHEMA_SNAPSHOT=y yarn develop 为我完成了
    3. 观察schema.gql 文件,您对createTypes 所做的更改会显示出来

    在我的情况下,添加 @link 调用有助于返回节点而不是 null

    exports.createSchemaCustomization = ({ actions }) => {
      const { createTypes } = actions
      const typeDefs = `
        type ContentfulThing implements Node {
          fieldWithModules: [SomeBigUnion] @link(by: "id", from: "fieldWithModules___NODE")
        }
      `
      createTypes(typeDefs)
    }
    

    【讨论】:

    • 如果您将GATSBY_UPDATE_SCHEMA_SNAPSHOT=y(或任何您想要的)添加到.env.development.env.production,那么您不需要将它添加到#2 中的命令中。
    猜你喜欢
    • 2020-03-20
    • 2021-07-14
    • 2018-07-14
    • 2020-09-30
    • 2021-08-27
    • 2021-03-16
    • 2019-11-21
    • 2019-11-28
    • 2021-11-20
    相关资源
    最近更新 更多