简体   繁体   English

Grapgql Playground Fauna - 文件未定义,但已定义

[英]Grapgql Playground Fauna - file not defined, but it is defined

Hi so I am learning how to use fauna as database for an e-commerce app.嗨,我正在学习如何使用动物群作为电子商务应用程序的数据库。 This is the first time setting up some schemas and I can't find an example of how to do this.这是第一次设置一些模式,我找不到如何执行此操作的示例。

My schema:我的架构:

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

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

This is my graphql mutation on faunas graphql playground这是我在动物群 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
   }
}

When pressing play I get the following error: "Field 'src' is not defined by type 'ArtworkEntryImageRelation'.按播放时出现以下错误:“字段'src'不是由类型'ArtworkEntryImageRelation'定义的。

Please guide me on the right path(I did replace the schema with the new saved changes)请引导我走正确的道路(我确实用新保存的更改替换了架构)

In your schema, the image field within the ArtworkEntry type says that the value is an Image type.在您的架构中, ArtworkEntry类型中的image字段表示该值是Image类型。 However, it doesn't say how that should happen.但是,它没有说明应该如何发生。

Since there appears to be a one-to-one relationship between an ArtworkEntry and an Image , the Image type should have the @embedded directive included, like so:由于ArtworkEntryImage之间似乎存在一对一的关系,因此Image类型应该包含@embedded指令,如下所示:

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

Use of @embedded allows you to "embed" one type within another, so that a single document exists with nested fields.使用@embedded允许您将一种类型“嵌入”到另一种类型中,这样单个文档就会存在嵌套字段。

With that change, your mutation also needs to change.随着这种变化,你的突变也需要改变。 As is, the error is:照原样,错误是:

{
  "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
        }
      ]
    }
  ]
}

To fix that, update your mutation to specify which fields to return within the Image type:要解决此问题,请更新您的突变以指定要在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
   }
}

And then the result is:然后结果是:

{
  "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"
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM