Search in sources :

Example 1 with WriteableStore

use of com.apollographql.apollo.internal.cache.normalized.WriteableStore 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();
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) ApolloException(com.apollographql.apollo.exception.ApolloException) Record(com.apollographql.apollo.cache.normalized.Record) WriteableStore(com.apollographql.apollo.internal.cache.normalized.WriteableStore) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Aggregations

Record (com.apollographql.apollo.cache.normalized.Record)1 ApolloException (com.apollographql.apollo.exception.ApolloException)1 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)1 WriteableStore (com.apollographql.apollo.internal.cache.normalized.WriteableStore)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 Nullable (javax.annotation.Nullable)1 Test (org.junit.Test)1