Search in sources :

Example 21 with Record

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

the class SqlNormalizedCacheTest method testRecordSelection_recordNotPresent.

@Test
public void testRecordSelection_recordNotPresent() {
    Record record = sqlStore.loadRecord(STANDARD_KEY, CacheHeaders.NONE);
    assertThat(record).isNull();
}
Also used : Record(com.apollographql.apollo.cache.normalized.Record) Test(org.junit.Test)

Example 22 with Record

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

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

the class ApolloStoreTest method storeClearAllCallsNormalizedCacheClearAll.

@Test
public void storeClearAllCallsNormalizedCacheClearAll() throws Exception {
    final NamedCountDownLatch latch = new NamedCountDownLatch("storeClearAllCallsNormalizedCacheClearAll", 1);
    final RealApolloStore realApolloStore = new RealApolloStore(new NormalizedCache() {

        @Nullable
        @Override
        public Record loadRecord(@Nonnull String key, @Nonnull CacheHeaders cacheHeaders) {
            return null;
        }

        @Nonnull
        @Override
        public Set<String> merge(@Nonnull Record record, @Nonnull CacheHeaders cacheHeaders) {
            return emptySet();
        }

        @Override
        public void clearAll() {
            latch.countDown();
        }

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

        @Nonnull
        @Override
        protected Set<String> performMerge(@Nonnull Record apolloRecord, @Nonnull CacheHeaders cacheHeaders) {
            return emptySet();
        }
    }, CacheKeyResolver.DEFAULT, new ScalarTypeAdapters(Collections.EMPTY_MAP), Executors.newSingleThreadExecutor(), new ApolloLogger(Optional.<Logger>absent()));
    realApolloStore.clearAll().execute();
    latch.awaitOrThrowWithTimeout(3, TimeUnit.SECONDS);
}
Also used : Collections.emptySet(java.util.Collections.emptySet) Set(java.util.Set) Nonnull(javax.annotation.Nonnull) NamedCountDownLatch(com.apollographql.apollo.NamedCountDownLatch) ApolloLogger(com.apollographql.apollo.internal.ApolloLogger) Logger(com.apollographql.apollo.Logger) ApolloLogger(com.apollographql.apollo.internal.ApolloLogger) ScalarTypeAdapters(com.apollographql.apollo.response.ScalarTypeAdapters) CacheHeaders(com.apollographql.apollo.cache.CacheHeaders) NormalizedCache(com.apollographql.apollo.cache.normalized.NormalizedCache) Record(com.apollographql.apollo.cache.normalized.Record) Nullable(javax.annotation.Nullable) CacheKey(com.apollographql.apollo.cache.normalized.CacheKey) Test(org.junit.Test)

Example 24 with Record

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

the class CacheFieldValueResolver method valueForList.

@SuppressWarnings("unchecked")
private List valueForList(List values) {
    if (values == null) {
        return null;
    }
    List result = new ArrayList();
    for (Object value : values) {
        if (value instanceof CacheReference) {
            CacheReference reference = (CacheReference) value;
            Record referencedRecord = readableCache.read(reference.key(), cacheHeaders);
            if (referencedRecord == null) {
                // evicted from LRU cache, we must prevent of further resolving cache response as it's broken
                throw new IllegalStateException("Cache MISS: failed to find record in cache by reference");
            }
            result.add(referencedRecord);
        } else if (value instanceof List) {
            result.add(valueForList((List) value));
        } else {
            result.add(value);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) CacheReference(com.apollographql.apollo.cache.normalized.CacheReference) Record(com.apollographql.apollo.cache.normalized.Record)

Example 25 with Record

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

the class CacheFieldValueResolver method valueForObject.

private Record valueForObject(Record record, ResponseField field) {
    CacheReference cacheReference;
    CacheKey fieldCacheKey = cacheKeyResolver.fromFieldArguments(field, variables);
    if (!fieldCacheKey.equals(CacheKey.NO_KEY)) {
        cacheReference = new CacheReference(fieldCacheKey.key());
    } else {
        cacheReference = fieldValue(record, field);
    }
    if (cacheReference != null) {
        Record referencedRecord = readableCache.read(cacheReference.key(), cacheHeaders);
        if (referencedRecord == null) {
            // evicted from LRU cache, we must prevent of further resolving cache response as it's broken
            throw new IllegalStateException("Cache MISS: failed to find record in cache by reference");
        }
        return referencedRecord;
    }
    return null;
}
Also used : CacheReference(com.apollographql.apollo.cache.normalized.CacheReference) Record(com.apollographql.apollo.cache.normalized.Record) CacheKey(com.apollographql.apollo.cache.normalized.CacheKey)

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