aws-appsyncvtlapache-velocityaws-appsync-resolver

AppSync VTL resolver - how to resolve parse error


I'm getting this error on my VTL response mapping and I'm not sure what it means

graphql: Encountered "}" at velocity[line 9, column 3]
                                Was expecting one of:
                                    "[" ...
                                    "{" ...
                                    <STRING_LITERAL> ...
                                    "true" ...
                                    "false" ...
                                    <INTEGER_LITERAL> ...
                                    <FLOATING_POINT_LITERAL> ...
                                    <IDENTIFIER> ...
                                    "{" ...
                                    "[" ...

For the following response mapping:

#set($isAdmin = $ctx.identity.resolverContext.is_admin)

#if(!$isAdmin)
  $util.toJson({
    "availability": {
      "startsAt": $ctx.result.availability.startsAt,
      "endsAt": $ctx.result.availability.endsAt,
    }
  })
#end

$util.toJson($ctx.result)

Solution

  • Remove the end comma in your object:

    #set($isAdmin = $ctx.identity.resolverContext.is_admin)
    
    #if(!$isAdmin)
      $util.toJson({
        "availability": {
          "startsAt": $ctx.result.availability.startsAt,
          "endsAt": $ctx.result.availability.endsAt
        }
      })
    #end
    
    $util.toJson($ctx.result)
    

    Here's a validator to help.