AWS AppSync GraphQL:在突变中更新对象时出现 DynamoDB ConditionalCheckFailedException
AWS AppSync GraphQL : DynamoDB ConditionalCheckFailedException when updating object in mutation
我正在使用 graphql 为使用 amplify 和 appsync 的本机应用程序建模用户对象。我需要在创建用户后更新名称字段,但我收到 DynamoDB:ConditionalCheckFailedException。创建用户和查询用户 table 工作正常,我只是无法更新现有对象的字段。
我正在我的项目目录 (amplify/backend/api/schema.graphql) 中定义用户类型,并让 amplify CLI 根据我的模式类型生成 GraphQL 语句(查询、变更和订阅)。
我正在使用的模式:
type User @model @auth(rules: [{allow: owner}]) {
username: String!
name: String
}
type Mutation {
createUser(input: CreateUserInput!, condition: ModelUserConditionInput): User
updateUser(input: UpdateUserInput!, condition: ModelUserConditionInput): User
deleteUser(input: DeleteUserInput!, condition: ModelUserConditionInput): User
}
input UpdateUserInput {
username: String
name: String
}
我 运行 使用登录用户的凭据创建用户的突变。从 AppSync 控制台,我以具有相同用户名 (745ab477-5702-4f8d-b938-a987a3d9d192) 和 运行 此突变的 Cognito 用户身份登录:
mutation updateUser{
updateUser(input: {
name: "James"
}
condition:{
username: {eq: "745ab477-5702-4f8d-b938-a987a3d9d192"}
}){
username
name
}
}
产生:
{
"data": {
"updateUser": null
},
"errors": [
{
"path": [
"updateUser"
],
"data": null,
"errorType": "DynamoDB:ConditionalCheckFailedException",
"errorInfo": null,
"locations": [
{
"line": 40,
"column": 5,
"sourceName": null
}
],
"message": "The conditional request failed (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ConditionalCheckFailedException; Request ID: HND4VSCK8HLM5NC08VDEV51CORVV4KQNSO5AEMVJF66Q9ASUAAJG)"
}
]
}
这是添加当前用户后的 DynamoDB 用户table:
您应该在架构中指定主键
type User @model @auth(rules: [{allow: owner}]) {
id:ID!,
username: String!
name: String
}
并像下面这样更改您的请求
mutation updateSampleUser{
updateSampleUser(
input:{name:"James",id:"9f4...."}
)
{
name
}
}
相当于下面的SQL语句
update tbl1 set name = "James" where id = "9f4...."
@auth(rules: [{allow: owner}])
只有所有者可以进行更新。
我正在使用 graphql 为使用 amplify 和 appsync 的本机应用程序建模用户对象。我需要在创建用户后更新名称字段,但我收到 DynamoDB:ConditionalCheckFailedException。创建用户和查询用户 table 工作正常,我只是无法更新现有对象的字段。
我正在我的项目目录 (amplify/backend/api/schema.graphql) 中定义用户类型,并让 amplify CLI 根据我的模式类型生成 GraphQL 语句(查询、变更和订阅)。
我正在使用的模式:
type User @model @auth(rules: [{allow: owner}]) {
username: String!
name: String
}
type Mutation {
createUser(input: CreateUserInput!, condition: ModelUserConditionInput): User
updateUser(input: UpdateUserInput!, condition: ModelUserConditionInput): User
deleteUser(input: DeleteUserInput!, condition: ModelUserConditionInput): User
}
input UpdateUserInput {
username: String
name: String
}
我 运行 使用登录用户的凭据创建用户的突变。从 AppSync 控制台,我以具有相同用户名 (745ab477-5702-4f8d-b938-a987a3d9d192) 和 运行 此突变的 Cognito 用户身份登录:
mutation updateUser{
updateUser(input: {
name: "James"
}
condition:{
username: {eq: "745ab477-5702-4f8d-b938-a987a3d9d192"}
}){
username
name
}
}
产生:
{
"data": {
"updateUser": null
},
"errors": [
{
"path": [
"updateUser"
],
"data": null,
"errorType": "DynamoDB:ConditionalCheckFailedException",
"errorInfo": null,
"locations": [
{
"line": 40,
"column": 5,
"sourceName": null
}
],
"message": "The conditional request failed (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ConditionalCheckFailedException; Request ID: HND4VSCK8HLM5NC08VDEV51CORVV4KQNSO5AEMVJF66Q9ASUAAJG)"
}
]
}
这是添加当前用户后的 DynamoDB 用户table:
您应该在架构中指定主键
type User @model @auth(rules: [{allow: owner}]) {
id:ID!,
username: String!
name: String
}
并像下面这样更改您的请求
mutation updateSampleUser{
updateSampleUser(
input:{name:"James",id:"9f4...."}
)
{
name
}
}
相当于下面的SQL语句
update tbl1 set name = "James" where id = "9f4...."
@auth(rules: [{allow: owner}])
只有所有者可以进行更新。