Search in sources :

Example 1 with Record

use of com.apollographql.apollo.cache.normalized.Record in project apollo-android by apollographql.

the class ApolloWatcherTest method testQueryWatcherUpdated_Store_write.

@Test
public void testQueryWatcherUpdated_Store_write() throws IOException, InterruptedException, TimeoutException, ApolloException {
    final List<String> heroNameList = new ArrayList<>();
    EpisodeHeroNameQuery query = EpisodeHeroNameQuery.builder().episode(Episode.EMPIRE).build();
    server.enqueue(mockResponse("EpisodeHeroNameResponseWithId.json"));
    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());
        }
    });
    assertThat(heroNameList.get(0)).isEqualTo("R2-D2");
    // Someone writes to the store directly
    Set<String> changedKeys = apolloClient.apolloStore().writeTransaction(new Transaction<WriteableStore, Set<String>>() {

        @Nullable
        @Override
        public Set<String> execute(WriteableStore cache) {
            Record record = Record.builder("2001").addField("name", "Artoo").build();
            return cache.merge(Collections.singletonList(record), CacheHeaders.NONE);
        }
    });
    apolloClient.apolloStore().publish(changedKeys);
    assertThat(heroNameList.get(1)).isEqualTo("Artoo");
    watcher.cancel();
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) ApolloException(com.apollographql.apollo.exception.ApolloException) Record(com.apollographql.apollo.cache.normalized.Record) WriteableStore(com.apollographql.apollo.internal.cache.normalized.WriteableStore) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 2 with Record

use of com.apollographql.apollo.cache.normalized.Record 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 3 with Record

use of com.apollographql.apollo.cache.normalized.Record in project apollo-android by apollographql.

the class ResponseNormalizationTest method testMergeNull.

@Test
public void testMergeNull() throws Exception {
    Record record = Record.builder("Key").addField("field1", "value1").build();
    normalizedCache.merge(Collections.singletonList(record), CacheHeaders.NONE);
    Record newRecord = record.toBuilder().addField("field2", null).build();
    normalizedCache.merge(Collections.singletonList(newRecord), CacheHeaders.NONE);
    final Record finalRecord = normalizedCache.loadRecord(record.key(), CacheHeaders.NONE);
    assertThat(finalRecord.hasField("field2")).isTrue();
    normalizedCache.remove(CacheKey.from(record.key()));
}
Also used : Record(com.apollographql.apollo.cache.normalized.Record) Test(org.junit.Test)

Example 4 with Record

use of com.apollographql.apollo.cache.normalized.Record in project apollo-android by apollographql.

the class ResponseNormalizationTest method testHeroParentTypeDependentFieldHuman.

@Test
public void testHeroParentTypeDependentFieldHuman() throws Exception {
    assertHasNoErrors("HeroParentTypeDependentFieldHumanResponse.json", new HeroParentTypeDependentFieldQuery(Input.fromNullable(EMPIRE)));
    Record lukeRecord = normalizedCache.loadRecord(TEST_FIELD_KEY_EMPIRE + ".friends.0", CacheHeaders.NONE);
    assertThat(lukeRecord.field("name")).isEqualTo("Han Solo");
    assertThat(lukeRecord.field("height({\"unit\":\"FOOT\"})")).isEqualTo(BigDecimal.valueOf(5.905512));
}
Also used : HeroParentTypeDependentFieldQuery(com.apollographql.apollo.integration.normalizer.HeroParentTypeDependentFieldQuery) Record(com.apollographql.apollo.cache.normalized.Record) Test(org.junit.Test)

Example 5 with Record

use of com.apollographql.apollo.cache.normalized.Record in project apollo-android by apollographql.

the class ResponseNormalizationTest method testHeroAndFriendsNamesQueryWithIDs.

@Test
public void testHeroAndFriendsNamesQueryWithIDs() throws Exception {
    assertHasNoErrors("HeroAndFriendsNameWithIdsResponse.json", new HeroAndFriendsNamesWithIDsQuery(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("2001"));
    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("1000"), new CacheReference("1002"), new CacheReference("1003")));
    final Record luke = normalizedCache.loadRecord("1000", CacheHeaders.NONE);
    assertThat(luke.field("name")).isEqualTo("Luke Skywalker");
}
Also used : HeroAndFriendsNamesWithIDsQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery) Record(com.apollographql.apollo.cache.normalized.Record) CacheReference(com.apollographql.apollo.cache.normalized.CacheReference) Test(org.junit.Test)

Aggregations

Record (com.apollographql.apollo.cache.normalized.Record)42 Test (org.junit.Test)35 CacheReference (com.apollographql.apollo.cache.normalized.CacheReference)15 NormalizedCache (com.apollographql.apollo.cache.normalized.NormalizedCache)7 Nonnull (javax.annotation.Nonnull)5 CacheKey (com.apollographql.apollo.cache.normalized.CacheKey)4 CacheHeaders (com.apollographql.apollo.cache.CacheHeaders)3 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)3 HeroAndFriendsNamesQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery)3 HeroTypeDependentAliasedFieldQuery (com.apollographql.apollo.integration.normalizer.HeroTypeDependentAliasedFieldQuery)3 NonNull (android.support.annotation.NonNull)2 ApolloCacheHeaders (com.apollographql.apollo.cache.ApolloCacheHeaders)2 NormalizedCacheFactory (com.apollographql.apollo.cache.normalized.NormalizedCacheFactory)2 RecordFieldJsonAdapter (com.apollographql.apollo.cache.normalized.RecordFieldJsonAdapter)2 ApolloException (com.apollographql.apollo.exception.ApolloException)2 HeroParentTypeDependentFieldQuery (com.apollographql.apollo.integration.normalizer.HeroParentTypeDependentFieldQuery)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Set (java.util.Set)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2