use of com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery in project apollo-android by apollographql.
the class ApolloWatcherTest method testQueryWatcherNotUpdated_DifferentQueries.
@Test
public void testQueryWatcherNotUpdated_DifferentQueries() throws Exception {
final List<String> heroNameList = new ArrayList<>();
server.enqueue(mockResponse("EpisodeHeroNameResponseWithId.json"));
EpisodeHeroNameQuery query = EpisodeHeroNameQuery.builder().episode(Episode.EMPIRE).build();
ApolloQueryWatcher<EpisodeHeroNameQuery.Data> watcher = apolloClient.query(query).watcher();
watcher.enqueueAndWatch(new ApolloCall.Callback<EpisodeHeroNameQuery.Data>() {
@Override
public void onResponse(@Nonnull Response<EpisodeHeroNameQuery.Data> response) {
heroNameList.add(response.data().hero().name());
}
@Override
public void onFailure(@Nonnull ApolloException e) {
Assert.fail(e.getMessage());
}
});
HeroAndFriendsNamesWithIDsQuery friendsQuery = HeroAndFriendsNamesWithIDsQuery.builder().episode(Episode.NEWHOPE).build();
server.enqueue(mockResponse("HeroAndFriendsNameWithIdsResponse.json"));
apolloClient.query(friendsQuery).responseFetcher(NETWORK_ONLY).enqueue(null);
watcher.cancel();
assertThat(heroNameList.get(0)).isEqualTo("R2-D2");
assertThat(heroNameList.size()).isEqualTo(1);
}
use of com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery in project apollo-android by apollographql.
the class ApolloWatcherTest method testQueryWatcherUpdated_DifferentQuery_DifferentResults.
@Test
public void testQueryWatcherUpdated_DifferentQuery_DifferentResults() throws Exception {
final List<String> heroNameList = new ArrayList<>();
server.enqueue(mockResponse("EpisodeHeroNameResponseWithId.json"));
EpisodeHeroNameQuery query = EpisodeHeroNameQuery.builder().episode(Episode.EMPIRE).build();
ApolloQueryWatcher<EpisodeHeroNameQuery.Data> watcher = apolloClient.query(query).watcher();
watcher.enqueueAndWatch(new ApolloCall.Callback<EpisodeHeroNameQuery.Data>() {
@Override
public void onResponse(@Nonnull Response<EpisodeHeroNameQuery.Data> response) {
heroNameList.add(response.data().hero().name());
}
@Override
public void onFailure(@Nonnull ApolloException e) {
Assert.fail(e.getMessage());
}
});
HeroAndFriendsNamesWithIDsQuery friendsQuery = HeroAndFriendsNamesWithIDsQuery.builder().episode(Episode.NEWHOPE).build();
enqueueAndAssertResponse(server, "HeroAndFriendsNameWithIdsNameChange.json", apolloClient.query(friendsQuery).responseFetcher(NETWORK_ONLY), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {
@Override
public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
return !response.hasErrors();
}
});
watcher.cancel();
assertThat(heroNameList.get(0)).isEqualTo("R2-D2");
assertThat(heroNameList.get(1)).isEqualTo("Artoo");
}
use of com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery in project apollo-android by apollographql.
the class OptimisticCacheTestCase method two_optimistic_reverse_rollback_order.
@Test
public void two_optimistic_reverse_rollback_order() throws Exception {
HeroAndFriendsNamesWithIDsQuery query1 = new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(Episode.JEDI));
UUID mutationId1 = UUID.randomUUID();
HeroNameWithIdQuery query2 = new HeroNameWithIdQuery();
UUID mutationId2 = UUID.randomUUID();
enqueueAndAssertResponse(server, "HeroAndFriendsNameWithIdsResponse.json", apolloClient.query(query1), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {
@Override
public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
return !response.hasErrors();
}
});
enqueueAndAssertResponse(server, "HeroNameWithIdResponse.json", apolloClient.query(query2), new Predicate<Response<HeroNameWithIdQuery.Data>>() {
@Override
public boolean test(Response<HeroNameWithIdQuery.Data> response) throws Exception {
return !response.hasErrors();
}
});
HeroAndFriendsNamesWithIDsQuery.Data data1 = new HeroAndFriendsNamesWithIDsQuery.Data(new HeroAndFriendsNamesWithIDsQuery.Hero("Droid", "2001", "R222-D222", Arrays.asList(new HeroAndFriendsNamesWithIDsQuery.Friend("Human", "1000", "Robocop"), new HeroAndFriendsNamesWithIDsQuery.Friend("Human", "1003", "Batman"))));
apolloClient.apolloStore().writeOptimisticUpdatesAndPublish(query1, data1, mutationId1).execute();
HeroNameWithIdQuery.Data data2 = new HeroNameWithIdQuery.Data(new HeroNameWithIdQuery.Hero("Human", "1000", "Spiderman"));
apolloClient.apolloStore().writeOptimisticUpdatesAndPublish(query2, data2, mutationId2).execute();
// check if query1 see optimistic updates
assertResponse(apolloClient.query(query1).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {
@Override
public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
assertThat(response.data().hero().id()).isEqualTo("2001");
assertThat(response.data().hero().name()).isEqualTo("R222-D222");
assertThat(response.data().hero().friends()).hasSize(2);
assertThat(response.data().hero().friends().get(0).id()).isEqualTo("1000");
assertThat(response.data().hero().friends().get(0).name()).isEqualTo("Spiderman");
assertThat(response.data().hero().friends().get(1).id()).isEqualTo("1003");
assertThat(response.data().hero().friends().get(1).name()).isEqualTo("Batman");
return true;
}
});
// check if query2 see the latest optimistic updates
assertResponse(apolloClient.query(query2).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroNameWithIdQuery.Data>>() {
@Override
public boolean test(Response<HeroNameWithIdQuery.Data> response) throws Exception {
assertThat(response.data().hero().id()).isEqualTo("1000");
assertThat(response.data().hero().name()).isEqualTo("Spiderman");
return true;
}
});
// rollback query2 optimistic updates
apolloClient.apolloStore().rollbackOptimisticUpdates(mutationId2).execute();
// check if query1 see the latest optimistic updates
assertResponse(apolloClient.query(query1).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {
@Override
public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
assertThat(response.data().hero().id()).isEqualTo("2001");
assertThat(response.data().hero().name()).isEqualTo("R222-D222");
assertThat(response.data().hero().friends()).hasSize(2);
assertThat(response.data().hero().friends().get(0).id()).isEqualTo("1000");
assertThat(response.data().hero().friends().get(0).name()).isEqualTo("Robocop");
assertThat(response.data().hero().friends().get(1).id()).isEqualTo("1003");
assertThat(response.data().hero().friends().get(1).name()).isEqualTo("Batman");
return true;
}
});
// check if query2 see the latest optimistic updates
assertResponse(apolloClient.query(query2).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroNameWithIdQuery.Data>>() {
@Override
public boolean test(Response<HeroNameWithIdQuery.Data> response) throws Exception {
assertThat(response.data().hero().id()).isEqualTo("1000");
assertThat(response.data().hero().name()).isEqualTo("Robocop");
return true;
}
});
// rollback query1 optimistic updates
apolloClient.apolloStore().rollbackOptimisticUpdates(mutationId1).execute();
// check if query1 see the latest non-optimistic updates
assertResponse(apolloClient.query(query1).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {
@Override
public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
assertThat(response.data().hero().id()).isEqualTo("2001");
assertThat(response.data().hero().name()).isEqualTo("R2-D2");
assertThat(response.data().hero().friends()).hasSize(3);
assertThat(response.data().hero().friends().get(0).id()).isEqualTo("1000");
assertThat(response.data().hero().friends().get(0).name()).isEqualTo("SuperMan");
assertThat(response.data().hero().friends().get(1).id()).isEqualTo("1002");
assertThat(response.data().hero().friends().get(1).name()).isEqualTo("Han Solo");
assertThat(response.data().hero().friends().get(2).id()).isEqualTo("1003");
assertThat(response.data().hero().friends().get(2).name()).isEqualTo("Leia Organa");
return true;
}
});
// check if query2 see the latest non-optimistic updates
assertResponse(apolloClient.query(query2).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroNameWithIdQuery.Data>>() {
@Override
public boolean test(Response<HeroNameWithIdQuery.Data> response) throws Exception {
assertThat(response.data().hero().id()).isEqualTo("1000");
assertThat(response.data().hero().name()).isEqualTo("SuperMan");
return true;
}
});
}
use of com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery in project apollo-android by apollographql.
the class ResponseWriteTestCase method objects.
@Test
public void objects() throws Exception {
HeroAndFriendsNamesWithIDsQuery query = new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(JEDI));
enqueueAndAssertResponse(server, "HeroAndFriendsNameWithIdsResponse.json", apolloClient.query(query), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {
@Override
public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
assertThat(response.data().hero().__typename()).isEqualTo("Droid");
assertThat(response.data().hero().name()).isEqualTo("R2-D2");
assertThat(response.data().hero().id()).isEqualTo("2001");
assertThat(response.data().hero().friends()).hasSize(3);
assertThat(response.data().hero().friends().get(0).__typename()).isEqualTo("Human");
assertThat(response.data().hero().friends().get(0).id()).isEqualTo("1000");
assertThat(response.data().hero().friends().get(0).name()).isEqualTo("Luke Skywalker");
assertThat(response.data().hero().friends().get(1).__typename()).isEqualTo("Human");
assertThat(response.data().hero().friends().get(1).id()).isEqualTo("1002");
assertThat(response.data().hero().friends().get(1).name()).isEqualTo("Han Solo");
assertThat(response.data().hero().friends().get(2).__typename()).isEqualTo("Human");
assertThat(response.data().hero().friends().get(2).id()).isEqualTo("1003");
assertThat(response.data().hero().friends().get(2).name()).isEqualTo("Leia Organa");
return true;
}
});
HeroAndFriendsNamesWithIDsQuery.Hero hero = new HeroAndFriendsNamesWithIDsQuery.Hero("Droid", "2001", "R222-D222", null);
apolloClient.apolloStore().write(query, new HeroAndFriendsNamesWithIDsQuery.Data(hero)).execute();
assertCachedQueryResponse(query, new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {
@Override
public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
assertThat(response.data().hero().__typename()).isEqualTo("Droid");
assertThat(response.data().hero().name()).isEqualTo("R222-D222");
assertThat(response.data().hero().id()).isEqualTo("2001");
assertThat(response.data().hero().friends()).isNull();
return true;
}
});
HeroAndFriendsNamesWithIDsQuery.Friend friend = new HeroAndFriendsNamesWithIDsQuery.Friend("Human", "1002", "Han Soloooo");
hero = new HeroAndFriendsNamesWithIDsQuery.Hero(hero.__typename(), hero.id(), "R222-D222", singletonList(friend));
apolloClient.apolloStore().write(query, new HeroAndFriendsNamesWithIDsQuery.Data(hero)).execute();
assertCachedQueryResponse(query, new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {
@Override
public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
assertThat(response.data().hero().__typename()).isEqualTo("Droid");
assertThat(response.data().hero().name()).isEqualTo("R222-D222");
assertThat(response.data().hero().id()).isEqualTo("2001");
assertThat(response.data().hero().friends()).hasSize(1);
assertThat(response.data().hero().friends().get(0).__typename()).isEqualTo("Human");
assertThat(response.data().hero().friends().get(0).id()).isEqualTo("1002");
assertThat(response.data().hero().friends().get(0).name()).isEqualTo("Han Soloooo");
return true;
}
});
}
use of com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery in project apollo-android by apollographql.
the class NormalizedCacheTestCase method readFragmentFromStore.
@Test
public void readFragmentFromStore() throws Exception {
enqueueAndAssertResponse(server, "HeroAndFriendsWithFragmentResponse.json", apolloClient.query(new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(Episode.NEWHOPE))), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {
@Override
public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
return !response.hasErrors();
}
});
HeroWithFriendsFragment heroWithFriendsFragment = apolloClient.apolloStore().read(new HeroWithFriendsFragment.Mapper(), CacheKey.from("2001"), Operation.EMPTY_VARIABLES).execute();
assertThat(heroWithFriendsFragment.id()).isEqualTo("2001");
assertThat(heroWithFriendsFragment.name()).isEqualTo("R2-D2");
assertThat(heroWithFriendsFragment.friends()).hasSize(3);
assertThat(heroWithFriendsFragment.friends().get(0).fragments().humanWithIdFragment().id()).isEqualTo("1000");
assertThat(heroWithFriendsFragment.friends().get(0).fragments().humanWithIdFragment().name()).isEqualTo("Luke Skywalker");
assertThat(heroWithFriendsFragment.friends().get(1).fragments().humanWithIdFragment().id()).isEqualTo("1002");
assertThat(heroWithFriendsFragment.friends().get(1).fragments().humanWithIdFragment().name()).isEqualTo("Han Solo");
assertThat(heroWithFriendsFragment.friends().get(2).fragments().humanWithIdFragment().id()).isEqualTo("1003");
assertThat(heroWithFriendsFragment.friends().get(2).fragments().humanWithIdFragment().name()).isEqualTo("Leia Organa");
HumanWithIdFragment fragment = apolloClient.apolloStore().read(new HumanWithIdFragment.Mapper(), CacheKey.from("1000"), Operation.EMPTY_VARIABLES).execute();
assertThat(fragment.id()).isEqualTo("1000");
assertThat(fragment.name()).isEqualTo("Luke Skywalker");
fragment = apolloClient.apolloStore().read(new HumanWithIdFragment.Mapper(), CacheKey.from("1002"), Operation.EMPTY_VARIABLES).execute();
assertThat(fragment.id()).isEqualTo("1002");
assertThat(fragment.name()).isEqualTo("Han Solo");
fragment = apolloClient.apolloStore().read(new HumanWithIdFragment.Mapper(), CacheKey.from("1003"), Operation.EMPTY_VARIABLES).execute();
assertThat(fragment.id()).isEqualTo("1003");
assertThat(fragment.name()).isEqualTo("Leia Organa");
}
Aggregations