AWS Amplify:变量输入包含未为输入对象类型定义的字段名称
AWS Amplify: The variables input contains a field name that is not defined for input object type
我不明白这里发生了什么。
这是我的架构:
type MonthResume @model @auth(rules: [{allow: owner, identityClaim: "sub"}]){
id: ID!
incomes: Float!
spendingByCategory: [Category]
}
type Category @model @auth(rules: [{allow: owner, identityClaim: "sub"}]){
id: ID!
name: String!
amount: Float!
}
这是 Amplify 给我的自动生成的更新突变:
export const updateMonthResume = /* GraphQL */ `
mutation UpdateMonthResume(
$input: UpdateMonthResumeInput!
$condition: ModelMonthResumeConditionInput
) {
updateMonthResume(input: $input, condition: $condition) {
id
incomes
spendingByCategory {
id
name
amount
createdAt
updatedAt
owner
}
createdAt
updatedAt
owner
}
}
`;
这是我的输入:
{
"input": {
"id": "d7f-ee2971fd3ae5",
"incomes": 220,
"spendingByCategory": null,
"createdAt": "2020-08-15T17:06:22.192Z",
"updatedAt": "2020-08-15T17:06:22.192Z",
"owner": "subId"
}
}
我只想更新收入金额,因此我这样调用 api:
const input = {
incomes: 0,
}
await API.graphql(graphqlOperation(updateMonthResume, input));
然后,我得到了错误。
我不明白,我不想更新收入,我需要改变我的输入吗?但是我为 objetc spendingByCategory 发送了一个空值(Amplify 自动执行)。
input CreateMonthResumeInput {
id: ID
incomes: Float!
}
好的,问题我解决了
与 SpendingCategory 无关(这是我收到的日志,我正在使用该参考搜索问题)。
我的输入属性不正确,因此它会发送该错误。
确保您输入的对象是完全正确的,然后再花时间处理其他日志。
总结评论的discussion/accumulate知识:
input object can only have properties defined in UpdateMonthResumeInput
type,
- no more
- can be less (if nullable/not required)
- ... but all required
usually there is a difference between:
- input types (used for mutation as input/params)
- ... and return types (result types for queries and mutations)
...和相关的questions/answers:
Why in this input doesn't show the property spendingByCategory
?
因为它不是 the MonthResume body
(自己的类型 fields/properties) - 它来自关系...就像创建用户然后添加用户 firend - 不可能立即与朋友一起创建用户 - 没有嵌套突变支持。
if I want to add an array of objects, without relationship, what should specify? I set spendingByCategory: [Category!]
just because I did't find the type Object
每个 [query/mutation] 深度级别都需要定义一个单独的类型(以及类型之间的关系)in graphql.您可以使用 'customJSON type'(任何可序列化或未知类型的内容)而无需为复杂 fields/properties.
定义类型
我 运行 使用 Lambda 函数解决了这个问题,发现为了传递正确的用户属性(所有者),我必须在 graphql.schema 中定义输入对象以包含所有者 - 所有者从客户端传递到创建数据库记录的 lambda 函数。不确定如果留给 graphql 所有者会是谁,因为 api 调用来自 lambda 函数。
我不明白这里发生了什么。
这是我的架构:
type MonthResume @model @auth(rules: [{allow: owner, identityClaim: "sub"}]){
id: ID!
incomes: Float!
spendingByCategory: [Category]
}
type Category @model @auth(rules: [{allow: owner, identityClaim: "sub"}]){
id: ID!
name: String!
amount: Float!
}
这是 Amplify 给我的自动生成的更新突变:
export const updateMonthResume = /* GraphQL */ `
mutation UpdateMonthResume(
$input: UpdateMonthResumeInput!
$condition: ModelMonthResumeConditionInput
) {
updateMonthResume(input: $input, condition: $condition) {
id
incomes
spendingByCategory {
id
name
amount
createdAt
updatedAt
owner
}
createdAt
updatedAt
owner
}
}
`;
这是我的输入:
{
"input": {
"id": "d7f-ee2971fd3ae5",
"incomes": 220,
"spendingByCategory": null,
"createdAt": "2020-08-15T17:06:22.192Z",
"updatedAt": "2020-08-15T17:06:22.192Z",
"owner": "subId"
}
}
我只想更新收入金额,因此我这样调用 api:
const input = {
incomes: 0,
}
await API.graphql(graphqlOperation(updateMonthResume, input));
然后,我得到了错误。
我不明白,我不想更新收入,我需要改变我的输入吗?但是我为 objetc spendingByCategory 发送了一个空值(Amplify 自动执行)。
input CreateMonthResumeInput {
id: ID
incomes: Float!
}
好的,问题我解决了
与 SpendingCategory 无关(这是我收到的日志,我正在使用该参考搜索问题)。
我的输入属性不正确,因此它会发送该错误。
确保您输入的对象是完全正确的,然后再花时间处理其他日志。
总结评论的discussion/accumulate知识:
input object can only have properties defined in
UpdateMonthResumeInput
type,
- no more
- can be less (if nullable/not required)
- ... but all required
usually there is a difference between:
- input types (used for mutation as input/params)
- ... and return types (result types for queries and mutations)
...和相关的questions/answers:
Why in this input doesn't show the property
spendingByCategory
?
因为它不是 the MonthResume body
(自己的类型 fields/properties) - 它来自关系...就像创建用户然后添加用户 firend - 不可能立即与朋友一起创建用户 - 没有嵌套突变支持。
if I want to add an array of objects, without relationship, what should specify? I set
spendingByCategory: [Category!]
just because I did't find the type Object
每个 [query/mutation] 深度级别都需要定义一个单独的类型(以及类型之间的关系)in graphql.您可以使用 'customJSON type'(任何可序列化或未知类型的内容)而无需为复杂 fields/properties.
定义类型我 运行 使用 Lambda 函数解决了这个问题,发现为了传递正确的用户属性(所有者),我必须在 graphql.schema 中定义输入对象以包含所有者 - 所有者从客户端传递到创建数据库记录的 lambda 函数。不确定如果留给 graphql 所有者会是谁,因为 api 调用来自 lambda 函数。