Page updated Nov 15, 2023

Connect your app code to the API

In this guide, you will connect your application code to the backend API using the Amplify Libraries. We will review how to install and configure Amplify Libraries.

Before you begin, you will need:

Install the Amplify Library

The Amplify Library is the recommended client library used to connect to your GraphQL APIs. To install the the Amplify client library, navigate to your frontend's root folder and run the following command in your Terminal:

npm install aws-amplify
1npm install aws-amplify

Configure the Amplify Library

You will then configure the library to connect with the Amplify CLI-provisioned API or you existing AppSync GraphQL API:

When you deploy your backend (amplify push), an aws-exports.js file is generated for you. This file contains your API's endpoint information and auth configurations.

import { Amplify } from 'aws-amplify' // highlight-next-line import awsconfig from './aws-exports' // highlight-next-line Amplify.configure(awsconfig)
1import { Amplify } from 'aws-amplify'
2// highlight-next-line
3import awsconfig from './aws-exports'
4
5// highlight-next-line
6Amplify.configure(awsconfig)

Generate GraphQL client code and typings

You can generate common GraphQL queries, mutations, and subscriptions directly from your backend GraphQL schema. This allows you to avoid hand-authoring GraphQL documents from scratch when developing your API quickly and also provides you end-to-end typing.

By default, when you deploy your API with amplify push, you're prompted to generate client code for GraphQL queries, mutations, and subscriptions. The generated GraphQL documents are stored in src/graphql/**/.

amplify push
1amplify push
? 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
1? Do you want to generate code for your newly created GraphQL API
2> Yes
3? Choose the code generation language target
4> javascript
5? Enter the file name pattern of graphql queries, mutations and subscriptions
6> src/graphql/**/*.js
7? Do you want to generate/update all possible GraphQL operations - queries, mutations and subscriptions
8> Yes
9? Enter maximum statement depth [increase from default if your schema is deeply nested]
10> 2

To customize any of the codegen settings above, run the following command in your Terminal:

amplify update codegen
1amplify update codegen

Use generated GraphQL queries, mutations, and subscriptions

You can use the generated GraphQL code after you import them into your codebase and pass them into the API.graphql() operation. With GraphQL, you typically have the following types of operations to interact with the API from a JavaScript client:

  • Mutations - write data to the API (create, update, and delete operations)
import { API } from 'aws-amplify'; import { createTodo, updateTodo, deleteTodo } from './graphql/mutations'; import { GraphQLQuery } from '@aws-amplify/api'; import { CreateTodoInput, CreateTodoMutation, UpdateTodoMutation, DeleteTodoMutation } from './API'; const todo: CreateTodoInput = { name: "My first todo", description: "Hello world!" }; /* create a todo */ await API.graphql<GraphQLQuery<CreateTodoMutation>>({ query: createTodo, variables: { input: todo } }); /* update a todo */ await API.graphql<GraphQLQuery<UpdateTodoMutation>>({ query: updateTodo, variables: { input: { id: "ENTER_TODO_ID_HERE", name: "Updated todo info" } } }); /* delete a todo */ await API.graphql<GraphQLQuery<DeleteTodoMutation>>({ query: deleteTodo, variables: { input: { id: "ENTER_TODO_ID_HERE" } } });
1import { API } from 'aws-amplify';
2import { createTodo, updateTodo, deleteTodo } from './graphql/mutations';
3import { GraphQLQuery } from '@aws-amplify/api';
4import { CreateTodoInput, CreateTodoMutation, UpdateTodoMutation, DeleteTodoMutation } from './API';
5
6const todo: CreateTodoInput = { name: "My first todo", description: "Hello world!" };
7
8/* create a todo */
9await API.graphql<GraphQLQuery<CreateTodoMutation>>({
10 query: createTodo,
11 variables: {
12 input: todo
13 }
14});
15
16/* update a todo */
17await API.graphql<GraphQLQuery<UpdateTodoMutation>>({
18 query: updateTodo,
19 variables: {
20 input: {
21 id: "ENTER_TODO_ID_HERE",
22 name: "Updated todo info"
23 }
24 }
25});
26
27/* delete a todo */
28await API.graphql<GraphQLQuery<DeleteTodoMutation>>({
29 query: deleteTodo,
30 variables: {
31 input: {
32 id: "ENTER_TODO_ID_HERE"
33 }
34 }
35});
  • Queries - read data from the API (list and get operations)
import { API } from 'aws-amplify'; import { GraphQLQuery } from '@aws-amplify/api'; import { listTodos } from './graphql/queries'; import { ListTodosQuery } from './API'; const todos = await API.graphql<GraphQLQuery<ListTodosQuery>>({ query: listTodos });
1import { API } from 'aws-amplify';
2import { GraphQLQuery } from '@aws-amplify/api';
3import { listTodos } from './graphql/queries';
4import { ListTodosQuery } from './API';
5
6const todos = await API.graphql<GraphQLQuery<ListTodosQuery>>({ query: listTodos });

To learn more about queries, mutations, and subscriptions in more detail, see the guides to Create, update, and delete application data and Read application data.

Conclusion

Congratulations! You have finished the Connect your app code to the API guide. In this guide, you reviewed the required configuration and installed and configured Amplify Libraries. You also reviewed some quick ways to write and read this data.

Next steps

Our recommended next steps include using the API to mutate and query data. You can also review how to subscribe to real-time events to look for mutations in your data. Some resources that will help with this work include: