The AWS AppSync SDK for Android enables you to access your AWS AppSync backend and perform operations like Queries
, Mutations
, and Subscriptions
. The SDK also includes support for offline operations. This SDK is based off of the Apollo project found here. Please log questions for this client SDK in this repo and questions for the AppSync service in the official AWS AppSync forum.
- A sample app using the events sample schema can be found here: https://github.com/aws-samples/aws-mobile-appsync-events-starter-android
Step by step documentation can be found here: https://aws-amplify.github.io/docs/android/api
In the project's build.gradle
, add the following dependency in
the build script:
classpath 'com.amazonaws:aws-android-sdk-appsync-gradle-plugin:2.7.+'
Sample project's build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
// ..other code..
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.amazonaws:aws-android-sdk-appsync-gradle-plugin:2.7.+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
In the app's build.gradle
, add the following plugin:
apply plugin: 'com.amazonaws.appsync'
Add the following dependency:
compile 'com.amazonaws:aws-android-sdk-appsync:2.7.+'
Sample app's build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.amazonaws.appsync'
android {
// Typical items
}
dependencies {
// Typical dependencies
compile 'com.amazonaws:aws-android-sdk-appsync:2.7.+'
}
Add the permissions to access network state to determine if the device is offline.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
To interact with AppSync, your client needs to define GraphQL queries and mutations.
For example, create a file named ./app/src/main/graphql/com/amazonaws/demo/posts/posts.graphql
:
query GetPost($id:ID!) {
getPost(id:$id) {
id
title
author
content
url
version
}
}
mutation AddPost($id: ID!, $author: String!, $title: String, $content: String, $url: String, $ups: Int!, $downs: Int!, $expectedVersion: Int!) {
putPost(id: $id, author: $author, title: $title, content: $content, url: $url, ups: $ups, downs: $downs, version: $expectedVersion) {
id
title
author
url
content
}
}
Next, fetch the schema.json
file from the AppSync console and place it alongside the posts.graphql
file.
./app/src/main/graphql/com/amazonaws/demo/posts/schema.json
Now build the project and the generated source files will be available to use within the app. They will not show up in your source directory, but are added in the build path.
AWSAppSyncClient client = AWSAppSyncClient.builder()
.context(context)
.apiKey(new BasicAPIKeyAuthProvider(Constants.APPSYNC_API_KEY)) // API Key based authorization
.region(Constants.APPSYNC_REGION)
.serverUrl(Constants.APPSYNC_API_URL)
.build();
Alternatively, you can use the awsconfiguration.json
file to supply the configuration information required to create a AWSAppSyncClient
object.
Create a file named awsconfiguration.json
under res/raw
directory of your app.
{
"AppSync": {
"Default": {
"ApiUrl": "YOUR-GRAPHQL-ENDPOINT",
"Region": "us-east-1",
"ApiKey": "YOUR-API-KEY",
"AuthMode": "API_KEY"
}
}
}
The AWSConfiguration
represents the configuration information present in awsconfiguration.json
file. By default, the information under Default
section will be used.
AWSConfiguration awsConfig = new AWSConfiguration(context);
AWSAppSyncClient client = AWSAppSyncClient.builder()
.context(context)
.awsConfiguration(awsConfig)
.build();
You can override the Default
configuration by using the AWSConfiguration#setConfiguration()
method.
{
"AppSync": {
"Default": {
"ApiUrl": "YOUR-GRAPHQL-ENDPOINT",
"Region": "us-east-1",
"ApiKey": "YOUR-API-KEY",
"AuthMode": "API_KEY"
},
"Custom": {
"ApiUrl": "YOUR-GRAPHQL-ENDPOINT",
"Region": "us-east-2",
"ApiKey": "YOUR-API-KEY",
"AuthMode": "API_KEY"
}
}
}
AWSConfiguration awsConfig = new AWSConfiguration(context);
awsConfig.setConfiguration("Custom");
AWSAppSyncClient client = AWSAppSyncClient.builder()
.context(context)
.awsConfiguration(awsConfig)
.build();
When making calls to AWS AppSync, there are several ways to authenticate those calls. The API key authorization (API_KEY) is the simplest way to onboard, but we recommend you use either Amazon IAM (AWS_IAM) or Amazon Cognito UserPools (AMAZON_COGNITO_USER_POOLS) or any OpenID Connect Provider (OPENID_CONNECT) after you onboard with an API key.
For authorization using the API key, update the awsconfiguration.json
file and code snippet as follows:
Add the following snippet to your awsconfiguration.json
file.
{
"AppSync": {
"Default": {
"ApiUrl": "YOUR-GRAPHQL-ENDPOINT",
"Region": "us-east-1",
"ApiKey": "YOUR-API-KEY",
"AuthMode": "API_KEY"
}
}
}
Add the following code to use the information in the Default
section from awsconfiguration.json
file.
AWSAppSyncClient client = AWSAppSyncClient.builder()
.context(context)
.awsConfiguration(new AWSConfiguration(context))
.build();
For authorization using the Amazon IAM credentials using Amazon IAM or Amazon STS or Amazon Cognito, update the awsconfiguration.json
file and code snippet as follows:
Add the following snippet to your awsconfiguration.json
file.
{
"CredentialsProvider": {
"CognitoIdentity": {
"Default": {
"PoolId": "YOUR-COGNITO-IDENTITY-POOLID",
"Region": "us-east-1"
}
}
},
"AppSync": {
"Default": {
"ApiUrl": "YOUR-GRAPHQL-ENDPOINT",
"Region": "us-east-1",
"AuthMode": "AWS_IAM"
}
}
}
Add the following code to use the information in the Default
section from awsconfiguration.json
file.
AWSConfiguration awsConfig = new AWSConfiguration(context);
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(context, awsConfig);
AWSAppSyncClient client = AWSAppSyncClient.builder()
.context(context)
.awsConfiguration(awsConfig)
.credentialsProvider(credentialsProvider)
.build();
For authorization using the Amazon Cognito UserPools, update the awsconfiguration.json
file and code snippet as follows:
Add the following snippet to your awsconfiguration.json
file.
{
"CognitoUserPool": {
"Default": {
"PoolId": "POOL-ID",
"AppClientId": "APP-CLIENT-ID",
"AppClientSecret": "APP-CLIENT-SECRET",
"Region": "us-east-1"
}
},
"AppSync": {
"Default": {
"ApiUrl": "YOUR-GRAPHQL-ENDPOINT",
"Region": "us-east-1",
"AuthMode": "AMAZON_COGNITO_USER_POOLS"
}
}
}
Add the following dependency to your app in order to use Amazon Cognito UserPools:
dependencies {
implementation 'com.amazonaws:aws-android-sdk-cognitoidentityprovider:2.9.+'
}
Add the following code to use the information in the Default
section from awsconfiguration.json
file.
AWSConfiguration awsConfig = new AWSConfiguration(context);
CognitoUserPool cognitoUserPool = new CognitoUserPool(context, awsConfig);
BasicCognitoUserPoolsAuthProvider basicCognitoUserPoolsAuthProvider = new BasicCognitoUserPoolsAuthProvider(cognitoUserPool);
AWSAppSyncClient awsAppSyncClient = AWSAppSyncClient.builder()
.context(context)
.awsConfiguration(awsConfig)
.cognitoUserPoolsAuthProvider(basicCognitoUserPoolsAuthProvider)
.build();
For authorization using any OIDC (OpenID Connect) Identity Provider, update the awsconfiguration.json
file and code snippet as follows:
Add the following snippet to your awsconfiguration.json
file.
{
"AppSync": {
"Default": {
"ApiUrl": "YOUR-GRAPHQL-ENDPOINT",
"Region": "us-east-1",
"AuthMode": "OPENID_CONNECT"
}
}
}
Add the following code to use the information in the Default
section from awsconfiguration.json
file.
AWSAppSyncClient client = AWSAppSyncClient.builder()
.context(context)
.awsConfiguration(new AWSConfiguration(context))
.oidcAuthProvider(new OidcAuthProvider() {
@Override
public String getLatestAuthToken() {
return "jwt-token-from-oidc-provider";
}
})
.build();
public void addPost() {
AddPostMutation addPostMutation = AddPostMutation.builder()
.id(UUID.randomUUID().toString())
.title(title)
.author(author)
.url(url)
.content(content)
.ups(0)
.downs(0)
.expectedVersion(1)
.build();
client.mutate(addPostMutation).enqueue(postsCallback);
}
private GraphQLCall.Callback<AddPostMutation.Data> postsCallback = new GraphQLCall.Callback<AddPostMutation.Data>() {
@Override
public void onResponse(@Nonnull final Response<AddPostMutation.Data> response) {
// non-UI calls
runOnUiThread(new Runnable() {
@Override
public void run() {
// UI related calls
}
});
}
@Override
public void onFailure(@Nonnull final ApolloException e) {
// Error handling
}
};
This library is licensed under the Amazon Software License.