【问题标题】:AWS Amplify not generating proper graphql input depthAWS Amplify 未生成正确的 graphql 输入深度
【发布时间】:2019-10-07 05:50:07
【问题描述】:

我是 graphql 和 AWS Amplify 的新手,所以请原谅任何无知 :)

我有一个这样的 graphql 架构:

type Location @model @auth(rules: [{allow: owner}]){
  street: String
  city: String
  state: String
  zip: String
}

type Trip @model @auth(rules: [{allow: owner}]){
  id: String!
  ...
  location: Location
}

我正在尝试使用这样的突变请求同时创建位置和行程:

mutation {
  createTrip(input: {
      id: "someIdentifier",
      location: {
        street: "somewhere"
      }
  }) {
      id
      location {
        street
      }
  }
}

但我收到这样的错误:

{
  "data": null,
  "errors": [
    {
      "path": null,
      "locations": [
        {
          "line": 2,
          "column": 21,
          "sourceName": null
        }
      ],
      "message": "Validation error of type WrongType: argument 'input' with value '...' contains a field not in 'CreateTripInput': 'location' @ 'createTrip'"
    }
  ]
}

查看生成的schema.graphql文件,发现输入模型上确实没有location对象:

input CreateTripInput {
  id: String!
  ...
}

如何让 amplify 生成正确的输入架构,以便我可以同时创建 Trip 和 location 对象?

【问题讨论】:

    标签: amazon-web-services graphql aws-amplify amplifyjs


    【解决方案1】:

    我从 aws-amplify 团队 here 得到了答复。总结一下:

    Trip 和 Location 都有 model 指令。没有 @connection 指令将 Trip 与 Location 连接起来。 “解决”这个问题的两个选项是:

    如果您希望模型位于 2 个单独的表中并希望能够根据位置查询 Trip,请更新连接模型的架构。但是,使用 2 个单独的表,您将无法在单个突变中同时创建 Trip 和 Location。例如:

    type Location @model @auth(rules: [{allow: owner}]){
      street: String
      city: String
      state: String
      zip: String
      trips: Trip @connection(name:"TripLocation")
    }
    
    type Trip @model @auth(rules: [{allow: owner}]){
      id: String!
      location: Location @connection(name:"TripLocation")
    }
    

    第二个选项,如果位置数据非常特定于旅行并且您不想创建单独的表,则从您的位置类型中删除 @model 指令。这样做可以让您创建 Location 作为相同突变的一部分。

    type Location {
      street: String
      city: String
      state: String
      zip: String
    
    }
    
    type Trip @model @auth(rules: [{allow: owner}]){
      id: String!
      location: Location
    }
    

    后者是我前进的解决方案。

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 2022-11-03
      • 2020-12-21
      • 2021-01-30
      • 2020-08-20
      • 2019-08-12
      • 2020-12-16
      • 2020-08-14
      • 2020-07-01
      相关资源
      最近更新 更多