Search in sources :

Example 1 with CacheAndNetworkFetcher

use of com.apollographql.apollo.internal.fetcher.CacheAndNetworkFetcher 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)

Aggregations

Log (android.util.Log)1 AWSAppSyncClient (com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient)1 AppSyncMutationCall (com.amazonaws.mobileconnectors.appsync.AppSyncMutationCall)1 DelegatingGraphQLCallback (com.amazonaws.mobileconnectors.appsync.client.DelegatingGraphQLCallback)1 LatchedGraphQLCallback (com.amazonaws.mobileconnectors.appsync.client.LatchedGraphQLCallback)1 AddPostMissingRequiredFieldsMutation (com.amazonaws.mobileconnectors.appsync.demo.AddPostMissingRequiredFieldsMutation)1 AddPostMutation (com.amazonaws.mobileconnectors.appsync.demo.AddPostMutation)1 AddPostRequiredFieldsOnlyMutation (com.amazonaws.mobileconnectors.appsync.demo.AddPostRequiredFieldsOnlyMutation)1 AllPostsQuery (com.amazonaws.mobileconnectors.appsync.demo.AllPostsQuery)1 DeletePostMutation (com.amazonaws.mobileconnectors.appsync.demo.DeletePostMutation)1 GetPostQuery (com.amazonaws.mobileconnectors.appsync.demo.GetPostQuery)1 UpdatePostMutation (com.amazonaws.mobileconnectors.appsync.demo.UpdatePostMutation)1 CreatePostInput (com.amazonaws.mobileconnectors.appsync.demo.type.CreatePostInput)1 DeletePostInput (com.amazonaws.mobileconnectors.appsync.demo.type.DeletePostInput)1 UpdatePostInput (com.amazonaws.mobileconnectors.appsync.demo.type.UpdatePostInput)1 AppSyncResponseFetchers (com.amazonaws.mobileconnectors.appsync.fetcher.AppSyncResponseFetchers)1 Await (com.amazonaws.mobileconnectors.appsync.util.Await)1 Sleep (com.amazonaws.mobileconnectors.appsync.util.Sleep)1 Response (com.apollographql.apollo.api.Response)1 ResponseFetcher (com.apollographql.apollo.fetcher.ResponseFetcher)1