无法解析 JSON 文档:无法识别的标记“$util”:需要('true'、'false' 或 'null')

Unable to parse the JSON document: Unrecognized token '$util': was expecting ('true', 'false' or 'null')

我正在尝试使用带过滤器的查询:

query queryPitchesByApprovedIndex($approved: Boolean = true) {
  queryPitchesByApprovedIndex(approved: $approved) {
    items {
      id
    }
  }
}

AppSync 制作的映射模板如下所示:

{
  "version": "2017-02-28",
  "operation": "Query",
  "query": {
    "expression": "#approved = :approved",
    "expressionNames": {
      "#approved": "approved",
    },
    "expressionValues": {
      ":approved": {"B": $util.dynamodb.toBinary($ctx.args.approved)},
    },
  },
  "index": "approved-index",
  "limit": $util.defaultIfNull($ctx.args.first, 20),
  "nextToken": $util.toJson($util.defaultIfNullOrEmpty($ctx.args.after, null)),
  "scanIndexForward": true,
  "select": "ALL_ATTRIBUTES",
}

我得到的错误是:

Unable to parse the JSON document: 'Unrecognized token '$util': was expecting ('true', 'false' or 'null')\n at [Source: (String)\"{\n \"version\": \"2017-02-28\",\n \"operation\": \"Query\",\n \"query\": {\n \"expression\": \"#approved = :approved\",\n \"expressionNames\": {\n \"#approved\": \"approved\",\n },\n \"expressionValues\": {\n \":approved\": {\"B\": $util.dynamodb.toBinary($ctx.args.approved)},\n },\n },\n \"index\": \"approved-index\",\n \"limit\": 20,\n \"nextToken\": null,\n \"scanIndexForward\": true,\n \"select\": \"ALL_ATTRIBUTES\",\n}\"; line: 10, column: 31]'

知道如何解决这个问题吗?

$util.dynamodb.toBinary(String data)String 作为输入,但您传递的是 Boolean,这就是评估失败的原因。

这是很好的反馈,我会与团队核实是否可以使该实用程序更宽松并采用布尔值 $util.dynamodb.toBinary(Boolean data)

在此期间,这里有一个可能的解决方法:

#if($ctx.args.approved) 
 #set($approved = $util.dynamodb.toBinaryJson("true"))
#else
 #set($approved = $util.dynamodb.toBinaryJson("false"))
#end

{
  "version": "2017-02-28",
  "operation": "Query",
  "query": {
    "expression": "#approved = :approved",
    "expressionNames": {
      "#approved": "approved",
    },
    "expressionValues": {
      ":approved":  $approved
    },
  },
  "index": "approved-index",
  "limit": $util.defaultIfNull($ctx.args.first, 20),
  "nextToken": $util.toJson($util.defaultIfNullOrEmpty($ctx.args.after, null)),
  "scanIndexForward": true,
  "select": "ALL_ATTRIBUTES",
}