【问题标题】:Grapgql Playground Fauna - file not defined, but it is definedGraphql Playground Fauna - 文件未定义,但已定义
【发布时间】:2022-06-14 00:54:32
【问题描述】:

您好,我正在学习如何使用动物群作为电子商务应用程序的数据库。这是第一次设置一些架构,我找不到如何执行此操作的示例。

我的架构:

type Image {
      src: String!
      alt: String!
    }

type ArtworkEntry {
    name: String!
    category: String!
    price: Int!
    currency: String!
    image: Image
}

这是我在动物群 graphql 操场上的 graphql 突变

mutation CreateArtworkEntry {
   createArtworkEntry(data: {
      name: "DDD"
      category: "DDD"
      price: 101
      currency: "USD"
      image: {
        src: "https://www.pexels.com/photo/26938/"
        alt: "https://www.pexels.com/photo/26938/"
      }
    }
  ) {
     name
    category
    image
    price
    currency
   }
}

按播放时出现以下错误: “字段‘src’不是由类型‘ArtworkEntryImageRelation’定义的。

请引导我走正确的道路(我确实用新保存的更改替换了架构)

【问题讨论】:

    标签: graphql faunadb


    【解决方案1】:

    在您的架构中,ArtworkEntry 类型中的image 字段表示该值为Image 类型。但是,它并没有说明应该如何发生。

    由于ArtworkEntryImage 之间似乎存在一对一的关系,因此Image 类型应该包含@embedded 指令,如下所示:

    type Image @embedded {
      src: String!
      alt: String!
    }
    

    使用@embedded 可以让您将一种类型“嵌入”到另一种类型中,这样单个文档就会存在嵌套字段。

    随着这种变化,你的突变也需要改变。照原样,错误是:

    {
      "data": null,
      "errors": [
        {
          "message": "Field 'image' of type 'Image' must have a sub selection. (line 7, column 5):\n    image\n    ^",
          "locations": [
            {
              "line": 7,
              "column": 5
            }
          ]
        }
      ]
    }
    

    要解决此问题,请更新您的变更以指定在 Image 类型中返回哪些字段:

    mutation CreateArtworkEntry {
       createArtworkEntry(data: {
          name: "DDD"
          category: "DDD"
          price: 101
          currency: "USD"
          image: {
            src: "https://www.pexels.com/photo/26938/"
            alt: "https://www.pexels.com/photo/26938/"
          }
        }
      ) {
         name
        category
        image {
          src
          alt
        }  
        price
        currency
       }
    }
    

    然后结果是:

    {
      "data": {
        "createArtworkEntry": {
          "name": "DDD",
          "category": "DDD",
          "image": {
            "src": "https://www.pexels.com/photo/26938/",
            "alt": "https://www.pexels.com/photo/26938/"
          },
          "price": 101,
          "currency": "USD"
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-18
      • 2018-07-19
      • 2018-11-09
      • 2014-10-14
      • 2018-08-06
      • 2020-08-01
      • 2019-10-01
      • 1970-01-01
      相关资源
      最近更新 更多