1
votes

I am trying to use a query with a filter:

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

The mapping template, made by AppSync, looks like this:

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

The error I get is:

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

Any idea how I can fix that?

1

1 Answers

2
votes

$util.dynamodb.toBinary(String data) takes a String as input, but you are passing a Boolean and this is why it fails evaluation.

This is good feedback, I will check with the team if it's possible to make the utility more lenient and take a Boolean as well $util.dynamodb.toBinary(Boolean data)

Here is a possible workaround in the meantime:

#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",
}