Search in sources :

Example 6 with CacheReference

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

use of com.apollographql.apollo.cache.normalized.CacheReference 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)

Example 8 with CacheReference

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

use of com.apollographql.apollo.cache.normalized.CacheReference 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)

Example 10 with CacheReference

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

the class ResponseNormalizationTest method testHeroNameWithVariable.

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

Aggregations

CacheReference (com.apollographql.apollo.cache.normalized.CacheReference)15 Record (com.apollographql.apollo.cache.normalized.Record)15 Test (org.junit.Test)12 HeroTypeDependentAliasedFieldQuery (com.apollographql.apollo.integration.normalizer.HeroTypeDependentAliasedFieldQuery)3 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)2 List (java.util.List)2 CacheKey (com.apollographql.apollo.cache.normalized.CacheKey)1 HeroAndFriendsNamesQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery)1 HeroAndFriendsNamesWithIDForParentOnlyQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDForParentOnlyQuery)1 HeroAndFriendsNamesWithIDsQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesWithIDsQuery)1 HeroAppearsInQuery (com.apollographql.apollo.integration.normalizer.HeroAppearsInQuery)1 HeroNameQuery (com.apollographql.apollo.integration.normalizer.HeroNameQuery)1 HeroParentTypeDependentFieldQuery (com.apollographql.apollo.integration.normalizer.HeroParentTypeDependentFieldQuery)1 SameHeroTwiceQuery (com.apollographql.apollo.integration.normalizer.SameHeroTwiceQuery)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1