aws-serverless-appsync-app icon indicating copy to clipboard operation
aws-serverless-appsync-app copied to clipboard

Optional Query.getDestinationsByState

Open idkjs opened this issue 6 years ago • 1 comments

Hey folks. Thanks for sharing this. So many good bits, especially on resolver mapping.

I can't get the optional resolver to resolve.

Using the suggested mapping:

{
    "version": "2017-02-28",
    "operation": "Query",
    "index":"state-index",
    "query":{
    	"expression":"#state = :state",
        "expressionNames":{
        	"#state":"state"
        },
        "expressionValues":{
        	":state" :{
            	"S": $util.dynamodb.toDynamoDBJson($ctx.args.state),
            }
        }
    }
}

with this default request mapping template:

## Pass back the result from DynamoDB. **
$util.toJson($ctx.result.items)
  • called with this query:
query GetByState {
  getDestinationsByState(state:"Florida"){
    id
    description
  }
}
  • returns this error: screen shot 2018-12-02 at 11 08 03 am
{
  "data": {
    "getDestinationsByState": null
  },
  "errors": [
    {
      "path": [
        "getDestinationsByState"
      ],
      "data": null,
      "errorType": "MappingTemplate",
      "errorInfo": null,
      "locations": [
        {
          "line": 14,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "Value for field '$[query][expressionValues][:state][S]' must be text."
    }
  ]
}
{
    "version": "2017-02-28",
    "operation": "Query",
    "index": "todoid-index",
    "query": {
        "expression": "todoid = :todoid",
        "expressionValues": {
            ":todoid": {
                "S": "$context.source.id"
            }
        }
    }
}

I'm thinking the solution is to create an index called state-index. We can only create a secondary index when we create the dynamodb table. What would you suggest is the best way to go about setting up the optional query?

Thanks.

idkjs avatar Dec 02 '18 10:12 idkjs

Solution

  1. Change the suggested mapping expressionValues key to the following to deal with
 "message": "Value for field '$[query][expressionValues][:state][S]' must be text."

error:

{
    "version": "2017-02-28",
    "operation": "Query",
    "index":"state-index",
    "query":{
    	"expression":"#state = :state",
        "expressionNames":{
        	"#state":"state"
        },
        "expressionValues":{
        	":state" : $util.dynamodb.toDynamoDBJson($ctx.args.state),
            
        }
    }
}
  1. Create state-index index with partition key state. screen shot 2018-12-02 at 11 43 17 am

  2. Running query from above returns:

{
  "data": {
    "getDestinationsByState": [
      {
        "id": "b7214nf8-4242-40nc-b68a-15f212xxxxx",
        "description": "Disney World"
      }
    ]
  }
}

This works but is it the best way?

Thanks.

idkjs avatar Dec 02 '18 10:12 idkjs