我对 AppSync 还很陌生。
即使我在解析器中设置了条件,我也无法限制 PutItem 命令。
查询
mutation MyMutation {
createTodo(input: {description: "test", name: "test2"}) {
id
description
name
}
}
结果
{
"data": {
"createTodo": null
},
"errors": [
{
"path": [
"createTodo"
],
"data": null,
"errorType": null,
"errorInfo": null,
"locations": [
{
"line": 2,
"column": 3,
"sourceName": null
}
],
"message": "The conditional request failed (Service: DynamoDb, Status Code: 400, Request ID: XXX"
}
]
}
旋转变压器
{
"operation": "PutItem",
"key": {
"id": $util.dynamodb.toDynamoDBJson($util.autoId())
},
"attributeValues" : {
"name" : $util.dynamodb.toDynamoDBJson($ctx.args.input.name),
"description" : $util.dynamodb.toDynamoDBJson($ctx.args.input.description)
},
"condition": {
"expression": "description = :sizeValue",
"expressionValues": {
":sizeValue": {
"S":"test"
}
}
}
}
在上面的例子中,我在描述中设置了“test”。所以,我认为不应该有条件例外。但是,我收到错误消息“条件请求失败”。
我检查了 CloudWatch 日志。
"transformedTemplate": "{\n \"operation\": \"PutItem\",\n \"key\": {\n \"id\": {\"S\":\"xxx\"}\n },\n \"attributeValues\" : {\n \"name\" : {\"S\":\"test2\"},\n \"description\" : {\"S\":\"test\"}\n },\n \"condition\": {\n \"expression\": \"description = :sizeValue\",\n\t\"expressionValues\": {\n \":sizeValue\": {\n \"S\":\"test\"\n }\n }\n }\n}"
}
↓(格式化后)
{
"operation": "PutItem",
"key": {
"id": {"S":"xxx"}
},
"attributeValues" : {
"name" : {"S":"test2"},
"description" : {"S":"test"}
},
"condition": {
"expression": "description = :sizeValue",
"expressionValues": {
":sizeValue": {
"S":"test"
}
}
}
}
根据上面的日志,我认为该请求应该满足条件。
有人对如何解决这个问题有任何想法吗?谢谢你。
您已经编写了一个 PutItem 请求,其中指出:
仅当项目已存储在 DynamoDB 中且
"id": {"S":"xxx"}
的值等于 description
时,才保存项目 test
。
我相信您认为条件基于您在请求中为属性设置的内容,但条件用于评估已存储在 DynamoDB 中的项目状态。
总而言之,您会得到一个
ConditionCheckFailedException
,因为您的 DynamoDB 表中没有包含 id=test2
和 description=test
的项目。这是您的条件评估为真实所需的。