Search in sources :

Example 1 with HeroAndFriendsNamesWithIDsQuery

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);
}
Also used : EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) ApolloException(com.apollographql.apollo.exception.ApolloException) HeroAndFriendsNamesWithIDsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with HeroAndFriendsNamesWithIDsQuery

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");
}
Also used : ArrayList(java.util.ArrayList) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Response(com.apollographql.apollo.api.Response) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) ApolloException(com.apollographql.apollo.exception.ApolloException) HeroAndFriendsNamesWithIDsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery) Test(org.junit.Test)

Example 3 with HeroAndFriendsNamesWithIDsQuery

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;
        }
    });
}
Also used : IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) HeroNameWithIdQuery(com.apollographql.apollo.integration.normalizer.HeroNameWithIdQuery) Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) HeroAndFriendsNamesWithIDsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery) UUID(java.util.UUID) Test(org.junit.Test)

Example 4 with HeroAndFriendsNamesWithIDsQuery

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;
        }
    });
}
Also used : Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) HeroAndFriendsNamesWithIDsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 5 with HeroAndFriendsNamesWithIDsQuery

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");
}
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) HeroAndFriendsNamesWithIDsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery) HeroWithFriendsFragment(com.apollographql.apollo.integration.normalizer.fragment.HeroWithFriendsFragment) HumanWithIdFragment(com.apollographql.apollo.integration.normalizer.fragment.HumanWithIdFragment) Test(org.junit.Test)

Aggregations

HeroAndFriendsNamesWithIDsQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery)13 Test (org.junit.Test)13 Response (com.apollographql.apollo.api.Response)11 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)10 Utils.assertResponse (com.apollographql.apollo.Utils.assertResponse)9 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)8 Utils.cacheAndAssertCachedResponse (com.apollographql.apollo.Utils.cacheAndAssertCachedResponse)6 ApolloException (com.apollographql.apollo.exception.ApolloException)4 CharacterNameByIdQuery (com.apollographql.apollo.integration.normalizer.CharacterNameByIdQuery)3 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)3 IOException (java.io.IOException)3 HeroNameWithIdQuery (com.apollographql.apollo.integration.normalizer.HeroNameWithIdQuery)2 ArrayList (java.util.ArrayList)2 UUID (java.util.UUID)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 CacheReference (com.apollographql.apollo.cache.normalized.CacheReference)1 Record (com.apollographql.apollo.cache.normalized.Record)1 CharacterDetailsQuery (com.apollographql.apollo.integration.normalizer.CharacterDetailsQuery)1 HeroWithFriendsFragment (com.apollographql.apollo.integration.normalizer.fragment.HeroWithFriendsFragment)1 HumanWithIdFragment (com.apollographql.apollo.integration.normalizer.fragment.HumanWithIdFragment)1