Search in sources :

Example 1 with HeroAndFriendsNamesQuery

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

the class CacheHeadersTest method testHeadersReceived.

@Test
@SuppressWarnings("CheckReturnValue")
public void testHeadersReceived() throws ApolloException, IOException {
    final AtomicBoolean hasHeader = new AtomicBoolean();
    final NormalizedCache normalizedCache = new NormalizedCache() {

        @Nullable
        @Override
        public Record loadRecord(@NonNull String key, @NonNull CacheHeaders cacheHeaders) {
            hasHeader.set(cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE));
            return null;
        }

        @Nonnull
        @Override
        public Set<String> merge(@NonNull Record record, @NonNull CacheHeaders cacheHeaders) {
            hasHeader.set(cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE));
            return emptySet();
        }

        @Override
        public void clearAll() {
        }

        @Override
        public boolean remove(@Nonnull CacheKey cacheKey) {
            return false;
        }

        @Nonnull
        @Override
        protected Set<String> performMerge(@Nonnull Record apolloRecord, @Nonnull CacheHeaders cacheHeaders) {
            return emptySet();
        }
    };
    final NormalizedCacheFactory<NormalizedCache> cacheFactory = new NormalizedCacheFactory<NormalizedCache>() {

        @Override
        public NormalizedCache create(RecordFieldJsonAdapter recordFieldAdapter) {
            return normalizedCache;
        }
    };
    ApolloClient apolloClient = ApolloClient.builder().normalizedCache(cacheFactory, new IdFieldCacheKeyResolver()).serverUrl(server.url("/")).okHttpClient(new OkHttpClient.Builder().dispatcher(new Dispatcher(Utils.immediateExecutorService())).build()).dispatcher(Utils.immediateExecutor()).build();
    server.enqueue(mockResponse("HeroAndFriendsNameResponse.json"));
    CacheHeaders cacheHeaders = CacheHeaders.builder().addHeader(ApolloCacheHeaders.DO_NOT_STORE, "true").build();
    Rx2Apollo.from(apolloClient.query(new HeroAndFriendsNamesQuery(Input.fromNullable(Episode.NEWHOPE))).cacheHeaders(cacheHeaders)).test();
    assertThat(hasHeader.get()).isTrue();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Nonnull(javax.annotation.Nonnull) Dispatcher(okhttp3.Dispatcher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RecordFieldJsonAdapter(com.apollographql.apollo.cache.normalized.RecordFieldJsonAdapter) ApolloCacheHeaders(com.apollographql.apollo.cache.ApolloCacheHeaders) CacheHeaders(com.apollographql.apollo.cache.CacheHeaders) HeroAndFriendsNamesQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery) NormalizedCache(com.apollographql.apollo.cache.normalized.NormalizedCache) NonNull(android.support.annotation.NonNull) NormalizedCacheFactory(com.apollographql.apollo.cache.normalized.NormalizedCacheFactory) Record(com.apollographql.apollo.cache.normalized.Record) CacheKey(com.apollographql.apollo.cache.normalized.CacheKey) Test(org.junit.Test)

Example 2 with HeroAndFriendsNamesQuery

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

the class ResponseNormalizationTest method testHeroAndFriendsNamesQueryWithoutIDs.

@Test
public void testHeroAndFriendsNamesQueryWithoutIDs() throws Exception {
    assertHasNoErrors("HeroAndFriendsNameResponse.json", new HeroAndFriendsNamesQuery(Input.fromNullable(JEDI)));
    Record record = normalizedCache.loadRecord(QUERY_ROOT_KEY, CacheHeaders.NONE);
    CacheReference heroReference = (CacheReference) record.field(TEST_FIELD_KEY_JEDI);
    assertThat(heroReference).isEqualTo(new CacheReference(TEST_FIELD_KEY_JEDI));
    final Record heroRecord = normalizedCache.loadRecord(heroReference.key(), CacheHeaders.NONE);
    assertThat(heroRecord.field("name")).isEqualTo("R2-D2");
    assertThat(heroRecord.field("friends")).isEqualTo(Arrays.asList(new CacheReference(TEST_FIELD_KEY_JEDI + ".friends.0"), new CacheReference(TEST_FIELD_KEY_JEDI + ".friends.1"), new CacheReference(TEST_FIELD_KEY_JEDI + ".friends.2")));
    final Record luke = normalizedCache.loadRecord(TEST_FIELD_KEY_JEDI + ".friends.0", CacheHeaders.NONE);
    assertThat(luke.field("name")).isEqualTo("Luke Skywalker");
}
Also used : HeroAndFriendsNamesQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery) Record(com.apollographql.apollo.cache.normalized.Record) CacheReference(com.apollographql.apollo.cache.normalized.CacheReference) Test(org.junit.Test)

Example 3 with HeroAndFriendsNamesQuery

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

the class SendOperationIdentifiersTest method sendOperationIdsTrue.

@Test
public void sendOperationIdsTrue() throws Exception {
    final HeroAndFriendsNamesQuery query = new HeroAndFriendsNamesQuery(Input.fromNullable(EMPIRE));
    ApolloClient apolloClient = ApolloClient.builder().serverUrl(server.url("/")).sendOperationIdentifiers(true).build();
    apolloClient.query(query).enqueue(null);
    String serverRequest = server.takeRequest().getBody().readUtf8();
    assertThat(serverRequest.contains(String.format("\"id\":\"%s\"", query.operationId()))).isTrue();
}
Also used : HeroAndFriendsNamesQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery) Test(org.junit.Test)

Example 4 with HeroAndFriendsNamesQuery

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

the class CacheHeadersTest method testDefaultHeadersReceived.

@Test
@SuppressWarnings("CheckReturnValue")
public void testDefaultHeadersReceived() throws Exception {
    final AtomicBoolean hasHeader = new AtomicBoolean();
    final NormalizedCache normalizedCache = new NormalizedCache() {

        @Nullable
        @Override
        public Record loadRecord(@NonNull String key, @NonNull CacheHeaders cacheHeaders) {
            hasHeader.set(cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE));
            return null;
        }

        @Nonnull
        @Override
        public Set<String> merge(@NonNull Record record, @NonNull CacheHeaders cacheHeaders) {
            hasHeader.set(cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE));
            return emptySet();
        }

        @Override
        public void clearAll() {
        }

        @Override
        public boolean remove(@Nonnull CacheKey cacheKey) {
            return false;
        }

        @Nonnull
        @Override
        protected Set<String> performMerge(@Nonnull Record apolloRecord, @Nonnull CacheHeaders cacheHeaders) {
            return emptySet();
        }
    };
    final NormalizedCacheFactory<NormalizedCache> cacheFactory = new NormalizedCacheFactory<NormalizedCache>() {

        @Override
        public NormalizedCache create(RecordFieldJsonAdapter recordFieldAdapter) {
            return normalizedCache;
        }
    };
    CacheHeaders cacheHeaders = CacheHeaders.builder().addHeader(ApolloCacheHeaders.DO_NOT_STORE, "true").build();
    ApolloClient apolloClient = ApolloClient.builder().normalizedCache(cacheFactory, new IdFieldCacheKeyResolver()).serverUrl(server.url("/")).okHttpClient(new OkHttpClient.Builder().dispatcher(new Dispatcher(Utils.immediateExecutorService())).build()).dispatcher(Utils.immediateExecutor()).defaultCacheHeaders(cacheHeaders).build();
    server.enqueue(mockResponse("HeroAndFriendsNameResponse.json"));
    Rx2Apollo.from(apolloClient.query(new HeroAndFriendsNamesQuery(Input.fromNullable(Episode.NEWHOPE))).cacheHeaders(cacheHeaders)).test();
    assertThat(hasHeader.get()).isTrue();
}
Also used : Nonnull(javax.annotation.Nonnull) Dispatcher(okhttp3.Dispatcher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RecordFieldJsonAdapter(com.apollographql.apollo.cache.normalized.RecordFieldJsonAdapter) ApolloCacheHeaders(com.apollographql.apollo.cache.ApolloCacheHeaders) CacheHeaders(com.apollographql.apollo.cache.CacheHeaders) HeroAndFriendsNamesQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery) NormalizedCache(com.apollographql.apollo.cache.normalized.NormalizedCache) NonNull(android.support.annotation.NonNull) NormalizedCacheFactory(com.apollographql.apollo.cache.normalized.NormalizedCacheFactory) Record(com.apollographql.apollo.cache.normalized.Record) CacheKey(com.apollographql.apollo.cache.normalized.CacheKey) Test(org.junit.Test)

Example 5 with HeroAndFriendsNamesQuery

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

the class OptimisticCacheTestCase method simple.

@Test
public void simple() throws Exception {
    HeroAndFriendsNamesQuery query = new HeroAndFriendsNamesQuery(Input.fromNullable(Episode.JEDI));
    enqueueAndAssertResponse(server, "HeroAndFriendsNameResponse.json", apolloClient.query(query), new Predicate<Response<HeroAndFriendsNamesQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsNamesQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    UUID mutationId = UUID.randomUUID();
    HeroAndFriendsNamesQuery.Data data = new HeroAndFriendsNamesQuery.Data(new HeroAndFriendsNamesQuery.Hero("Droid", "R222-D222", Arrays.asList(new HeroAndFriendsNamesQuery.Friend("Human", "SuperMan"), new HeroAndFriendsNamesQuery.Friend("Human", "Batman"))));
    apolloClient.apolloStore().writeOptimisticUpdatesAndPublish(query, data, mutationId).execute();
    assertResponse(apolloClient.query(query).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroAndFriendsNamesQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsNamesQuery.Data> response) throws Exception {
            assertThat(response.data().hero().name()).isEqualTo("R222-D222");
            assertThat(response.data().hero().friends()).hasSize(2);
            assertThat(response.data().hero().friends().get(0).name()).isEqualTo("SuperMan");
            assertThat(response.data().hero().friends().get(1).name()).isEqualTo("Batman");
            return true;
        }
    });
    apolloClient.apolloStore().rollbackOptimisticUpdates(mutationId).execute();
    assertResponse(apolloClient.query(query).responseFetcher(CACHE_ONLY), new Predicate<Response<HeroAndFriendsNamesQuery.Data>>() {

        @Override
        public boolean test(Response<HeroAndFriendsNamesQuery.Data> response) throws Exception {
            assertThat(response.data().hero().name()).isEqualTo("R2-D2");
            assertThat(response.data().hero().friends()).hasSize(3);
            assertThat(response.data().hero().friends().get(0).name()).isEqualTo("Luke Skywalker");
            assertThat(response.data().hero().friends().get(1).name()).isEqualTo("Han Solo");
            assertThat(response.data().hero().friends().get(2).name()).isEqualTo("Leia Organa");
            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) HeroAndFriendsNamesQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery) UUID(java.util.UUID) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Aggregations

HeroAndFriendsNamesQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery)7 Test (org.junit.Test)7 Record (com.apollographql.apollo.cache.normalized.Record)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Dispatcher (okhttp3.Dispatcher)3 NonNull (android.support.annotation.NonNull)2 Utils.enqueueAndAssertResponse (com.apollographql.apollo.Utils.enqueueAndAssertResponse)2 ApolloCacheHeaders (com.apollographql.apollo.cache.ApolloCacheHeaders)2 CacheHeaders (com.apollographql.apollo.cache.CacheHeaders)2 CacheKey (com.apollographql.apollo.cache.normalized.CacheKey)2 NormalizedCache (com.apollographql.apollo.cache.normalized.NormalizedCache)2 NormalizedCacheFactory (com.apollographql.apollo.cache.normalized.NormalizedCacheFactory)2 RecordFieldJsonAdapter (com.apollographql.apollo.cache.normalized.RecordFieldJsonAdapter)2 IOException (java.io.IOException)2 Nonnull (javax.annotation.Nonnull)2 OkHttpClient (okhttp3.OkHttpClient)2 Utils.assertResponse (com.apollographql.apollo.Utils.assertResponse)1 Response (com.apollographql.apollo.api.Response)1 CacheReference (com.apollographql.apollo.cache.normalized.CacheReference)1 ApolloException (com.apollographql.apollo.exception.ApolloException)1