amazon-web-services - 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 对象?
解决方案
我能够从此处的 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
}
后者是我前进的解决方案。
推荐阅读
- javascript - Firebase 实时数据库 ServerValue 错误
- python-3.x - 如何在 Python 中将参数传递给 SQL 查询并将结果写入 csv 文件
- r - RShiny : mainPanel 行为随输入而变化
- firebase - 如何从 vue/firebase 应用程序中删除 eslint
- javascript - 使用 Sequelize 一天中每个小时的负载数据
- sql - 对于范围内的每个日期,获取日期范围内的记录
- html - 将叠加在图像上的整个画布块居中
- python - 创建一个函数,从其输入参数的数字中生成最小的数字
- java - CrudRepository 返回 null
- reactjs - 如何在 React 中正确实现 Redux