use of com.apollographql.apollo.cache.normalized.Record in project apollo-android by apollographql.
the class ApolloWatcherTest method testQueryWatcherUpdated_Store_write.
@Test
public void testQueryWatcherUpdated_Store_write() throws IOException, InterruptedException, TimeoutException, ApolloException {
final List<String> heroNameList = new ArrayList<>();
EpisodeHeroNameQuery query = EpisodeHeroNameQuery.builder().episode(Episode.EMPIRE).build();
server.enqueue(mockResponse("EpisodeHeroNameResponseWithId.json"));
ApolloQueryWatcher<EpisodeHeroNameQuery.Data> watcher = apolloClient.query(query).watcher();
watcher.enqueueAndWatch(new ApolloCall.Callback<EpisodeHeroNameQuery.Data>() {
@Override
public void onResponse(@Nonnull Response<EpisodeHeroNameQuery.Data> response) {
heroNameList.add(response.data().hero().name());
}
@Override
public void onFailure(@Nonnull ApolloException e) {
Assert.fail(e.getMessage());
}
});
assertThat(heroNameList.get(0)).isEqualTo("R2-D2");
// Someone writes to the store directly
Set<String> changedKeys = apolloClient.apolloStore().writeTransaction(new Transaction<WriteableStore, Set<String>>() {
@Nullable
@Override
public Set<String> execute(WriteableStore cache) {
Record record = Record.builder("2001").addField("name", "Artoo").build();
return cache.merge(Collections.singletonList(record), CacheHeaders.NONE);
}
});
apolloClient.apolloStore().publish(changedKeys);
assertThat(heroNameList.get(1)).isEqualTo("Artoo");
watcher.cancel();
}
use of com.apollographql.apollo.cache.normalized.Record in project apollo-android by apollographql.
the class CacheHeadersTest method testHeadersReceived.
@Test
@SuppressWarnings("CheckReturnValue")
public void testHeadersReceived() throws ApolloException, IOException {
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;
}
};
ApolloClient apolloClient = ApolloClient.builder().normalizedCache(cacheFactory, new IdFieldCacheKeyResolver()).serverUrl(server.url("/")).okHttpClient(new OkHttpClient.Builder().dispatcher(new Dispatcher(Utils.immediateExecutorService())).build()).dispatcher(Utils.immediateExecutor()).build();
server.enqueue(mockResponse("HeroAndFriendsNameResponse.json"));
CacheHeaders cacheHeaders = CacheHeaders.builder().addHeader(ApolloCacheHeaders.DO_NOT_STORE, "true").build();
Rx2Apollo.from(apolloClient.query(new HeroAndFriendsNamesQuery(Input.fromNullable(Episode.NEWHOPE))).cacheHeaders(cacheHeaders)).test();
assertThat(hasHeader.get()).isTrue();
}
use of com.apollographql.apollo.cache.normalized.Record in project apollo-android by apollographql.
the class ResponseNormalizationTest method testMergeNull.
@Test
public void testMergeNull() throws Exception {
Record record = Record.builder("Key").addField("field1", "value1").build();
normalizedCache.merge(Collections.singletonList(record), CacheHeaders.NONE);
Record newRecord = record.toBuilder().addField("field2", null).build();
normalizedCache.merge(Collections.singletonList(newRecord), CacheHeaders.NONE);
final Record finalRecord = normalizedCache.loadRecord(record.key(), CacheHeaders.NONE);
assertThat(finalRecord.hasField("field2")).isTrue();
normalizedCache.remove(CacheKey.from(record.key()));
}
use of com.apollographql.apollo.cache.normalized.Record in project apollo-android by apollographql.
the class ResponseNormalizationTest method testHeroParentTypeDependentFieldHuman.
@Test
public void testHeroParentTypeDependentFieldHuman() throws Exception {
assertHasNoErrors("HeroParentTypeDependentFieldHumanResponse.json", new HeroParentTypeDependentFieldQuery(Input.fromNullable(EMPIRE)));
Record lukeRecord = normalizedCache.loadRecord(TEST_FIELD_KEY_EMPIRE + ".friends.0", CacheHeaders.NONE);
assertThat(lukeRecord.field("name")).isEqualTo("Han Solo");
assertThat(lukeRecord.field("height({\"unit\":\"FOOT\"})")).isEqualTo(BigDecimal.valueOf(5.905512));
}
use of com.apollographql.apollo.cache.normalized.Record in project apollo-android by apollographql.
the class ResponseNormalizationTest method testHeroAndFriendsNamesQueryWithIDs.
@Test
public void testHeroAndFriendsNamesQueryWithIDs() throws Exception {
assertHasNoErrors("HeroAndFriendsNameWithIdsResponse.json", new HeroAndFriendsNamesWithIDsQuery(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("1000"), new CacheReference("1002"), new CacheReference("1003")));
final Record luke = normalizedCache.loadRecord("1000", CacheHeaders.NONE);
assertThat(luke.field("name")).isEqualTo("Luke Skywalker");
}
Aggregations