aws-serverless-appsync-app
aws-serverless-appsync-app copied to clipboard
Optional Query.getDestinationsByState
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:
{
"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."
}
]
}
- example from aws docs
{
"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.
Solution
- 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),
}
}
}
-
Create
state-index
index with partition key state. -
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.