Search in sources :

Example 1 with ResponseFetcher

use of com.apollographql.apollo.fetcher.ResponseFetcher in project aws-mobile-appsync-sdk-android by awslabs.

the class MultiClientInstrumentationTest method testQueryPostsWithAllResponseFetchersAndMultipleClients.

@Test
public void testQueryPostsWithAllResponseFetchersAndMultipleClients() {
    final ResponseFetcher[] appSyncResponseFetchers = new ResponseFetcher[] { AppSyncResponseFetchers.NETWORK_FIRST, AppSyncResponseFetchers.NETWORK_ONLY, AppSyncResponseFetchers.CACHE_FIRST };
    for (ResponseFetcher appSyncResponseFetcher : appSyncResponseFetchers) {
        Log.d(TAG, "Response Fetcher: " + appSyncResponseFetcher.toString());
        Map<String, AWSAppSyncClient> appSyncClientMap = new HashMap<>();
        appSyncClientMap.put("AMAZON_COGNITO_USER_POOLS", AWSAppSyncClients.withUserPoolsFromAWSConfiguration(appSyncResponseFetcher));
        appSyncClientMap.put("AMAZON_COGNITO_USER_POOLS_2", AWSAppSyncClients.withUserPools2FromAWSConfiguration(idToken, appSyncResponseFetcher));
        for (final String clientName : appSyncClientMap.keySet()) {
            // List Posts
            final AWSAppSyncClient awsAppSyncClient = appSyncClientMap.get(clientName);
            Log.d(TAG, "AWSAppSyncClient for clientName: " + clientName + "; client: " + awsAppSyncClient);
            assertNotNull(awsAppSyncClient);
            Map<String, Response<AllPostsQuery.Data>> listPostsResponses = Posts.list(awsAppSyncClient, appSyncResponseFetcher);
            Response<AllPostsQuery.Data> allPostsResponse = listPostsResponses.get("CACHE") != null ? listPostsResponses.get("CACHE") : listPostsResponses.get("NETWORK");
            assertNotNull(allPostsResponse);
            assertNotNull(allPostsResponse.data());
            AllPostsQuery.ListPosts listPosts = allPostsResponse.data().listPosts();
            assertNotNull(listPosts);
            List<AllPostsQuery.Item> items = listPosts.items();
            assertNotNull(items);
            // Loop over the list and query each post
            String postID;
            for (AllPostsQuery.Item item : items) {
                postID = item.id();
                Map<String, Response<GetPostQuery.Data>> getPostResponses = Posts.query(awsAppSyncClient, appSyncResponseFetcher, postID);
                if (clientName.equals("API_KEY") || clientName.equals("AWS_IAM")) {
                    assertResponseForQueryPost(appSyncResponseFetcher, getPostResponses, postID);
                } else if (clientName.startsWith("AMAZON_COGNITO_USER_POOLS")) {
                    assertResponseForQueryPostWithUserPools(appSyncResponseFetcher, getPostResponses, postID);
                }
            }
        }
        // Clear the cache
        for (final String clientName : appSyncClientMap.keySet()) {
            final AWSAppSyncClient awsAppSyncClient = appSyncClientMap.get(clientName);
            assertNotNull(awsAppSyncClient);
            try {
                awsAppSyncClient.clearCaches();
            } catch (AWSAppSyncClientException e) {
                fail("Error in clearing the cache." + e);
            }
        }
    }
}
Also used : AWSAppSyncClientException(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClientException) AllPostsQuery(com.amazonaws.mobileconnectors.appsync.demo.AllPostsQuery) GetPostQuery(com.amazonaws.mobileconnectors.appsync.demo.GetPostQuery) HashMap(java.util.HashMap) ResponseFetcher(com.apollographql.apollo.fetcher.ResponseFetcher) Response(com.apollographql.apollo.api.Response) AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) Test(org.junit.Test)

Example 2 with ResponseFetcher

use of com.apollographql.apollo.fetcher.ResponseFetcher in project aws-mobile-appsync-sdk-android by awslabs.

the class ComplexObjectsInstrumentationTests method listArticles.

Map<String, Response<AllArticlesQuery.Data>> listArticles(AWSAppSyncClient awsAppSyncClient, ResponseFetcher responseFetcher) {
    final CountDownLatch cacheLatch = new CountDownLatch(1);
    final CountDownLatch networkLatch = new CountDownLatch(1);
    final Map<String, Response<AllArticlesQuery.Data>> responses = new HashMap<>();
    awsAppSyncClient.query(AllArticlesQuery.builder().build()).responseFetcher(responseFetcher).enqueue(DelegatingGraphQLCallback.to(response -> {
        if (response.fromCache()) {
            responses.put("CACHE", response);
            cacheLatch.countDown();
        } else {
            responses.put("NETWORK", response);
            networkLatch.countDown();
        }
    }, failure -> {
        cacheLatch.countDown();
        networkLatch.countDown();
    }));
    if (AppSyncResponseFetchers.NETWORK_ONLY.equals(responseFetcher) || AppSyncResponseFetchers.CACHE_AND_NETWORK.equals(responseFetcher)) {
        Await.latch(networkLatch);
        assertNotNull(responses.get("NETWORK"));
    }
    if (AppSyncResponseFetchers.CACHE_ONLY.equals(responseFetcher) || AppSyncResponseFetchers.CACHE_AND_NETWORK.equals(responseFetcher)) {
        Await.latch(cacheLatch);
        assertNotNull(responses.get("CACHE"));
    }
    return responses;
}
Also used : Response(com.apollographql.apollo.api.Response) AndroidJUnit4(androidx.test.runner.AndroidJUnit4) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) AppSyncResponseFetchers(com.amazonaws.mobileconnectors.appsync.fetcher.AppSyncResponseFetchers) CustomCognitoUserPool(com.amazonaws.mobileconnectors.appsync.identity.CustomCognitoUserPool) HashMap(java.util.HashMap) CreateArticleMutation(com.amazonaws.mobileconnectors.appsync.demo.CreateArticleMutation) AllArticlesQuery(com.amazonaws.mobileconnectors.appsync.demo.AllArticlesQuery) UpdateArticleInput(com.amazonaws.mobileconnectors.appsync.demo.type.UpdateArticleInput) DelegatingGraphQLCallback(com.amazonaws.mobileconnectors.appsync.client.DelegatingGraphQLCallback) Map(java.util.Map) DataFile(com.amazonaws.mobileconnectors.appsync.util.DataFile) AWSAppSyncClients(com.amazonaws.mobileconnectors.appsync.client.AWSAppSyncClients) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) Await(com.amazonaws.mobileconnectors.appsync.util.Await) LatchedGraphQLCallback(com.amazonaws.mobileconnectors.appsync.client.LatchedGraphQLCallback) CountDownLatch(java.util.concurrent.CountDownLatch) UpdateArticleMutation(com.amazonaws.mobileconnectors.appsync.demo.UpdateArticleMutation) Response(com.apollographql.apollo.api.Response) ResponseFetcher(com.apollographql.apollo.fetcher.ResponseFetcher) S3ObjectInput(com.amazonaws.mobileconnectors.appsync.demo.type.S3ObjectInput) AWSAppSyncClient(com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient) CreateArticleInput(com.amazonaws.mobileconnectors.appsync.demo.type.CreateArticleInput) InternetConnectivity.goOnline(com.amazonaws.mobileconnectors.appsync.util.InternetConnectivity.goOnline) Assert.assertEquals(org.junit.Assert.assertEquals) HashMap(java.util.HashMap) AllArticlesQuery(com.amazonaws.mobileconnectors.appsync.demo.AllArticlesQuery) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with ResponseFetcher

use of com.apollographql.apollo.fetcher.ResponseFetcher 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 4 with ResponseFetcher

use of com.apollographql.apollo.fetcher.ResponseFetcher in project aws-mobile-appsync-sdk-android by awslabs.

the class Posts method list.

public static Map<String, Response<AllPostsQuery.Data>> list(AWSAppSyncClient client, ResponseFetcher responseFetcher) {
    int expectedResponseCount = responseFetcher.equals(AppSyncResponseFetchers.CACHE_AND_NETWORK) ? 2 : 1;
    CountDownLatch queryCountDownLatch = new CountDownLatch(expectedResponseCount);
    Log.d(TAG, "Calling Query listPosts with responseFetcher: " + responseFetcher.toString());
    Map<String, Response<AllPostsQuery.Data>> listPostsQueryResponses = new HashMap<>();
    client.query(AllPostsQuery.builder().build()).responseFetcher(responseFetcher).enqueue(DelegatingGraphQLCallback.to(response -> {
        if (response.fromCache()) {
            listPostsQueryResponses.put("CACHE", response);
        } else {
            listPostsQueryResponses.put("NETWORK", response);
        }
        queryCountDownLatch.countDown();
    }, failure -> {
        failure.printStackTrace();
        queryCountDownLatch.countDown();
    }));
    Await.latch(queryCountDownLatch);
    if (responseFetcher.equals(AppSyncResponseFetchers.CACHE_AND_NETWORK)) {
        assertNotNull(listPostsQueryResponses.get("CACHE"));
        assertNotNull(listPostsQueryResponses.get("NETWORK"));
        assertEquals(2, listPostsQueryResponses.size());
    } else {
        assertEquals(1, listPostsQueryResponses.size());
    }
    return listPostsQueryResponses;
}
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) AllPostsQuery(com.amazonaws.mobileconnectors.appsync.demo.AllPostsQuery) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

AWSAppSyncClient (com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient)4 Response (com.apollographql.apollo.api.Response)4 ResponseFetcher (com.apollographql.apollo.fetcher.ResponseFetcher)4 HashMap (java.util.HashMap)4 DelegatingGraphQLCallback (com.amazonaws.mobileconnectors.appsync.client.DelegatingGraphQLCallback)3 LatchedGraphQLCallback (com.amazonaws.mobileconnectors.appsync.client.LatchedGraphQLCallback)3 AllPostsQuery (com.amazonaws.mobileconnectors.appsync.demo.AllPostsQuery)3 GetPostQuery (com.amazonaws.mobileconnectors.appsync.demo.GetPostQuery)3 AppSyncResponseFetchers (com.amazonaws.mobileconnectors.appsync.fetcher.AppSyncResponseFetchers)3 Await (com.amazonaws.mobileconnectors.appsync.util.Await)3 Map (java.util.Map)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 Assert.assertNotNull (org.junit.Assert.assertNotNull)3 Log (android.util.Log)2 AppSyncMutationCall (com.amazonaws.mobileconnectors.appsync.AppSyncMutationCall)2 AddPostMissingRequiredFieldsMutation (com.amazonaws.mobileconnectors.appsync.demo.AddPostMissingRequiredFieldsMutation)2 AddPostMutation (com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation)2 AddPostRequiredFieldsOnlyMutation (com.amazonaws.mobileconnectors.appsync.demo.AddPostRequiredFieldsOnlyMutation)2 DeletePostMutation (com.amazonaws.mobileconnectors.appsync.demo.DeletePostMutation)2