Search in sources :

Example 61 with Response

use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.

the class NormalizedCacheTestCase method masterDetailFailIncomplete.

@Test
public void masterDetailFailIncomplete() throws Exception {
    enqueueAndAssertResponse(server, "HeroAndFriendsNameWithIdsResponse.json", apolloClient.query(new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(Episode.NEWHOPE))).responseFetcher(NETWORK_ONLY), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    assertResponse(apolloClient.query(new CharacterDetailsQuery("1002")).responseFetcher(CACHE_ONLY), new Predicate<Response<CharacterDetailsQuery.Data>>() {

        @Override
        public boolean test(Response<CharacterDetailsQuery.Data> response) throws Exception {
            assertThat(response.data()).isNull();
            return true;
        }
    });
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.cacheAndAssertCachedResponse(com.apollographql.apollo.Utils.cacheAndAssertCachedResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) CharacterDetailsQuery(com.apollographql.apollo.integration.normalizer.CharacterDetailsQuery) HeroAndFriendsNamesWithIDsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery) Test(org.junit.Test)

Example 62 with Response

use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.

the class Rx2ApolloTest method queryWatcherNotUpdatedSameQuerySameResults.

@Test
@SuppressWarnings("CheckReturnValue")
public void queryWatcherNotUpdatedSameQuerySameResults() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    TestObserver<EpisodeHeroNameQuery.Data> observer = new TestObserver<>();
    Rx2Apollo.from(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).watcher()).map(new Function<Response<EpisodeHeroNameQuery.Data>, EpisodeHeroNameQuery.Data>() {

        @Override
        public EpisodeHeroNameQuery.Data apply(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
            return response.data();
        }
    }).subscribeWith(observer);
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).responseFetcher(NETWORK_ONLY).enqueue(null);
    observer.assertValueCount(1).assertValueAt(0, new Predicate<EpisodeHeroNameQuery.Data>() {

        @Override
        public boolean test(EpisodeHeroNameQuery.Data data) throws Exception {
            assertThat(data.hero().name()).isEqualTo("R2-D2");
            return true;
        }
    });
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Response(com.apollographql.apollo.api.Response) Function(io.reactivex.functions.Function) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 63 with Response

use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.

the class Rx2ApolloTest method queryWatcherUpdatedDifferentQueryDifferentResults.

@Test
@SuppressWarnings("CheckReturnValue")
public void queryWatcherUpdatedDifferentQueryDifferentResults() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    TestObserver<EpisodeHeroNameQuery.Data> observer = new TestObserver<>();
    Rx2Apollo.from(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).watcher()).map(new Function<Response<EpisodeHeroNameQuery.Data>, EpisodeHeroNameQuery.Data>() {

        @Override
        public EpisodeHeroNameQuery.Data apply(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
            return response.data();
        }
    }).subscribeWith(observer);
    server.enqueue(mockResponse("HeroAndFriendsNameWithIdsNameChange.json"));
    apolloClient.query(new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(NEWHOPE))).enqueue(null);
    observer.assertValueCount(2).assertValueAt(0, new Predicate<EpisodeHeroNameQuery.Data>() {

        @Override
        public boolean test(EpisodeHeroNameQuery.Data data) throws Exception {
            assertThat(data.hero().name()).isEqualTo("R2-D2");
            return true;
        }
    }).assertValueAt(1, new Predicate<EpisodeHeroNameQuery.Data>() {

        @Override
        public boolean test(EpisodeHeroNameQuery.Data data) throws Exception {
            assertThat(data.hero().name()).isEqualTo("Artoo");
            return true;
        }
    });
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Response(com.apollographql.apollo.api.Response) Function(io.reactivex.functions.Function) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) HeroAndFriendsNamesWithIDsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery) TestObserver(io.reactivex.observers.TestObserver) Predicate(io.reactivex.functions.Predicate) Test(org.junit.Test)

Example 64 with Response

use of com.apollographql.apollo.api.Response 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)

Example 65 with Response

use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.

the class RxApollo method from.

/**
 * Converts an {@link ApolloCall} to a Observable. The number of emissions this Observable will have is based on the
 * {@link ResponseFetcher} used with the call.
 *
 * @param call             the ApolloCall to convert
 * @param <T>              the value type
 * @param backpressureMode The {@link rx.Emitter.BackpressureMode} to use.
 * @return the converted Observable
 */
@Nonnull
public static <T> Observable<Response<T>> from(@Nonnull final ApolloCall<T> call, Emitter.BackpressureMode backpressureMode) {
    checkNotNull(call, "call == null");
    return Observable.create(new Action1<Emitter<Response<T>>>() {

        @Override
        public void call(final Emitter<Response<T>> emitter) {
            final AtomicBoolean canceled = new AtomicBoolean();
            emitter.setCancellation(new Cancellable() {

                @Override
                public void cancel() throws Exception {
                    canceled.set(true);
                    call.cancel();
                }
            });
            call.enqueue(new ApolloCall.Callback<T>() {

                @Override
                public void onResponse(@Nonnull Response<T> response) {
                    if (!canceled.get()) {
                        emitter.onNext(response);
                    }
                }

                @Override
                public void onFailure(@Nonnull ApolloException e) {
                    Exceptions.throwIfFatal(e);
                    if (!canceled.get()) {
                        emitter.onError(e);
                    }
                }

                @Override
                public void onStatusEvent(@Nonnull ApolloCall.StatusEvent event) {
                    if (!canceled.get()) {
                        if (event == ApolloCall.StatusEvent.COMPLETED) {
                            emitter.onCompleted();
                        }
                    }
                }
            });
        }
    }, backpressureMode);
}
Also used : Response(com.apollographql.apollo.api.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Emitter(rx.Emitter) Nonnull(javax.annotation.Nonnull) ApolloException(com.apollographql.apollo.exception.ApolloException) Cancellable(rx.functions.Cancellable) Nonnull(javax.annotation.Nonnull)

Aggregations

Response (com.apollographql.apollo.api.Response)67 Test (org.junit.Test)55 ApolloException (com.apollographql.apollo.exception.ApolloException)34 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)29 Utils.assertResponse (com.apollographql.apollo.Utils.assertResponse)26 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)26 IOException (java.io.IOException)23 MockResponse (okhttp3.mockwebserver.MockResponse)22 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)18 ParseException (java.text.ParseException)17 ApolloHttpException (com.apollographql.apollo.exception.ApolloHttpException)12 AllPlanetsQuery (com.apollographql.apollo.integration.httpcache.AllPlanetsQuery)12 HeroAndFriendsNamesWithIDsQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery)11 Utils.cacheAndAssertCachedResponse (com.apollographql.apollo.Utils.cacheAndAssertCachedResponse)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 Nonnull (javax.annotation.Nonnull)7 TestObserver (io.reactivex.observers.TestObserver)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 TestSubscriber (rx.observers.TestSubscriber)5 Handler (android.os.Handler)4