Search in sources :

Example 6 with Record

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

the class ResponseNormalizationTest method testHeroAndFriendsNamesWithIDForParentOnly.

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

Example 7 with Record

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

the class ResponseNormalizationTest method testHeroAndFriendsNamesQueryWithoutIDs.

@Test
public void testHeroAndFriendsNamesQueryWithoutIDs() throws Exception {
    assertHasNoErrors("HeroAndFriendsNameResponse.json", new HeroAndFriendsNamesQuery(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(TEST_FIELD_KEY_JEDI));
    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(TEST_FIELD_KEY_JEDI + ".friends.0"), new CacheReference(TEST_FIELD_KEY_JEDI + ".friends.1"), new CacheReference(TEST_FIELD_KEY_JEDI + ".friends.2")));
    final Record luke = normalizedCache.loadRecord(TEST_FIELD_KEY_JEDI + ".friends.0", CacheHeaders.NONE);
    assertThat(luke.field("name")).isEqualTo("Luke Skywalker");
}
Also used : HeroAndFriendsNamesQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery) Record(com.apollographql.apollo.cache.normalized.Record) CacheReference(com.apollographql.apollo.cache.normalized.CacheReference) Test(org.junit.Test)

Example 8 with Record

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

the class ResponseNormalizationTest method testHeroParentTypeDependentAliasedFieldQueryHuman.

@Test
public void testHeroParentTypeDependentAliasedFieldQueryHuman() 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 9 with Record

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

the class ResponseNormalizer method didResolveObject.

@Override
public void didResolveObject(ResponseField field, Optional<R> objectSource) {
    path = pathStack.pop();
    if (objectSource.isPresent()) {
        Record completedRecord = currentRecordBuilder.build();
        valueStack.push(new CacheReference(completedRecord.key()));
        dependentKeys.add(completedRecord.key());
        recordSet.merge(completedRecord);
    }
    currentRecordBuilder = recordStack.pop().toBuilder();
}
Also used : Record(com.apollographql.apollo.cache.normalized.Record) CacheReference(com.apollographql.apollo.cache.normalized.CacheReference)

Example 10 with Record

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

the class LruNormalizedCache method performMerge.

@Nonnull
protected Set<String> performMerge(@Nonnull final Record apolloRecord, @Nonnull final CacheHeaders cacheHeaders) {
    final Record oldRecord = lruCache.getIfPresent(apolloRecord.key());
    if (oldRecord == null) {
        lruCache.put(apolloRecord.key(), apolloRecord);
        return Collections.emptySet();
    } else {
        Set<String> changedKeys = oldRecord.mergeWith(apolloRecord);
        // re-insert to trigger new weight calculation
        lruCache.put(apolloRecord.key(), oldRecord);
        return changedKeys;
    }
}
Also used : Record(com.apollographql.apollo.cache.normalized.Record) Nonnull(javax.annotation.Nonnull)

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