首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何结合gatsby片段创建gatsby模式

如何结合gatsby片段创建gatsby模式
EN

Stack Overflow用户
提问于 2020-09-25 14:16:05
回答 1查看 1.1K关注 0票数 2

我试图为graphql查询定义正确的架构,以便它可以返回null (使内容成为可选的)。据我所知,我需要在gatsby-node.js文件中预定义模式,这样gatsby就不必通过数据推断类型。但是,我不确定模式定义需要是什么,因为我们正在使用gatsby片段来查询数据。这是(部分)查询:

代码语言:javascript
复制
... on ContentfulWorkPage {
  comingSoon {
   id
   title
   image {
     fluid(quality: 92, maxWidth: 1280) {
       ...GatsbyContentfulFluid_withWebp_noBase64
     }
   }
   comingSoonText
  }
}

问题是我不知道如何定义GatsbyContentfulFluid_withWebp_noBase64

我想做的是:

代码语言:javascript
复制
const types = `
    type ContentfulWorkPage implements Node {
      comingSoon: ComingSoon
    }

    type ComingSoon {
      id: String
      title: String
      comingSoonText: String
      image: Image
    }

    type Image {
      fluid: Fluid
    }

   type Fluid {
    quality: Int
    maxWidth: Int
    title: String
   }
  `
  createTypes(types)

尽管我认为我走在正确的轨道上,但我仍然会犯以下错误:

代码语言:javascript
复制
There was an error in your GraphQL query:

Unknown argument "quality" on field "fluid" of type "Image".

GraphQL request:70:21
69 |             image {
70 |               fluid(quality: 92, maxWidth: 1280) {
   |                     ^
71 |                 ...GatsbyContentfulFluid_withWebp_noBase64

File: src/templates/mainPages/work.js:105:21


 ERROR #85901  GRAPHQL

There was an error in your GraphQL query:

Unknown argument "maxWidth" on field "fluid" of type "Image".

GraphQL request:70:34
69 |             image {
70 |               fluid(quality: 92, maxWidth: 1280) {
   |                                  ^
71 |                 ...GatsbyContentfulFluid_withWebp_noBase64

File: src/templates/mainPages/work.js:105:34


 ERROR #85901  GRAPHQL

There was an error in your GraphQL query:

Fragment "GatsbyContentfulFluid_withWebp_noBase64" cannot be spread here as objects of type "Fluid" can never be of type "ContentfulFluid".

GraphQL request:71:17
70 |               fluid(quality: 92, maxWidth: 1280) {
71 |                 ...GatsbyContentfulFluid_withWebp_noBase64
   |                 ^
72 |               }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-28 09:50:31

因此,我在gatsby文档(https://www.gatsbyjs.com/docs/actions/#printTypeDefinitions)中进行了深入研究之后,成功地解决了这个问题。我发现,在gatsby-node.js文件中,您可以使用printTypeDefinitions函数打印数据类型,然后可以使用该函数预先定义可选属性:

代码语言:javascript
复制
exports.createSchemaCustomization= ({ actions }) => {
  actions.printTypeDefinitions({path: './typeDefs.txt'})
}

这将在根文件夹中创建一个typeDefs.txt文件,其中描述所有推断类型。因此,如果您正在寻找像我一样更复杂的类型防御,以便可以使它们在内容上是可选的,那么您可以:

将一些数据添加到您的可选字段中(或您正在使用的任何后端/服务器) createSchemaCustomization

  • 使用该函数从数据

  • 中获取类型定义,从typeDefs文件中复制所需的任何部分,而

  • 则在

  • 中的createTypes函数中使用它

示例(comingSoon定义从typeDevs文件中复制):

代码语言:javascript
复制
exports.createSchemaCustomization = ({ actions }) => {
  const contentfulSomePage = `
      type ContentfulSomePage implements Node {
        comingSoon: [ContentfulCaseComingSoon] @link(by: "id", from: "comingSoon___NODE")
      }

      type ContentfulCaseComingSoon implements Node @derivedTypes @dontInfer {
        title: String
        comingSoonText: String
        image: ContentfulAsset @link(by: "id", from: "image___NODE")
      }
  `
  actions.createTypes(contentfulWorkPage)
}

额外提示:如果您只需要指定一个属性为可选属性,则可以推断其馀属性。删除@dontInfer注释,只复制可空属性,而不是从文件中复制整个类型。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64065878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档