Connect API and database to the app
Now that you’ve created and configured your application and initialized a new Amplify project, you can add a feature. The first feature you will add is an API.
The Amplify CLI supports creating and interacting with two types of API categories: REST and GraphQL.
The API you will be creating in this step is a GraphQL API using AWS AppSync (a managed GraphQL service) and the database will be Amazon DynamoDB (a NoSQL database).
Create a GraphQL API and database
Add a GraphQL API to your app and automatically provision a database by running the following command from the root of your application directory:
amplify add api
Accept the default values which are highlighted below:
? Select from one of the below mentioned services: GraphQL? Here is the GraphQL API that we will create. Select a setting to edit or continue Continue? Choose a schema template: Single object with fields (e.g., “Todo” with ID, name, description)
The CLI will prompt you to open this GraphQL schema in your text editor.
amplify/backend/api/your-api-name/schema.graphql
# This "input" configures a global authorization rule to enable public access to# all models in this schema. Learn more about authorization rules here: https://docs.amplify.aws/react/build-a-backend/graphqlapi/customize-authorization-rules/
input AMPLIFY { globalAuthRule: AuthRule = { allow: public }} # FOR TESTING ONLY!type Todo @model { id: ID! name: String! description: String}
The schema generated is for a Todo app. You'll notice a @model
directive on the Todo
type. This directive is part of the Amplify GraphQL API category.
Amplify GraphQL API provides custom GraphQL directives that allow you to define data models, set up authorization rules, configure serverless functions as resolvers, and more.
A GraphQL type decorated with the @model
directive will scaffold out the database table for the type (Todo table), the schema for CRUD (create, read, update, delete) and list operations, and the GraphQL resolvers needed to make everything work together.
From the command line, press enter to accept the schema and continue to the next steps.
Deploying the API
To deploy this backend, run the push
command:
amplify push
Choose the following values for each prompt:
✔ Are you sure you want to continue? (Y/n) · yes...? Do you want to generate code for your newly created GraphQL API: Yes? Choose the code generation language target: typescript? Enter the file name pattern of graphql queries, mutations and subscriptions: src/graphql/**/*.ts? Do you want to generate/update all possible GraphQL operations - queries, mutations and subscriptions: Yes? Enter maximum statement depth [increase from default if your schema is deeply nested]: 2? Enter the file name for the generated code: src/API.ts
✔ Are you sure you want to continue? (Y/n) · yes...? Do you want to generate code for your newly created GraphQL API Yes? Choose the code generation language target javascript? Enter the file name pattern of graphql queries, mutations and subscriptions src/graphql/**/*.js? Do you want to generate/update all possible GraphQL operations - queries, mutations and subscriptions Yes? Enter maximum statement depth [increase from default if your schema is deeply nested] 2? Enter the file name for the generated code src/API.js
Now the API is live and you can start interacting with it! The API you have deployed includes operations for creating, reading, updating, deleting, and listing posts.
Review deployment status
Next, run the following command to check Amplify's status:
amplify status
This will give us the current status of the Amplify project, including the current environment, any categories that have been created, and what state those categories are in. It should look similar to this:
Current Environment: dev
┌──────────┬───────────────────────┬───────────┬───────────────────┐│ Category │ Resource name │ Operation │ Provider plugin │├──────────┼───────────────────────┼───────────┼───────────────────┤│ Api │ your-api-name │ No Change │ awscloudformation │└──────────┴───────────────────────┴───────────┴───────────────────┘
Review deployed API in AWS console
To view the GraphQL API in the AppSync console at any time, run the following command:
amplify console api
To view your entire app in the Amplify console at any time, run the following command:
amplify console
Test API with local mocking
To test this out locally, you can run the mock
command. Note: Refer to the instructions to setup mocking.
If you'd like to go ahead and connect the front end, you can jump to the next step.
amplify mock api
Note: amplify mock api
requires Java.
# If you have not already deployed your API, you will be walked through the following steps for GraphQL code generation? Choose the code generation language target: javascript (or preferred target)? Enter the file name pattern of graphql queries, mutations and subscriptions: src/graphql/**/*.js? Do you want to generate/update all possible GraphQL operations - queries, mutations and subscriptions: Yes? Enter maximum statement depth [increase from default if your schema is deeply nested] 2
This will open the GraphiQL explorer on a local port. From the test environment you can try out different operations locally, like queries and mutations.
In the GraphiQL toolbar, select Use: User Pool and try creating a todo:
mutation CreateTodo { createTodo(input: { name: "Test Todo", description: "Todo description" }) { id owner name updatedAt createdAt description }}
Next, update auth to Use: API Key and try querying the list of todos:
query ListTodos { listTodos { items { description createdAt id owner name } }}
Connect frontend to API
Next, open App.vue.
Writing data with GraphQL mutations
To create a new todo in the database, create a client
by using the generateClient()
operation, then pass the createTodo
mutation into client.graphql()
along with the data you'd like to write.
<script setup> import * as mutations from './graphql/mutations'; import { generateClient } from 'aws-amplify/api'; import { onMounted, ref } from 'vue';
const name = ref(''); const description = ref('');
const client = generateClient();
async function addTodo() { if (!name.value || !description.value) return; const todo = { name: name.value, description: description.value }; await client.graphql({ query: mutations.createTodo, variables: { input: todo } }); name.value = ''; description.value = ''; }</script><template> <div id="app"> <h1>Todo App</h1> <input type="text" v-model="name" placeholder="Todo name" /> <input type="text" v-model="description" placeholder="Todo description" /> <button v-on:click="addTodo">Create Todo</button> </div></template>
Reading data with GraphQL queries
To display the data, update App.vue
to list all the items in the database by importing queries
and then using the onMounted()
Vue lifecycle hook to update the page when a query runs on page load:
<script setup> import * as mutations from './graphql/mutations'; import * as queries from './graphql/queries'; import { generateClient } from 'aws-amplify/api'; import { onMounted, ref } from 'vue';
const name = ref(''); const description = ref(''); const todos = ref([]);
const client = generateClient();
async function addTodo() { if (!name.value || !description.value) return; const todo = { name: name.value, description: description.value }; await client.graphql({ query: mutations.createTodo, variables: { input: todo } }); name.value = ''; description.value = ''; }
async function fetchTodos() { const fetchedTodos = await client.graphql({ query: queries.listTodos });
todos.value = fetchedTodos.data.listTodos.items; }
onMounted(() => { fetchTodos(); });</script><template> <div id="app"> <h1>Todo App</h1> <input type="text" v-model="name" placeholder="Todo name" /> <input type="text" v-model="description" placeholder="Todo description" /> <button v-on:click="addTodo">Create Todo</button>
<div v-for="item in todos" :key="item.id"> <h3>{{ item.name }}</h3> <p>{{ item.description }}</p> </div> </div></template>
Real-time data with GraphQL subscriptions
Now if you wish to subscribe to data, import the onCreateTodo
subscription and create a new subscription by passing the onCreateTodo
subscription on client.graphql()
like so:
<script setup> import * as mutations from './graphql/mutations'; import * as queries from './graphql/queries'; import * as subscriptions from './graphql/subscriptions'; import { generateClient } from 'aws-amplify/api'; import { onMounted, ref } from 'vue';
const name = ref(''); const description = ref(''); const todos = ref([]);
const client = generateClient();
async function addTodo() { if (!name.value || !description.value) return; const todo = { name: name.value, description: description.value }; await client.graphql({ query: mutations.createTodo, variables: { input: todo } }); name.value = ''; description.value = ''; }
async function fetchTodos() { const fetchedTodos = await client.graphql({ query: queries.listTodos });
todos.value = fetchedTodos.data.listTodos.items; }
function subscribeToNewTodos() { client .graphql({ query: subscriptions.onCreateTodo }) .subscribe({ next: ({ data }) => { todos.value = [...todos.value, data.onCreateTodo]; } }); }
onMounted(() => { fetchTodos(); subscribeToNewTodos(); });</script><template> <div id="app"> <h1>Todo App</h1> <input type="text" v-model="name" placeholder="Todo name" /> <input type="text" v-model="description" placeholder="Todo description" /> <button v-on:click="addTodo">Create Todo</button>
<div v-for="item in todos" :key="item.id"> <h3>{{ item.name }}</h3> <p>{{ item.description }}</p> </div> </div></template>
Run locally
Next, run the app and you should see the updated UI with the ability to create and view the list of todos:
npm run dev
You have successfully deployed your API and connected it to your app!