use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class ApolloCacheInterceptor method resolveFromCache.
InterceptorResponse resolveFromCache(InterceptorRequest request) throws ApolloException {
ResponseNormalizer<Record> responseNormalizer = apolloStore.cacheResponseNormalizer();
// noinspection unchecked
ApolloStoreOperation<Response> apolloStoreOperation = apolloStore.read(request.operation, responseFieldMapper, responseNormalizer, request.cacheHeaders);
Response cachedResponse = apolloStoreOperation.execute();
if (cachedResponse.data() != null) {
logger.d("Cache HIT for operation %s", request.operation);
return new InterceptorResponse(null, cachedResponse, responseNormalizer.records());
}
logger.d("Cache MISS for operation %s", request.operation);
throw new ApolloException(String.format("Cache miss for operation %s", request.operation));
}
use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class ApolloInterceptorChainTest method onProceedAsyncCalled_chainPassesControlToInterceptor.
@Test
public void onProceedAsyncCalled_chainPassesControlToInterceptor() throws TimeoutException, InterruptedException {
final AtomicInteger counter = new AtomicInteger(1);
EpisodeHeroNameQuery query = createQuery();
ApolloInterceptor interceptor = new ApolloInterceptor() {
@Override
public void interceptAsync(@Nonnull InterceptorRequest request, @Nonnull ApolloInterceptorChain chain, @Nonnull Executor dispatcher, @Nonnull CallBack callBack) {
counter.decrementAndGet();
}
@Override
public void dispose() {
}
};
List<ApolloInterceptor> interceptors = Collections.singletonList(interceptor);
RealApolloInterceptorChain chain = new RealApolloInterceptorChain(interceptors);
chain.proceedAsync(ApolloInterceptor.InterceptorRequest.builder(query).fetchFromCache(false).build(), Utils.immediateExecutor(), new CallBack() {
@Override
public void onResponse(@Nonnull InterceptorResponse response) {
}
@Override
public void onFailure(@Nonnull ApolloException e) {
}
@Override
public void onCompleted() {
}
@Override
public void onFetch(ApolloInterceptor.FetchSourceType sourceType) {
}
});
// which means the test should fail.
if (counter.get() != 0) {
Assert.fail("Control not passed to the interceptor");
}
}
use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class ApolloCallbackTest method onNetworkError.
@Test
public void onNetworkError() throws Exception {
final CountDownLatch countDownLatch = new CountDownLatch(1);
final AtomicBoolean invoked = new AtomicBoolean();
final Handler callbackHandler = mockCallbackHandler(invoked);
final AtomicReference<ApolloException> exceptionRef = new AtomicReference<>();
apolloClient.query(EMPTY_QUERY).enqueue(ApolloCallback.wrap(new ApolloCall.Callback() {
@Override
public void onResponse(@Nonnull Response response) {
countDownLatch.countDown();
}
@Override
public void onFailure(@Nonnull ApolloException e) {
countDownLatch.countDown();
}
@Override
public void onNetworkError(@Nonnull ApolloNetworkException e) {
exceptionRef.set(e);
countDownLatch.countDown();
}
}, callbackHandler));
countDownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
assertThat(invoked.get()).isTrue();
assertThat(exceptionRef.get()).isInstanceOf(ApolloNetworkException.class);
}
use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class ApolloPrefetchCallbackTest method onNetworkError.
@Test
public void onNetworkError() throws Exception {
final CountDownLatch countDownLatch = new CountDownLatch(1);
final AtomicBoolean invoked = new AtomicBoolean();
final Handler callbackHandler = mockCallbackHandler(invoked);
apolloClient.prefetch(EMPTY_QUERY).enqueue(ApolloPrefetchCallback.wrap(new ApolloPrefetch.Callback() {
@Override
public void onSuccess() {
fail("Expected onNetworkError");
}
@Override
public void onFailure(@Nonnull ApolloException e) {
fail("Expected onNetworkError");
}
@Override
public void onNetworkError(@Nonnull ApolloNetworkException e) {
countDownLatch.countDown();
}
}, callbackHandler));
countDownLatch.await(2, TimeUnit.SECONDS);
assertThat(invoked.get()).isTrue();
}
use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class QueryRefetchTest method refetchWatchers.
@Test
@SuppressWarnings("CheckReturnValue")
public void refetchWatchers() throws Exception {
server.enqueue(mockResponse("ReviewsEmpireEpisodeResponse.json"));
server.enqueue(mockResponse("CreateReviewResponse.json"));
server.enqueue(mockResponse("ReviewsEmpireEpisodeResponseUpdated.json"));
final AtomicReference<Response<ReviewsByEpisodeQuery.Data>> empireReviewsWatchResponse = new AtomicReference<>();
ApolloQueryWatcher<ReviewsByEpisodeQuery.Data> queryWatcher = apolloClient.query(new ReviewsByEpisodeQuery(Episode.EMPIRE)).watcher().refetchResponseFetcher(NETWORK_FIRST).enqueueAndWatch(new ApolloCall.Callback<ReviewsByEpisodeQuery.Data>() {
@Override
public void onResponse(@Nonnull Response<ReviewsByEpisodeQuery.Data> response) {
empireReviewsWatchResponse.set(response);
}
@Override
public void onFailure(@Nonnull ApolloException e) {
}
});
CreateReviewMutation mutation = new CreateReviewMutation(Episode.EMPIRE, ReviewInput.builder().stars(5).commentary("Awesome").favoriteColor(ColorInput.builder().build()).build());
Rx2Apollo.from(apolloClient.mutate(mutation).refetchQueries(queryWatcher.operation().name())).test();
assertThat(server.getRequestCount()).isEqualTo(3);
Response<ReviewsByEpisodeQuery.Data> empireReviewsQueryResponse = empireReviewsWatchResponse.get();
assertThat(empireReviewsQueryResponse.data().reviews()).hasSize(4);
assertThat(empireReviewsQueryResponse.data().reviews().get(3).stars()).isEqualTo(5);
assertThat(empireReviewsQueryResponse.data().reviews().get(3).commentary()).isEqualTo("Awesome");
queryWatcher.cancel();
}
Aggregations