Search in sources :

Example 1 with ReviewsByEpisodeQuery

use of com.apollographql.apollo.integration.normalizer.ReviewsByEpisodeQuery in project apollo-android by apollographql.

the class QueryRefetchTest method refetchNoPreCachedQuery.

@Test
@SuppressWarnings("CheckReturnValue")
public void refetchNoPreCachedQuery() throws Exception {
    CreateReviewMutation mutation = new CreateReviewMutation(Episode.EMPIRE, ReviewInput.builder().stars(5).commentary("Awesome").favoriteColor(ColorInput.builder().build()).build());
    server.enqueue(mockResponse("CreateReviewResponse.json"));
    server.enqueue(mockResponse("ReviewsEmpireEpisodeResponse.json"));
    RealApolloCall call = (RealApolloCall) apolloClient.mutate(mutation).refetchQueries(new ReviewsByEpisodeQuery(Episode.EMPIRE));
    Rx2Apollo.from(call).test();
    assertThat(server.getRequestCount()).isEqualTo(2);
    assertResponse(apolloClient.query(new ReviewsByEpisodeQuery(Episode.EMPIRE)).responseFetcher(CACHE_ONLY), new Predicate<Response<ReviewsByEpisodeQuery.Data>>() {

        @Override
        public boolean test(Response<ReviewsByEpisodeQuery.Data> response) throws Exception {
            assertThat(response.data().reviews()).hasSize(3);
            assertThat(response.data().reviews().get(2).stars()).isEqualTo(5);
            assertThat(response.data().reviews().get(2).commentary()).isEqualTo("Amazing");
            return true;
        }
    });
}
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) CreateReviewMutation(com.apollographql.apollo.integration.normalizer.CreateReviewMutation) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Example 2 with ReviewsByEpisodeQuery

use of com.apollographql.apollo.integration.normalizer.ReviewsByEpisodeQuery in project apollo-android by apollographql.

the class QueryRefetchTest method refetchPreCachedQuery.

@Test
@SuppressWarnings("CheckReturnValue")
public void refetchPreCachedQuery() throws Exception {
    enqueueAndAssertResponse(server, "ReviewsEmpireEpisodeResponse.json", apolloClient.query(new ReviewsByEpisodeQuery(Episode.EMPIRE)).responseFetcher(NETWORK_FIRST), new Predicate<Response<ReviewsByEpisodeQuery.Data>>() {

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

        @Override
        public boolean test(Response<ReviewsByEpisodeQuery.Data> response) throws Exception {
            assertThat(response.data().reviews()).hasSize(3);
            assertThat(response.data().reviews().get(2).stars()).isEqualTo(5);
            assertThat(response.data().reviews().get(2).commentary()).isEqualTo("Amazing");
            return true;
        }
    });
    CreateReviewMutation mutation = new CreateReviewMutation(Episode.EMPIRE, ReviewInput.builder().stars(5).commentary("Awesome").favoriteColor(ColorInput.builder().build()).build());
    server.enqueue(mockResponse("CreateReviewResponse.json"));
    server.enqueue(mockResponse("ReviewsEmpireEpisodeResponseUpdated.json"));
    RealApolloCall call = (RealApolloCall) apolloClient.mutate(mutation).refetchQueries(new ReviewsByEpisodeQuery(Episode.EMPIRE));
    Rx2Apollo.from(call).test();
    assertThat(server.getRequestCount()).isEqualTo(3);
    assertResponse(apolloClient.query(new ReviewsByEpisodeQuery(Episode.EMPIRE)).responseFetcher(CACHE_ONLY), new Predicate<Response<ReviewsByEpisodeQuery.Data>>() {

        @Override
        public boolean test(Response<ReviewsByEpisodeQuery.Data> response) throws Exception {
            assertThat(response.data().reviews()).hasSize(4);
            assertThat(response.data().reviews().get(3).stars()).isEqualTo(5);
            assertThat(response.data().reviews().get(3).commentary()).isEqualTo("Awesome");
            return true;
        }
    });
}
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) CreateReviewMutation(com.apollographql.apollo.integration.normalizer.CreateReviewMutation) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Example 3 with ReviewsByEpisodeQuery

use of com.apollographql.apollo.integration.normalizer.ReviewsByEpisodeQuery in project apollo-android by apollographql.

the class OptimisticCacheTestCase method mutation_and_query_watcher.

@Test
public void mutation_and_query_watcher() throws Exception {
    server.enqueue(mockResponse("ReviewsEmpireEpisodeResponse.json"));
    final List<ReviewsByEpisodeQuery.Data> watcherData = new ArrayList<>();
    apolloClient.query(new ReviewsByEpisodeQuery(Episode.EMPIRE)).responseFetcher(ApolloResponseFetchers.NETWORK_FIRST).watcher().refetchResponseFetcher(ApolloResponseFetchers.CACHE_FIRST).enqueueAndWatch(new ApolloCall.Callback<ReviewsByEpisodeQuery.Data>() {

        @Override
        public void onResponse(@Nonnull Response<ReviewsByEpisodeQuery.Data> response) {
            watcherData.add(response.data());
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
        }
    });
    server.enqueue(mockResponse("UpdateReviewResponse.json"));
    UpdateReviewMutation updateReviewMutation = new UpdateReviewMutation("empireReview2", ReviewInput.builder().commentary("Great").stars(5).favoriteColor(ColorInput.builder().build()).build());
    apolloClient.mutate(updateReviewMutation, new UpdateReviewMutation.Data(new UpdateReviewMutation.UpdateReview("Review", "empireReview2", 5, "Great"))).enqueue(new ApolloCall.Callback<UpdateReviewMutation.Data>() {

        @Override
        public void onResponse(@Nonnull Response<UpdateReviewMutation.Data> response) {
        }

        @Override
        public void onFailure(@Nonnull ApolloException e) {
        }
    });
    assertThat(watcherData).hasSize(3);
    // before mutation and optimistic updates
    assertThat(watcherData.get(0).reviews()).hasSize(3);
    assertThat(watcherData.get(0).reviews().get(0).id()).isEqualTo("empireReview1");
    assertThat(watcherData.get(0).reviews().get(0).stars()).isEqualTo(1);
    assertThat(watcherData.get(0).reviews().get(0).commentary()).isEqualTo("Boring");
    assertThat(watcherData.get(0).reviews().get(1).id()).isEqualTo("empireReview2");
    assertThat(watcherData.get(0).reviews().get(1).stars()).isEqualTo(2);
    assertThat(watcherData.get(0).reviews().get(1).commentary()).isEqualTo("So-so");
    assertThat(watcherData.get(0).reviews().get(2).id()).isEqualTo("empireReview3");
    assertThat(watcherData.get(0).reviews().get(2).stars()).isEqualTo(5);
    assertThat(watcherData.get(0).reviews().get(2).commentary()).isEqualTo("Amazing");
    // optimistic updates
    assertThat(watcherData.get(1).reviews()).hasSize(3);
    assertThat(watcherData.get(1).reviews().get(0).id()).isEqualTo("empireReview1");
    assertThat(watcherData.get(1).reviews().get(0).stars()).isEqualTo(1);
    assertThat(watcherData.get(1).reviews().get(0).commentary()).isEqualTo("Boring");
    assertThat(watcherData.get(1).reviews().get(1).id()).isEqualTo("empireReview2");
    assertThat(watcherData.get(1).reviews().get(1).stars()).isEqualTo(5);
    assertThat(watcherData.get(1).reviews().get(1).commentary()).isEqualTo("Great");
    assertThat(watcherData.get(1).reviews().get(2).id()).isEqualTo("empireReview3");
    assertThat(watcherData.get(1).reviews().get(2).stars()).isEqualTo(5);
    assertThat(watcherData.get(1).reviews().get(2).commentary()).isEqualTo("Amazing");
    // after mutation with rolled back optimistic updates
    assertThat(watcherData.get(2).reviews()).hasSize(3);
    assertThat(watcherData.get(2).reviews().get(0).id()).isEqualTo("empireReview1");
    assertThat(watcherData.get(2).reviews().get(0).stars()).isEqualTo(1);
    assertThat(watcherData.get(2).reviews().get(0).commentary()).isEqualTo("Boring");
    assertThat(watcherData.get(2).reviews().get(1).id()).isEqualTo("empireReview2");
    assertThat(watcherData.get(2).reviews().get(1).stars()).isEqualTo(4);
    assertThat(watcherData.get(2).reviews().get(1).commentary()).isEqualTo("Not Bad");
    assertThat(watcherData.get(2).reviews().get(2).id()).isEqualTo("empireReview3");
    assertThat(watcherData.get(2).reviews().get(2).stars()).isEqualTo(5);
    assertThat(watcherData.get(2).reviews().get(2).commentary()).isEqualTo("Amazing");
}
Also used : ReviewsByEpisodeQuery(com.apollographql.apollo.integration.normalizer.ReviewsByEpisodeQuery) ArrayList(java.util.ArrayList) ApolloException(com.apollographql.apollo.exception.ApolloException) UpdateReviewMutation(com.apollographql.apollo.integration.normalizer.UpdateReviewMutation) Test(org.junit.Test)

Example 4 with ReviewsByEpisodeQuery

use of com.apollographql.apollo.integration.normalizer.ReviewsByEpisodeQuery 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)4 ReviewsByEpisodeQuery (com.apollographql.apollo.integration.normalizer.ReviewsByEpisodeQuery)4 Test (org.junit.Test)4 Utils.assertResponse (com.apollographql.apollo.Utils.assertResponse)3 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)3 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)3 Response (com.apollographql.apollo.api.Response)3 CreateReviewMutation (com.apollographql.apollo.integration.normalizer.CreateReviewMutation)3 IOException (java.io.IOException)2 ApolloCall (com.apollographql.apollo.ApolloCall)1 UpdateReviewMutation (com.apollographql.apollo.integration.normalizer.UpdateReviewMutation)1 ArrayList (java.util.ArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1