Search in sources :

Example 1 with HeroNameQuery

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

the class OptimisticCacheTestCase method full_persisted_partial_optimistic.

@Test
public void full_persisted_partial_optimistic() throws Exception {
    enqueueAndAssertResponse(server, "HeroNameWithEnumsResponse.json", apolloClient.query(new HeroNameWithEnumsQuery()), new Predicate<Response<HeroNameWithEnumsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroNameWithEnumsQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    UUID mutationId = UUID.randomUUID();
    apolloClient.apolloStore().writeOptimisticUpdates(new HeroNameQuery(), new HeroNameQuery.Data(new HeroNameQuery.Hero("Droid", "R22-D22")), mutationId).execute();
    assertResponse(apolloClient.query(new HeroNameWithEnumsQuery()).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroNameWithEnumsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroNameWithEnumsQuery.Data> response) throws Exception {
            assertThat(response.data().hero().name()).isEqualTo("R22-D22");
            assertThat(response.data().hero().firstAppearsIn()).isEqualTo(Episode.EMPIRE);
            assertThat(response.data().hero().appearsIn()).isEqualTo(Arrays.asList(Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI));
            return true;
        }
    });
    apolloClient.apolloStore().rollbackOptimisticUpdates(mutationId).execute();
    assertResponse(apolloClient.query(new HeroNameWithEnumsQuery()).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroNameWithEnumsQuery.Data>>() {

        @Override
        public boolean test(Response<HeroNameWithEnumsQuery.Data> response) throws Exception {
            assertThat(response.data().hero().name()).isEqualTo("R2-D2");
            assertThat(response.data().hero().firstAppearsIn()).isEqualTo(Episode.EMPIRE);
            assertThat(response.data().hero().appearsIn()).isEqualTo(Arrays.asList(Episode.NEWHOPE, Episode.EMPIRE, Episode.JEDI));
            return true;
        }
    });
}
Also used : Utils.enqueueAndAssertResponse(com.apollographql.apollo.Utils.enqueueAndAssertResponse) Utils.assertResponse(com.apollographql.apollo.Utils.assertResponse) Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) HeroNameWithEnumsQuery(com.apollographql.apollo.integration.normalizer.HeroNameWithEnumsQuery) HeroNameQuery(com.apollographql.apollo.integration.normalizer.HeroNameQuery) UUID(java.util.UUID) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Example 2 with HeroNameQuery

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

the class IntegrationTest method operationResponseParser.

@Test
public void operationResponseParser() throws Exception {
    String json = Utils.readFileToString(getClass(), "/HeroNameResponse.json");
    HeroNameQuery query = new HeroNameQuery();
    Response<HeroNameQuery.Data> response = new OperationResponseParser<>(query, query.responseFieldMapper(), new ScalarTypeAdapters(Collections.EMPTY_MAP)).parse(new Buffer().writeUtf8(json));
    assertThat(response.data().hero().name()).isEqualTo("R2-D2");
}
Also used : Buffer(okio.Buffer) ScalarTypeAdapters(com.apollographql.apollo.response.ScalarTypeAdapters) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) HeroNameQuery(com.apollographql.apollo.integration.normalizer.HeroNameQuery) Test(org.junit.Test)

Example 3 with HeroNameQuery

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

the class ResponseNormalizationTest method testHeroName.

@Test
public void testHeroName() throws Exception {
    assertHasNoErrors("HeroNameResponse.json", new HeroNameQuery());
    Record record = normalizedCache.loadRecord(QUERY_ROOT_KEY, CacheHeaders.NONE);
    CacheReference reference = (CacheReference) record.field("hero");
    assertThat(reference).isEqualTo(new CacheReference("hero"));
    final Record heroRecord = normalizedCache.loadRecord(reference.key(), CacheHeaders.NONE);
    assertThat(heroRecord.field("name")).isEqualTo("R2-D2");
}
Also used : Record(com.apollographql.apollo.cache.normalized.Record) CacheReference(com.apollographql.apollo.cache.normalized.CacheReference) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) HeroNameQuery(com.apollographql.apollo.integration.normalizer.HeroNameQuery) Test(org.junit.Test)

Aggregations

HeroNameQuery (com.apollographql.apollo.integration.normalizer.HeroNameQuery)3 Test (org.junit.Test)3 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)2 Utils.assertResponse (com.apollographql.apollo.Utils.assertResponse)1 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)1 Response (com.apollographql.apollo.api.Response)1 CacheReference (com.apollographql.apollo.cache.normalized.CacheReference)1 Record (com.apollographql.apollo.cache.normalized.Record)1 ApolloException (com.apollographql.apollo.exception.ApolloException)1 HeroNameWithEnumsQuery (com.apollographql.apollo.integration.normalizer.HeroNameWithEnumsQuery)1 ScalarTypeAdapters (com.apollographql.apollo.response.ScalarTypeAdapters)1 IOException (java.io.IOException)1 UUID (java.util.UUID)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 Buffer (okio.Buffer)1