Search in sources :

Example 16 with AWSAppSyncClient

use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient in project aws-mobile-appsync-sdk-android by awslabs.

the class AWSAppSyncClients method withUserPools2FromAWSConfiguration.

@NonNull
public static AWSAppSyncClient withUserPools2FromAWSConfiguration(String idTokenStringForCustomCognitoUserPool) {
    // Amazon Cognito User Pools - Custom CognitoUserPool
    AWSConfiguration awsConfiguration = new AWSConfiguration(getTargetContext());
    awsConfiguration.setConfiguration("MultiAuthAndroidIntegTestApp_AMAZON_COGNITO_USER_POOLS_2");
    JSONObject appSyncConfig = awsConfiguration.optJsonObject("AppSync");
    String clientDatabasePrefix = JsonExtract.stringValue(appSyncConfig, "ClientDatabasePrefix");
    String clientName = JsonExtract.stringValue(appSyncConfig, "AuthMode");
    AWSAppSyncClient.Builder awsAppSyncClientBuilder4 = AWSAppSyncClient.builder().context(getTargetContext()).cognitoUserPoolsAuthProvider(() -> idTokenStringForCustomCognitoUserPool).awsConfiguration(awsConfiguration).useClientDatabasePrefix(true);
    AWSAppSyncClient awsAppSyncClient4 = awsAppSyncClientBuilder4.build();
    validateAppSyncClient(awsAppSyncClient4, clientDatabasePrefix, clientName);
    return awsAppSyncClient4;
}
Also used : AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) JSONObject(org.json.JSONObject) AWSConfiguration(com.amazonaws.mobile.config.AWSConfiguration) NonNull(androidx.annotation.NonNull)

Example 17 with AWSAppSyncClient

use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient in project aws-mobile-appsync-sdk-android by awslabs.

the class AddPostActivity method save.

private void save() {
    final AddPostActivity display = this;
    String title = ((EditText) findViewById(R.id.updateTitle)).getText().toString();
    String author = ((EditText) findViewById(R.id.updateAuthor)).getText().toString();
    String url = ((EditText) findViewById(R.id.updateUrl)).getText().toString();
    String content = ((EditText) findViewById(R.id.updateContent)).getText().toString();
    final AWSAppSyncClient client = ClientFactory.getInstance(this.getApplicationContext());
    CreatePostMutation.Data expected = new CreatePostMutation.Data(null);
    CreatePostInput createPostInput = CreatePostInput.builder().title(title).author(author).content(content).url(url).ups(0).downs(0).build();
    CreatePostMutation addPostMutation = CreatePostMutation.builder().input(createPostInput).build();
    client.mutate(addPostMutation, expected).enqueue(new GraphQLCall.Callback<CreatePostMutation.Data>() {

        @Override
        public void onResponse(@Nonnull final Response<CreatePostMutation.Data> response) {
            Log.d(TAG, "Post added");
            // Add To Delta Table
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(display, "Added!", Toast.LENGTH_SHORT).show();
                    display.finish();
                }
            });
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(display, "Failed to add post!", Toast.LENGTH_SHORT).show();
                    display.finish();
                }
            });
            Log.d(TAG, "Post add failed with [" + e.getLocalizedMessage() + "]");
            e.printStackTrace();
        }
    });
}
Also used : GraphQLCall(com.apollographql.apollo.GraphQLCall) CreatePostInput(type.CreatePostInput) CreatePostMutation(com.amazonaws.amplify.generated.graphql.CreatePostMutation) AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) ApolloException(com.apollographql.apollo.exception.ApolloException)

Example 18 with AWSAppSyncClient

use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient in project aws-mobile-appsync-sdk-android by awslabs.

the class Posts method query.

/**
 * Queries for a post with a given ID.
 * @param client AppSync client through which to make the query
 * @param responseFetcher A fetcher, e.g. {@link CacheAndNetworkFetcher}
 * @param postId ID of a post for which to query
 * @return cached/network responses, labeled in a Map as "NETWORK" and/or "CACHE"
 */
public static Map<String, Response<GetPostQuery.Data>> query(AWSAppSyncClient client, ResponseFetcher responseFetcher, String postId) {
    Log.d(TAG, "Calling Query queryPost with responseFetcher: " + responseFetcher.toString());
    int expectedResponseCount = responseFetcher.equals(AppSyncResponseFetchers.CACHE_AND_NETWORK) ? 2 : 1;
    CountDownLatch queryCountDownLatch = new CountDownLatch(expectedResponseCount);
    Map<String, Response<GetPostQuery.Data>> getPostQueryResponses = new HashMap<>();
    client.query(GetPostQuery.builder().id(postId).build()).responseFetcher(responseFetcher).enqueue(DelegatingGraphQLCallback.to(response -> {
        if (response.fromCache()) {
            getPostQueryResponses.put("CACHE", response);
        } else {
            getPostQueryResponses.put("NETWORK", response);
        }
        queryCountDownLatch.countDown();
    }, failure -> {
        failure.printStackTrace();
        queryCountDownLatch.countDown();
    }));
    Await.latch(queryCountDownLatch);
    Log.d(TAG, "responseFetcher: " + responseFetcher.toString() + "; Cache response: " + getPostQueryResponses.get("CACHE"));
    Log.d(TAG, "responseFetcher: " + responseFetcher.toString() + "; Network response: " + getPostQueryResponses.get("NETWORK"));
    return getPostQueryResponses;
}
Also used : Response(com.apollographql.apollo.api.Response) AddPostMutation(com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation) AddPostRequiredFieldsOnlyMutation(com.amazonaws.mobileconnectors.appsync.demo.AddPostRequiredFieldsOnlyMutation) AppSyncResponseFetchers(com.amazonaws.mobileconnectors.appsync.fetcher.AppSyncResponseFetchers) HashMap(java.util.HashMap) AllPostsQuery(com.amazonaws.mobileconnectors.appsync.demo.AllPostsQuery) UpdatePostMutation(com.amazonaws.mobileconnectors.appsync.demo.UpdatePostMutation) AppSyncMutationCall(com.amazonaws.mobileconnectors.appsync.AppSyncMutationCall) DelegatingGraphQLCallback(com.amazonaws.mobileconnectors.appsync.client.DelegatingGraphQLCallback) Map(java.util.Map) GetPostQuery(com.amazonaws.mobileconnectors.appsync.demo.GetPostQuery) DeletePostInput(com.amazonaws.mobileconnectors.appsync.demo.type.DeletePostInput) CacheAndNetworkFetcher(com.apollographql.apollo.internal.fetcher.CacheAndNetworkFetcher) Log(android.util.Log) AddPostMissingRequiredFieldsMutation(com.amazonaws.mobileconnectors.appsync.demo.AddPostMissingRequiredFieldsMutation) Sleep(com.amazonaws.mobileconnectors.appsync.util.Sleep) Assert.assertNotNull(org.junit.Assert.assertNotNull) CreatePostInput(com.amazonaws.mobileconnectors.appsync.demo.type.CreatePostInput) Await(com.amazonaws.mobileconnectors.appsync.util.Await) LatchedGraphQLCallback(com.amazonaws.mobileconnectors.appsync.client.LatchedGraphQLCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Assert.assertNull(org.junit.Assert.assertNull) UpdatePostInput(com.amazonaws.mobileconnectors.appsync.demo.type.UpdatePostInput) Response(com.apollographql.apollo.api.Response) ResponseFetcher(com.apollographql.apollo.fetcher.ResponseFetcher) AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) Assert.assertEquals(org.junit.Assert.assertEquals) DeletePostMutation(com.amazonaws.mobileconnectors.appsync.demo.DeletePostMutation) GetPostQuery(com.amazonaws.mobileconnectors.appsync.demo.GetPostQuery) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 19 with AWSAppSyncClient

use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient in project aws-mobile-appsync-sdk-android by awslabs.

the class MultiClientInstrumentationTest method testClearMutationsCacheOnly.

@Test
public void testClearMutationsCacheOnly() throws ClearCacheException {
    AWSAppSyncClient client = AWSAppSyncClients.withAPIKEYFromAWSConfiguration();
    Response<AllPostsQuery.Data> networkResponse = Posts.list(client, AppSyncResponseFetchers.NETWORK_FIRST).get("NETWORK");
    assertNotNull(networkResponse);
    assertNotNull(networkResponse.data());
    goOffline();
    // Add a post
    AddPostMutation.Data addPostMutationData = new AddPostMutation.Data(new AddPostMutation.CreatePost("Post", "", "", "", "", "", null, null, 0));
    AddPostMutation addPostMutation = AddPostMutation.builder().input(CreatePostInput.builder().title("Learning to Live ").author("Dream Theater @ ").url("Dream Theater Station").content("No energy for anger @" + System.currentTimeMillis()).ups(1).downs(0).build()).build();
    client.mutate(addPostMutation, addPostMutationData).enqueue(NoOpGraphQLCallback.instance());
    // Act: clear the caches.
    client.clearCaches(ClearCacheOptions.builder().clearMutations().build());
    // Check Mutation Queue
    assertTrue(client.isMutationQueueEmpty());
    // Query from cache and check data is available since only mutations queue is cleared.
    Response<AllPostsQuery.Data> cacheResponse = Posts.list(client, AppSyncResponseFetchers.CACHE_ONLY).get("CACHE");
    assertNotNull(cacheResponse);
    assertNotNull(cacheResponse.data());
}
Also used : AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) AddPostMutation(com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation) Test(org.junit.Test)

Example 20 with AWSAppSyncClient

use of com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient in project aws-mobile-appsync-sdk-android by awslabs.

the class MultiClientInstrumentationTest method testClearCache.

@Test
public void testClearCache() throws ClearCacheException {
    AWSAppSyncClient client = AWSAppSyncClients.withAPIKEYFromAWSConfiguration();
    goOffline();
    // Add a post
    AddPostMutation.Data expected = new AddPostMutation.Data(new AddPostMutation.CreatePost("Post", "", "", "", "", "", null, null, 0));
    AddPostMutation addPostMutation = AddPostMutation.builder().input(CreatePostInput.builder().title("Learning to Live ").author("Dream Theater @ ").url("Dream Theater Station").content("No energy for anger @" + System.currentTimeMillis()).ups(1).downs(0).build()).build();
    client.mutate(addPostMutation, expected).enqueue(NoOpGraphQLCallback.instance());
    assertFalse(client.isMutationQueueEmpty());
    client.clearCaches();
    // Check Mutation Queue
    assertTrue(client.isMutationQueueEmpty());
    // Query from cache and check no data is available
    Response<AllPostsQuery.Data> listPostsResponse = Posts.list(client, AppSyncResponseFetchers.CACHE_ONLY).get("CACHE");
    assertNotNull(listPostsResponse);
    assertNull(listPostsResponse.data());
}
Also used : AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) AddPostMutation(com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation) Test(org.junit.Test)

Aggregations

AWSAppSyncClient (com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient)28 Test (org.junit.Test)19 AddPostMutation (com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation)8 AWSConfiguration (com.amazonaws.mobile.config.AWSConfiguration)6 AllPostsQuery (com.amazonaws.mobileconnectors.appsync.demo.AllPostsQuery)6 Response (com.apollographql.apollo.api.Response)5 GetPostQuery (com.amazonaws.mobileconnectors.appsync.demo.GetPostQuery)4 ResponseFetcher (com.apollographql.apollo.fetcher.ResponseFetcher)4 HashMap (java.util.HashMap)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 JSONObject (org.json.JSONObject)4 DelegatingGraphQLCallback (com.amazonaws.mobileconnectors.appsync.client.DelegatingGraphQLCallback)3 LatchedGraphQLCallback (com.amazonaws.mobileconnectors.appsync.client.LatchedGraphQLCallback)3 UpdatePostMutation (com.amazonaws.mobileconnectors.appsync.demo.UpdatePostMutation)3 CreatePostInput (com.amazonaws.mobileconnectors.appsync.demo.type.CreatePostInput)3 AppSyncResponseFetchers (com.amazonaws.mobileconnectors.appsync.fetcher.AppSyncResponseFetchers)3 BasicAPIKeyAuthProvider (com.amazonaws.mobileconnectors.appsync.sigv4.BasicAPIKeyAuthProvider)3 Await (com.amazonaws.mobileconnectors.appsync.util.Await)3 Cancelable (com.apollographql.apollo.internal.util.Cancelable)3 Map (java.util.Map)3