I am having issues finding good sources for / figuring out how to correctly add server-side validation to my AppSync GraphQL mutations.
In essence I used AWS dashboard to define my AppSync schema, hence had DynamoDB tables created for me, plus some basic resolvers set up for the data.
No I need to achieve following:
inventory
and gold
purchaseItem
mutation with item_id
item_id
exists int 'Items' table of associated DynamoDB, check if player has enough gold, again in 'Players' table of associated DynamoDB, if so, write to Players
DynamoDB table by adding item to their inventory and new subtracted gold amount.I believe most efficient way to achieve this that will result in less cost and latency is to use "Apache Velocity" templating language for AppSync?
It would be great to see example of this showing how to Query / Write to DynamoDB, handle errors and resolve the mutation correctly.
For writing to DynamoDB with VTL use the following tutorial
you can start with the PutItem template. My request template looks like this:
{ "version" : "2017-02-28", "operation" : "PutItem", "key" : { "noteId" : { "S" : "${context.arguments.noteId}" }, "userId" : { "S" : "${context.identity.sub}" } }, "attributeValues" : { "title" : { "S" : "${context.arguments.title}" }, "content": { "S" : "${context.arguments.content}" } } }
For query:
{ "version" : "2017-02-28", "operation" : "Query", "query" : { ## Provide a query expression. ** "expression": "userId = :userId", "expressionValues" : { ":userId" : { "S" : "${context.identity.sub}" } } }, ## Add 'limit' and 'nextToken' arguments to this field in your schema to implement pagination. ** "limit": #if(${context.arguments.limit}) ${context.arguments.limit} #else 20 #end, "nextToken": #if(${context.arguments.nextToken}) "${context.arguments.nextToken}" #else null #end }
This is based on the Paginated Query template.