Relational databases

You are currently viewing the legacy GraphQL Transformer documentation. View latest documentation

The Amplify CLI currently supports importing serverless Amazon Aurora MySQL 5.6 databases. The following instruction show how to create an Amazon Aurora Serverless database, import this database as a GraphQL data source and test it.

First, if you do not have an Amplify project with a GraphQL API create one using these simple commands.

1amplify init
2amplify add api

Go to the AWS RDS console and click "Create database".

Select "Standard Create" for the database creation method

Database Creation

For Engine Options keep the following options

Engine Option

Select "Serverless" in Database Features

Database Features

In Settings fill in the following information

Database Settings

Select the Capacity Settings as shown below

Database Capacity

Expand the "Additional connectivity configuration" and enable "Data API" and "Create New" if you do not have a VPC security group configured

Database Connectivity

Expand "Additional Configuration" and fill in "Initial Database Name" as MarketPlace

Database Additional Configuration

Click Create Database. Once created, click Query Editor on the side menu to open a connection prompt. To connect, select the cluster and fill in the credentials configured earlier.

Database Connect

After connecting, create a database and some tables.

Database details

1USE MarketPlace;
2CREATE TABLE Customers (
3 id int(11) NOT NULL PRIMARY KEY,
4 name varchar(50) NOT NULL,
5 phone varchar(50) NOT NULL,
6 email varchar(50) NOT NULL
7);
8CREATE TABLE Orders (
9 id int(11) NOT NULL PRIMARY KEY,
10 customerId int(11) NOT NULL,
11 orderDate datetime DEFAULT CURRENT_TIMESTAMP,
12 KEY `customerId` (`customerId`),
13 CONSTRAINT `customer_orders_ibfk_1` FOREIGN KEY (`customerId`) REFERENCES `Customers` (`id`)
14);

Return to your command line and run amplify api add-graphql-datasource from the root of your amplify project.

Add GraphQL Data Source

Push your project to AWS with amplify push.

Run amplify push to push your project to AWS. You can then open the AppSync console with amplify api console, to try interacting with your RDS database via your GraphQL API.

Interact with your SQL database from GraphQL

Your API is now configured to work with your serverless Amazon Aurora MySQL database. Try running a mutation to create a customer from the AppSync Console and then query it from the RDS Console to double check.

Create a customer:

1mutation CreateCustomer {
2 createCustomers(
3 createCustomersInput: {
4 id: 1
5 name: "Hello"
6 phone: "111-222-3333"
7 email: "customer1@mydomain.com"
8 }
9 ) {
10 id
11 name
12 phone
13 email
14 }
15}

GraphQL Results

Then open the RDS console and run a simple select statement to see the new customer:

1USE MarketPlace;
2SELECT * FROM Customers;

SQL Results

How does this work?

The add-graphql-datasource will add a custom stack to your project that provides a basic set of functionality for working with an existing data source. You can find the new stack in the stacks/ directory, a set of new resolvers in the resolvers/ directory, and will also find a few additions to your schema.graphql. You may edit details in the custom stack and/or resolver files without worry. You may run add-graphql-datasource again to update your project with changes in the database but be careful as these will overwrite any existing templates in the stacks/ or resolvers/ directories. When using multiple environment with the Amplify CLI, you will be asked to configure the data source once per environment.