首页 > 解决方案 > AWS Amplify 未生成正确的 graphql 输入深度

问题描述

我对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-servicesgraphqlaws-amplifyamplifyjs

解决方案


我能够从此处的 aws-amplify 团队获得答案。总结一下:

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

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

type Location {
  street: String
  city: String
  state: String
  zip: String

}

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

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


推荐阅读