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:
amplify add apiEnter the following when prompted:
? Please select from one of the below mentioned services: `GraphQL`# The part below will show you some options you can change, if you wish to change any of them you can navigate with# your arrow keys and update any field, otherwise you can click on `Continue` to move on? 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)`? Do you want to edit the schema now? `No`The guided schema creation will output amplify/backend/api/{api_name}/schema.graphql containing the following:
type Todo @model { id: ID! name: String! description: String}To push your changes to the cloud, execute the command:
amplify pushUpon 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:
amplify codegen modelsThe 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:
dependencies { implementation 'com.amplifyframework:core:ANDROID_VERSION' implementation 'com.amplifyframework:aws-api:ANDROID_VERSION'}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:
Amplify.addPlugin(new AWSApiPlugin());Your class will look like this:
public class MyAmplifyApp extends Application { @Override public void onCreate() { super.onCreate();
try { // Add these lines to add the AWSApiPlugin plugins Amplify.addPlugin(new AWSApiPlugin()); Amplify.configure(getApplicationContext());
Log.i("MyAmplifyApp", "Initialized Amplify"); } catch (AmplifyException error) { Log.e("MyAmplifyApp", "Could not initialize Amplify", error); } }}Amplify.addPlugin(AWSApiPlugin())Your class will look like this:
class MyAmplifyApp : Application() { override fun onCreate() { super.onCreate()
try { // Add these lines to add the AWSApiPlugin plugins Amplify.addPlugin(AWSApiPlugin()) Amplify.configure(applicationContext)
Log.i("MyAmplifyApp", "Initialized Amplify") } catch (error: AmplifyException) { Log.e("MyAmplifyApp", "Could not initialize Amplify", error) } }}RxAmplify.addPlugin(new AWSApiPlugin());Your class will look like this:
public class MyAmplifyApp extends Application { @Override public void onCreate() { super.onCreate();
try { // Add these lines to add the AWSApiPlugin plugins RxAmplify.addPlugin(new AWSApiPlugin()); RxAmplify.configure(getApplicationContext());
Log.i("MyAmplifyApp", "Initialized Amplify"); } catch (AmplifyException error) { Log.e("MyAmplifyApp", "Could not initialize Amplify", error); } }}Create a Todo
Todo todo = Todo.builder() .name("My first todo") .description("todo description") .build();
Amplify.API.mutate( ModelMutation.create(todo), response -> Log.i("MyAmplifyApp", "Added Todo with id: " + response.getData().getId()), error -> Log.e("MyAmplifyApp", "Create failed", error));val todo = Todo.builder() .name("My first todo") .description("todo description") .build()
Amplify.API.mutate(ModelMutation.create(todo), { Log.i("MyAmplifyApp", "Added Todo with id: ${it.data.id}") }, { Log.e("MyAmplifyApp", "Create failed", it) })val todo = Todo.builder() .name("My first todo") .description("todo description") .build()try { val response = Amplify.API.mutate(ModelMutation.create(todo)) Log.i("MyAmplifyApp", "Added Todo with id: ${response.data.id}")} catch (error: ApiException) { Log.e("MyAmplifyApp", "Create failed", error)}Todo todo = Todo.builder() .name("My first todo") .description("todo description") .build();
RxAmplify.API.mutate(ModelMutation.create(todo)) .subscribe( response -> Log.i("MyAmplifyApp", "Added Todo with id: " + response.getData().getId()), error -> Log.e("MyAmplifyApp", "Create failed", error) );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: