[英]Querying DynamoDB with a list in AWS AppSync
I am trying to build an AWS AppSync query with a list using IN
:我正在尝试使用
IN
使用列表构建 AWS AppSync 查询:
{
"version" : "2017-02-28",
"operation" : "Query",
"index" : "my-index",
"query" : {
"expression": "id IN :ids",
"expressionValues" : { ":ids" : $util.dynamodb.toStringSet($ctx.args.ids) }
},
"limit": $util.defaultIfNull(${ctx.args.first}, 20),
"nextToken": $util.toJson($util.defaultIfNullOrBlank($ctx.args.after, null))
}
However, trying it out with parameters like:但是,尝试使用以下参数进行测试:
query ListItemsWithIdList {
listItemsWithIdList(first:20, ids: ["id1", "id2"]) {
items {
id
}
nextToken
}
}
It throws an error:它抛出一个错误:
Unable to parse the JSON document: 'Unexpected character ('S' (code 83)): was expecting double-quote to start field name
at [Source: (String)\"{
\"version\" : \"2017-02-28\",
\"operation\" : \"Query\",
\"index\" : \"my-index\",
\"query\" : {
\"expression\": \"id IN :ids\",
\"expressionValues\" : { \":ids\" : {SS=[id1, id2]} }
},
\"limit\": 20,
\"nextToken\": null
}\"; line: 7, column: 47]'"
It seems OK to use IN
for query comparison operator;将
IN
用于查询比较运算符似乎可以; however, how can I pass a String List
as a parameter and fetch the results whose IDs are among those parameters supplied?但是,如何将
String List
作为参数传递并获取 ID 位于所提供参数中的结果?
EDIT: Corrected variable name typo.编辑:更正了变量名拼写错误。
I don't think AWS AppSync support IN
just for now.我认为 AWS AppSync 暂时不支持
IN
。 I try to test your scenario and come up with a solution using contains()
function.我尝试测试您的场景并提出使用
contains()
函数的解决方案。
Here is the result after query:查询后的结果如下:
Another alternative solution is to use Scan
(not recommended)另一种替代解决方案是使用
Scan
(不推荐)
{
"version" : "2017-02-28",
"operation" : "Scan",
"filter" : {
"expression": "contains (:authors, author)",
"expressionValues" : {
":authors" : $util.dynamodb.toDynamoDBJson($ctx.args.authors)
}
}
}
Btw, AWS AppSync support BatchGetItem
operation.顺便说一句,AWS AppSync 支持
BatchGetItem
操作。 You can pass a list of keys in a single query and return the results from a table.您可以在单个查询中传递键列表并从表中返回结果。
Reference: https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html参考: https : //docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html
Here is an example and I tested it worked like charm:这是一个例子,我测试了它的魅力:
## REQUEST MAPPING
#set($authors = [])
#foreach( $author in $ctx.args.authors )
#set($map = {})
$util.qr($map.put("author", $util.dynamodb.toString($author)))
$util.qr($authors.add($map))
#end
{
"version" : "2018-05-29",
"operation" : "BatchGetItem",
"tables" : {
"tbDiary": {
"keys": $util.toJson($authors),
"consistentRead": true
}
}
}
## RESPONSE MAPPING
$util.toJson($ctx.result.data.tbDiary)
I think there could be 2 issues here:我认为这里可能有两个问题:
Your arguments in the listItemsWithIdList
query accepts an argument named userIds
. listItemsWithIdList
查询中的参数接受名为userIds
的参数。 You have $ctx.args.ids)
in your Resolver template.您的解析器模板中有
$ctx.args.ids)
。 You need use the same argument name in both places.您需要在两个地方使用相同的参数名称。
When you use $util.dynamodb.toStringSet
in your Mapping Template, like you see in the error, it gets transformed to { \\":ids\\" : {SS=[id1, id2]} }
.当您在映射模板中使用
$util.dynamodb.toStringSet
时,就像您在错误中看到的那样,它会转换为{ \\":ids\\" : {SS=[id1, id2]} }
。 However, you want the id values to be contained within quotes.但是,您希望将 id 值包含在引号中。 AWS AppSync provides another utility method for this purpose.
AWS AppSync 为此提供了另一种实用方法。 You can change your template to use
$util.dynamodb.toStringSetJson
, which then gets converted to { \\":ids\\" : {SS=[\\"id1\\", \\"id2\\"]} }
.您可以更改模板以使用
$util.dynamodb.toStringSetJson
,然后将其转换为{ \\":ids\\" : {SS=[\\"id1\\", \\"id2\\"]} }
。
Let me know if this resolved your issue.让我知道这是否解决了您的问题。 Here is the reference to the Resolver Mapping Template Utilities.
这是对解析器映射模板实用程序的参考。
Hopefully you have already resolved it.希望你已经解决了。 However, there are two issues in your setup:
但是,您的设置有两个问题:
This has to be a filter expression and since you don't have a key condition, this may even have to be a scan instead of a query这必须是一个过滤器表达式,并且由于您没有关键条件,这甚至可能必须是扫描而不是查询
Then there are syntax issues (which is what the error is really because of): In the EXPRESSION, add parentheses to the variable "expression": "id IN (:ids"),然后是语法问题(这是错误的真正原因):在 EXPRESSION 中,将括号添加到变量“表达式”:“id IN (:ids”),
And in EXPRESSION VALUES use toStringSetJson instead of toStringSet "expressionValues" : { ":ids" : $util.dynamodb.toStringSetJson($ctx.args.ids) }在表达式值中使用 toStringSetJson 而不是 toStringSet "expressionValues" : { ":ids" : $util.dynamodb.toStringSetJson($ctx.args.ids) }
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.