Search in sources :

Example 1 with HeroWithFriendsFragment

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

the class ResponseWriteTestCase method fragments.

@Test
public void fragments() throws Exception {
    HeroAndFriendsWithFragmentsQuery query = new HeroAndFriendsWithFragmentsQuery(Input.fromNullable(Episode.NEWHOPE));
    enqueueAndAssertResponse(server, "HeroAndFriendsWithFragmentResponse.json", apolloClient.query(query), new Predicate<Response<HeroAndFriendsWithFragmentsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsWithFragmentsQuery.Data> response) throws Exception {
            assertThat(response.data().hero().__typename()).isEqualTo("Droid");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().__typename()).isEqualTo("Droid");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().id()).isEqualTo("2001");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().name()).isEqualTo("R2-D2");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends()).hasSize(3);
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().__typename()).isEqualTo("Human");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().id()).isEqualTo("1000");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().name()).isEqualTo("Luke Skywalker");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().__typename()).isEqualTo("Human");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().id()).isEqualTo("1002");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().name()).isEqualTo("Han Solo");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(2).fragments().humanWithIdFragment().__typename()).isEqualTo("Human");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(2).fragments().humanWithIdFragment().id()).isEqualTo("1003");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(2).fragments().humanWithIdFragment().name()).isEqualTo("Leia Organa");
            return true;
        }
    });
    apolloClient.apolloStore().write(new HeroWithFriendsFragment("Droid", "2001", "R222-D222", asList(new HeroWithFriendsFragment.Friend("Human", new HeroWithFriendsFragment.Friend.Fragments(new HumanWithIdFragment("Human", "1000", "SuperMan"))), new HeroWithFriendsFragment.Friend("Human", new HeroWithFriendsFragment.Friend.Fragments(new HumanWithIdFragment("Human", "1002", "Han Solo"))))), CacheKey.from("2001"), query.variables()).execute();
    apolloClient.apolloStore().write(new HumanWithIdFragment("Human", "1002", "Beast"), CacheKey.from("1002"), query.variables()).execute();
    assertCachedQueryResponse(query, new Predicate<Response<HeroAndFriendsWithFragmentsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsWithFragmentsQuery.Data> response) throws Exception {
            assertThat(response.data().hero().__typename()).isEqualTo("Droid");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().__typename()).isEqualTo("Droid");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().id()).isEqualTo("2001");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().name()).isEqualTo("R222-D222");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends()).hasSize(2);
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().__typename()).isEqualTo("Human");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().id()).isEqualTo("1000");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().name()).isEqualTo("SuperMan");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().__typename()).isEqualTo("Human");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().id()).isEqualTo("1002");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().name()).isEqualTo("Beast");
            return true;
        }
    });
}
Also used : Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) HeroWithFriendsFragment(com.apollographql.apollo.integration.normalizer.fragment.HeroWithFriendsFragment) HumanWithIdFragment(com.apollographql.apollo.integration.normalizer.fragment.HumanWithIdFragment) HeroAndFriendsWithFragmentsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsWithFragmentsQuery) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 2 with HeroWithFriendsFragment

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

the class ResponseWriteTestCase method operation_with_fragments.

@Test
public void operation_with_fragments() throws Exception {
    HeroAndFriendsWithFragmentsQuery query = new HeroAndFriendsWithFragmentsQuery(Input.fromNullable(Episode.NEWHOPE));
    enqueueAndAssertResponse(server, "HeroAndFriendsWithFragmentResponse.json", apolloClient.query(query), new Predicate<Response<HeroAndFriendsWithFragmentsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsWithFragmentsQuery.Data> response) throws Exception {
            assertThat(response.data().hero().__typename()).isEqualTo("Droid");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().__typename()).isEqualTo("Droid");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().id()).isEqualTo("2001");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().name()).isEqualTo("R2-D2");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends()).hasSize(3);
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().__typename()).isEqualTo("Human");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().id()).isEqualTo("1000");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().name()).isEqualTo("Luke Skywalker");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().__typename()).isEqualTo("Human");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().id()).isEqualTo("1002");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().name()).isEqualTo("Han Solo");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(2).fragments().humanWithIdFragment().__typename()).isEqualTo("Human");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(2).fragments().humanWithIdFragment().id()).isEqualTo("1003");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(2).fragments().humanWithIdFragment().name()).isEqualTo("Leia Organa");
            return true;
        }
    });
    HeroAndFriendsWithFragmentsQuery.Hero hero = new HeroAndFriendsWithFragmentsQuery.Hero("Droid", new HeroAndFriendsWithFragmentsQuery.Hero.Fragments(new HeroWithFriendsFragment("Droid", "2001", "R222-D222", asList(new HeroWithFriendsFragment.Friend("Human", new HeroWithFriendsFragment.Friend.Fragments(new HumanWithIdFragment("Human", "1006", "SuperMan"))), new HeroWithFriendsFragment.Friend("Human", new HeroWithFriendsFragment.Friend.Fragments(new HumanWithIdFragment("Human", "1004", "Beast")))))));
    apolloClient.apolloStore().write(query, new HeroAndFriendsWithFragmentsQuery.Data(hero)).execute();
    assertCachedQueryResponse(query, new Predicate<Response<HeroAndFriendsWithFragmentsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsWithFragmentsQuery.Data> response) throws Exception {
            assertThat(response.data().hero().__typename()).isEqualTo("Droid");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().__typename()).isEqualTo("Droid");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().id()).isEqualTo("2001");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().name()).isEqualTo("R222-D222");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends()).hasSize(2);
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().__typename()).isEqualTo("Human");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().id()).isEqualTo("1006");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(0).fragments().humanWithIdFragment().name()).isEqualTo("SuperMan");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().__typename()).isEqualTo("Human");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().id()).isEqualTo("1004");
            assertThat(response.data().hero().fragments().heroWithFriendsFragment().friends().get(1).fragments().humanWithIdFragment().name()).isEqualTo("Beast");
            return true;
        }
    });
}
Also used : HeroAndFriendsWithFragmentsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsWithFragmentsQuery) ParseException(java.text.ParseException) Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) HeroWithFriendsFragment(com.apollographql.apollo.integration.normalizer.fragment.HeroWithFriendsFragment) HumanWithIdFragment(com.apollographql.apollo.integration.normalizer.fragment.HumanWithIdFragment) Test(org.junit.Test)

Example 3 with HeroWithFriendsFragment

use of com.apollographql.apollo.integration.normalizer.fragment.HeroWithFriendsFragment 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

Utils.assertResponse (com.apollographql.apollo.Utils.assertResponse)3 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)3 Response (com.apollographql.apollo.api.Response)3 HeroWithFriendsFragment (com.apollographql.apollo.integration.normalizer.fragment.HeroWithFriendsFragment)3 HumanWithIdFragment (com.apollographql.apollo.integration.normalizer.fragment.HumanWithIdFragment)3 Test (org.junit.Test)3 HeroAndFriendsWithFragmentsQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsWithFragmentsQuery)2 ParseException (java.text.ParseException)2 Utils.cacheAndAssertCachedResponse (com.apollographql.apollo.Utils.cacheAndAssertCachedResponse)1 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)1 HeroAndFriendsNamesWithIDsQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery)1