对单个项的细粒度访问-GetItem,DynamoDB Resolver AppSync

时间:2019-05-17 12:34:01

标签: amazon-web-services amazon-dynamodb aws-appsync

如何在AppSync中提供对单个项目的细粒度访问。我有以下用于GetItem操作的解析器。

{   
    "version": "2017-02-28",
    "operation": "GetItem",
    "key": {
       "identityId": $util.dynamodb.toDynamoDBJson($ctx.args.identityId),
       "id": $util.dynamodb.toDynamoDBJson($ctx.args.id),   
    },   
    "condition": {
       "expression": "attribute_exists(#author) AND #author = :author",
       "expressionNames": {
          "#identityId": "identityId",
          "#id": "id",
          "#author": "author"
       },
       "expressionValues": {
          ":author" : { "S" : "${ctx.identity.cognitoIdentityId}" }
       }   
    }
}

但是,当我运行查询时,我得到了:

GraphQL error: Unsupported element '$[condition]'.

可以,因为根据文档,此操作https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-dynamodb.html#aws-appsync-resolver-mapping-template-reference-dynamodb-getitem没有条件键

我的问题 如果我不能设置条件,如何过滤/限制对属于特定作者的项目的访问(精细访问)?

1 个答案:

答案 0 :(得分:2)

您可以在如下的响应映射模板中过滤结果。据我了解,您正在从cognitoIdentityId获取author字段,并且您的商品具有不同的主键,因此为什么在查询时不能使用author。

#if($context.result["author"] == $ctx.identity.cognitoIdentityId)
    $utils.toJson($context.result);
#else
    $utils.unauthorized()
#end