Search in sources :

Example 31 with ApolloException

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));
}
Also used : Response(com.apollographql.apollo.api.Response) ApolloException(com.apollographql.apollo.exception.ApolloException) Record(com.apollographql.apollo.cache.normalized.Record)

Example 32 with ApolloException

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");
    }
}
Also used : CallBack(com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack) Nonnull(javax.annotation.Nonnull) RealApolloInterceptorChain(com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain) ApolloInterceptorChain(com.apollographql.apollo.interceptor.ApolloInterceptorChain) RealApolloInterceptorChain(com.apollographql.apollo.internal.interceptor.RealApolloInterceptorChain) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) Executor(java.util.concurrent.Executor) InterceptorResponse(com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ApolloException(com.apollographql.apollo.exception.ApolloException) ApolloInterceptor(com.apollographql.apollo.interceptor.ApolloInterceptor) Test(org.junit.Test)

Example 33 with ApolloException

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);
}
Also used : Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ApolloException(com.apollographql.apollo.exception.ApolloException) Nonnull(javax.annotation.Nonnull) Handler(android.os.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ApolloNetworkException(com.apollographql.apollo.exception.ApolloNetworkException) Test(org.junit.Test)

Example 34 with ApolloException

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Nonnull(javax.annotation.Nonnull) ApolloException(com.apollographql.apollo.exception.ApolloException) Handler(android.os.Handler) CountDownLatch(java.util.concurrent.CountDownLatch) ApolloNetworkException(com.apollographql.apollo.exception.ApolloNetworkException) Test(org.junit.Test)

Example 35 with ApolloException

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();
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) ReviewsByEpisodeQuery(com.apollographql.apollo.integration.normalizer.ReviewsByEpisodeQuery) ApolloException(com.apollographql.apollo.exception.ApolloException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CreateReviewMutation(com.apollographql.apollo.integration.normalizer.CreateReviewMutation) ApolloCall(com.apollographql.apollo.ApolloCall) Test(org.junit.Test)

Aggregations

ApolloException (com.apollographql.apollo.exception.ApolloException)37 Test (org.junit.Test)31 Response (com.apollographql.apollo.api.Response)20 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)16 Nonnull (javax.annotation.Nonnull)15 MockResponse (okhttp3.mockwebserver.MockResponse)12 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 Handler (android.os.Handler)7 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)6 ApolloHttpException (com.apollographql.apollo.exception.ApolloHttpException)6 ApolloInterceptor (com.apollographql.apollo.interceptor.ApolloInterceptor)6 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)5 AllPlanetsQuery (com.apollographql.apollo.integration.httpcache.AllPlanetsQuery)5 InterceptorResponse (com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse)5 ApolloInterceptorChain (com.apollographql.apollo.interceptor.ApolloInterceptorChain)5 Executor (java.util.concurrent.Executor)5 TimeoutException (java.util.concurrent.TimeoutException)5