Search in sources :

Example 26 with Record

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

the class ApolloCacheInterceptor method resolveFromCache.

InterceptorResponse resolveFromCache(InterceptorRequest request) throws ApolloException {
    ResponseNormalizer<Record> responseNormalizer = apolloStore.cacheResponseNormalizer();
    // noinspection unchecked
    ApolloStoreOperation<Response> apolloStoreOperation = apolloStore.read(request.operation, responseFieldMapper, responseNormalizer, request.cacheHeaders);
    Response cachedResponse = apolloStoreOperation.execute();
    if (cachedResponse.data() != null) {
        logger.d("Cache HIT for operation %s", request.operation);
        return new InterceptorResponse(null, cachedResponse, responseNormalizer.records());
    }
    logger.d("Cache MISS for operation %s", request.operation);
    throw new ApolloException(String.format("Cache miss for operation %s", request.operation));
}
Also used : Response(com.apollographql.apollo.api.Response) ApolloException(com.apollographql.apollo.exception.ApolloException) Record(com.apollographql.apollo.cache.normalized.Record)

Example 27 with Record

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

the class SqlNormalizedCacheTest method testHeader_evictAfterRead.

// Tests for StandardCacheHeader compliance
@Test
public void testHeader_evictAfterRead() {
    createRecord(STANDARD_KEY);
    Record record = sqlStore.loadRecord(STANDARD_KEY, CacheHeaders.builder().addHeader(ApolloCacheHeaders.EVICT_AFTER_READ, "true").build());
    assertThat(record).isNotNull();
    Record nullRecord = sqlStore.loadRecord(STANDARD_KEY, CacheHeaders.builder().addHeader(ApolloCacheHeaders.EVICT_AFTER_READ, "true").build());
    assertThat(nullRecord).isNull();
}
Also used : Record(com.apollographql.apollo.cache.normalized.Record) Test(org.junit.Test)

Example 28 with Record

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

the class SqlNormalizedCache method performMerge.

@Nonnull
protected Set<String> performMerge(@Nonnull final Record apolloRecord, @Nonnull final CacheHeaders cacheHeaders) {
    Optional<Record> optionalOldRecord = selectRecordForKey(apolloRecord.key());
    Set<String> changedKeys;
    if (!optionalOldRecord.isPresent()) {
        createRecord(apolloRecord.key(), recordFieldAdapter.toJson(apolloRecord.fields()));
        changedKeys = Collections.emptySet();
    } else {
        Record oldRecord = optionalOldRecord.get();
        changedKeys = oldRecord.mergeWith(apolloRecord);
        if (!changedKeys.isEmpty()) {
            updateRecord(oldRecord.key(), recordFieldAdapter.toJson(oldRecord.fields()));
        }
    }
    return changedKeys;
}
Also used : Record(com.apollographql.apollo.cache.normalized.Record) Nonnull(javax.annotation.Nonnull)

Example 29 with Record

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

the class ResponseNormalizationTest method testHeroTypeDependentAliasedFieldQueryDroid.

@Test
public void testHeroTypeDependentAliasedFieldQueryDroid() throws Exception {
    assertHasNoErrors("HeroTypeDependentAliasedFieldResponse.json", new HeroTypeDependentAliasedFieldQuery(Input.fromNullable(JEDI)));
    Record record = normalizedCache.loadRecord(QUERY_ROOT_KEY, CacheHeaders.NONE);
    CacheReference heroReference = (CacheReference) record.field(TEST_FIELD_KEY_JEDI);
    final Record hero = normalizedCache.loadRecord(heroReference.key(), CacheHeaders.NONE);
    assertThat(hero.field("primaryFunction")).isEqualTo("Astromech");
    assertThat(hero.field("__typename")).isEqualTo("Droid");
}
Also used : HeroTypeDependentAliasedFieldQuery(com.apollographql.apollo.integration.normalizer.HeroTypeDependentAliasedFieldQuery) Record(com.apollographql.apollo.cache.normalized.Record) CacheReference(com.apollographql.apollo.cache.normalized.CacheReference) Test(org.junit.Test)

Example 30 with Record

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

the class ResponseNormalizationTest method testHeroTypeDependentAliasedFieldQueryHuman.

@Test
public void testHeroTypeDependentAliasedFieldQueryHuman() throws Exception {
    assertHasNoErrors("HeroTypeDependentAliasedFieldResponseHuman.json", new HeroTypeDependentAliasedFieldQuery(Input.fromNullable(EMPIRE)));
    Record record = normalizedCache.loadRecord(QUERY_ROOT_KEY, CacheHeaders.NONE);
    CacheReference heroReference = (CacheReference) record.field(TEST_FIELD_KEY_EMPIRE);
    final Record hero = normalizedCache.loadRecord(heroReference.key(), CacheHeaders.NONE);
    assertThat(hero.field("homePlanet")).isEqualTo("Tatooine");
    assertThat(hero.field("__typename")).isEqualTo("Human");
}
Also used : HeroTypeDependentAliasedFieldQuery(com.apollographql.apollo.integration.normalizer.HeroTypeDependentAliasedFieldQuery) 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