Set up Amplify GraphQL API
The Amplify API category provides an interface for retrieving and persisting your model data. The API category comes with default built-in support for AWS AppSync. The Amplify CLI allows you to define your API and provision a GraphQL service with CRUD operations and real-time functionality.
Goal
To setup and configure your application with Amplify API to save items in the backend.
Prerequisites
- An Android application targeting Android API level 24 (Android 7.0) or above
- For a full example of creating Android project, please follow the project setup walkthrough
Configure API
To start provisioning API resources in the backend, go to your project directory and execute the command:
1amplify add api
Enter the following when prompted:
1? Please select from one of the below mentioned services:2 `GraphQL`3# The part below will show you some options you can change, if you wish to change any of them you can navigate with4# your arrow keys and update any field, otherwise you can click on `Continue` to move on5? Here is the GraphQL API that we will create. Select a setting to edit or continue6 `Continue`7? Choose a schema template:8 `Single object with fields (e.g., “Todo” with ID, name, description)`9? Do you want to edit the schema now?10 `No`
The guided schema creation will output amplify/backend/api/{api_name}/schema.graphql
containing the following:
1type Todo @model {2 id: ID!3 name: String!4 description: String5}
To push your changes to the cloud, execute the command:
1amplify push
Upon completion, amplifyconfiguration.json
will be updated to reference provisioned backend resources. Note that this file should already be a part of your project if you followed the Project setup walkthrough.
Generate Todo Model class
To generate the Todo
model, change directories to your project folder and execute the command:
1amplify codegen models
The generated files will be under the app/src/main/java/com/amplifyframework.datastore.generated.model
directory. It is strongly advised not to put any hand written code in app/src/main/java/com/amplifyframework.datastore.generated
directory as it gets re-generated each time codegen is run.
Install Amplify Libraries
Expand Gradle Scripts, open build.gradle (Module :app). You will already have configured Amplify by following the steps in the Project Setup walkthrough.
Add these libraries into the dependencies
block:
1dependencies {2 implementation 'com.amplifyframework:core:ANDROID_VERSION'3 implementation 'com.amplifyframework:aws-api:ANDROID_VERSION'4}
Click Sync Now.
Initialize Amplify API
Call Amplify.addPlugin()
to initialize the Amplify API category followed by Amplify.configure()
.
Add the following code to your onCreate()
method in your application class:
1Amplify.addPlugin(new AWSApiPlugin());
Your class will look like this:
1public class MyAmplifyApp extends Application {2 @Override3 public void onCreate() {4 super.onCreate();5
6 try {7 // Add these lines to add the AWSApiPlugin plugins8 Amplify.addPlugin(new AWSApiPlugin());9 Amplify.configure(getApplicationContext());10
11 Log.i("MyAmplifyApp", "Initialized Amplify");12 } catch (AmplifyException error) {13 Log.e("MyAmplifyApp", "Could not initialize Amplify", error);14 }15 }16}
Create a Todo
1Todo todo = Todo.builder()2 .name("My first todo")3 .description("todo description")4 .build();5
6Amplify.API.mutate(7 ModelMutation.create(todo),8 response -> Log.i("MyAmplifyApp", "Added Todo with id: " + response.getData().getId()),9 error -> Log.e("MyAmplifyApp", "Create failed", error)10);
Upon successfully executing this code, you should see an instance of todo
persisted in your dynamoDB table.
To navigate to your backend, run amplify console api
and choose GraphQL
. This will open the AppSync console to your GraphQL service. Select Data Sources
and select the Resource link in your TodoTable
to bring you to the DynamoDB Console. Select the items
tab to see the Todo
object that has been persisted in your database.
Next steps
Congratulations! You've created a Todo
object in your database. Check out the following links to see other Amplify API use cases: