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;
}
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;
}
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");
}
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");
}
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");
}
Aggregations