Rethink how your product delivery teams build and design your products. Architect to build the building blocks that allow experimentation with Amplify.
From user research, digital strategy to solving bold engineering problems. Our team specialises in providing a suite of services that take an idea from a rough sketch to an enterprise grade product.
Learning new technologies and frameworks ensures we are ahead of the curve. Here is a collection of step by step tutorials about things we've learnt. Learn with us!
We love open source, and we love giving back. Take a look at our open source products and how we're pushing the bounds of Engineering excellence one product at a time
We believe the best digital products are built by a diverse and skilled team. We’ve created a safe inclusive workspace, and we believe in diversity. We are a group that believes in software development and design is a craft. This is what unites us.
Our team is diverse. Each coming from a different background and beliefs. We think of product development & design as a craft. We love to learn new ways of improving our craft - be it learning new frameworks, or adding new specialties.
We believe the best digital products are built by a diverse and skilled team. We’ve created a safe inclusive workspace, and we believe in diversity. We are a group that believes in software development and design is a craft. This is what unites us.
Our team is diverse. Each coming from a different background and beliefs. We think of product development & design as a craft. We love to learn new ways of improving our craft - be it learning new frameworks, or adding new specialties.
We believe the best digital products are built by a diverse and skilled team. We’ve created a safe inclusive workspace, and we believe in diversity. We are a group that believes in software development and design is a craft. This is what unites us.
Our team is diverse. Each coming from a different background and beliefs. We think of product development & design as a craft. We love to learn new ways of improving our craft - be it learning new frameworks, or adding new specialties.
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
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.