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;
}
Aggregations