use of com.apollographql.apollo.integration.normalizer.UpdateReviewMutation 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");
}
Aggregations