使用 AWS AppSync 中的列表查询 DynamoDB [英] Querying DynamoDB with a list in AWS AppSync

查看:27
本文介绍了使用 AWS AppSync 中的列表查询 DynamoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 IN 使用列表构建 AWS AppSync 查询:

I am trying to build an AWS AppSync query with a list using IN:

{
    "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
  }
}

它会抛出一个错误:

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]'"

查询比较运算符使用IN似乎没问题;但是,如何将 String List 作为参数传递并获取其 ID 在提供的参数中的结果?

It seems OK to use IN for query comparison operator; however, how can I pass a String List as a parameter and fetch the results whose IDs are among those parameters supplied?

更正变量名拼写错误.

推荐答案

我认为 AWS AppSync 目前不支持 IN.我尝试测试您的场景并使用 contains() 函数提出解决方案.

I don't think AWS AppSync support IN just for now. I try to test your scenario and come up with a solution using contains() function.

查询后的结果如下:

另一种替代解决方案是使用 Scan(不推荐)

Another alternative solution is to use Scan (not recommended)

{
    "version" : "2017-02-28",
    "operation" : "Scan",
    "filter" : {
        "expression": "contains (:authors, author)",
        "expressionValues" : {
            ":authors" : $util.dynamodb.toDynamoDBJson($ctx.args.authors)
        }
    }
}

顺便说一句,AWS AppSync 支持 BatchGetItem 操作.您可以在单个查询中传递键列表并从表中返回结果.
参考:https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html
这是一个例子,我测试它像魅力一样工作:

Btw, AWS AppSync support BatchGetItem operation. 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
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)

这篇关于使用 AWS AppSync 中的列表查询 DynamoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆