简体   繁体   English

使用 AWS AppSync 中的列表查询 DynamoDB

[英]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.

相关问题 使用 AWS AppSync 将项目附加到 DynamoDB 列表 - Append item to list using AWS AppSync to DynamoDB AWS AppSync加密DynamoDB数据 - AWS AppSync encrypting DynamoDB Data 具有业务逻辑的AWS Appsync + DynamoDB - AWS Appsync + DynamoDB with business logic 带有dynamodb解析器的AWS AppSync条件更新 - AWS AppSync conditional update with dynamodb resolver AWS Appsync 和 DynamoDB - 使用 IN 运算符进行查询 - AWS Appsync and DynamoDB - Query using IN operator AWS Amplify Datastore AppSync 未将 IndexDB 与 DynamoDB 同步 - AWS Amplify Datastore AppSync not synchronize IndexDB with DynamoDB AWS Amplify - AppSync和多个DynamoDB表 - AWS Amplify - AppSync & Multiple DynamoDB Tables Aws Appsync解析器:如何创建解析器以更新列表映射中的项目(DynaMoDB) - Aws Appsync Resolver : How to create a resolver to update item in list map (DynaMoDB) AWS Appsync Graphql 查询以获取项目列表返回空数组,即使 dynamodb 表中有项目 - AWS Appsync Graphql query to get list of items returns empty array even though the dynamodb table has items in it AWS AppSync GraphQL - 如何使用 PK/SK 查询而不是扫描整个 dynamoDB 表以获取 graphql 列表 API - AWS AppSync GraphQL - How to use PK/SK query instead of scanning whole dynamoDB table for graphql list APIs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM