AppSync: Resolving mutations directly off of Aurora Serverless
July 30, 2021
Mohammed Ali Chherawalla
Software Engineer
Contents
The database acts as a single source of truth in most modern applications. Backend applications typically expose APIs for CRUD operations to query and mutate values in the underlying data store.
Based on the authentication scope of the user, the users should be allowed to fetch/update/create/delete entities in the database. With strong Role-based access control(RBAC), CRUD operations can be resolved directly off of the database.
AppSync is a managed service from AWS that exposes a GraphQL interface to interact with the API. It collects data from and resolves queries and mutations from multiple data sources. An Aurora Serverless Cluster can be used as a data source. In this tutorial, I will take you through how to resolve mutations directly off of the Aurora in AppSync.
AppSync uses Apache VTL resolvers to transform GraphQL requests from the client into requests to the data source.
It provides support for the reverse as well. It translates the response from the data source into a GraphQL response.
resolve create mutations directly off of the database and return the newly created entity.
resolve update mutations directly off of the database and return the updated entity.
resolve delete mutations directly off of the database and return the deleted entity. (We will soft delete records from the database i.e “deleted_at = NOW()”)
consists of a CD pipeline that will create the required infrastructure (including the PostgreSQL DB) and deploy your AWS AppSync application using the serverless framework
has queries to fetch users, notes, and lists.
uses AWS Lambdas as a data source to resolve queries
Through the course of this tutorial, we will add support for mutations to this application.
Setup the database
Run the setup-local.sh script which will run the database migrations
./scripts/setup-local.sh
Adding PostgreSQL Aurora Serverless as an AppSync data source
Step 1
Create an rds folder with a datasources.yml file in the resources folder
git add .
git commit -m 'Add mutations and types in the schema.graphql'
Before you move on, here’s a tip top C Execs already know: LeadReads is your source for exclusive digital product insights and stories. Don’t miss out!
We need toconvert the incoming GraphQL into SQLstatements to
create a record in the database
return the created record
According to the convention, the GraphQL request is in camelCase. However, the database columns are snake_case.
Iterate over the keys in the args.input
Convert each key from camelCase to snake_case
Boolean values are stored SMALLINT in the database. If the value for input[property] is boolean we convert it to 0/1, so it can be inserted into the database.
Stringify the values and columns array. Replace square braces [] with round braces ()
This is a hack because the velocityjs engine handles stringification slightly differently. So adding this makes sure that our resolvers work both locally as well as on the deployed instance.
Step 3
Create a new file for the createNote request resolver.
Convert the DateTime value from the database into an ISO Date Time. When using RDS as a data source AppSync isn’t able to handle AWSDateTime out of the box.
Convert the snake_casecolumn names to camelCase.
Step 6
Create the mutation mapping templates for the create mutations
We need to convert the incoming GraphQL into SQL statements to
update a record in the database
return the updated record
According to convention the GraphQL request is in camelCase. However, the database columns are snake_case.
Iterate over the keys in the args.input
Convert each key from camelCase to snake_case
Boolean values are stored SMALLINT in the database. If the value for input[property] is boolean we convert it to 0/1, so it can be inserted into the database.
If $update already has a value append a comma.
Step 2
Create a new file for the updateNote request resolver.
Import the newly created collection into Postman and test outyour queries and mutations!
Where to go from here
To write tests in the postman collection and run them as part of the CI pipeline head over to our article on postman-tests.
I hope you enjoyed this tutorial on resolving mutations directly off of the database using AppSync and Aurora Serverless. If you have any questions or comments, please join the forum discussion on Twitter.
Every full moon we will visit your inbox.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
The Wednesday Newsletter
Build faster, smarter, and leaner—with AI at the core.
Learn how modern product teams can ship faster, think clearer, and build better with AI-powered product engineering at the core.
From the team behind 10% of India's unicorns.
No noise. Just ideas that move the needle.
You’re in! A fresh drop from Wednesday is heading your way 🚀
Oops! Something went wrong while submitting the form.
We respect your privacy. Unsubscribe at any time.
2025
By using this website, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. View our privacy policy for more information