Skip to content

Commit

Permalink
Fix issue with query watcher for empty cache (#915)
Browse files Browse the repository at this point in the history
If cache is empty re-fetch any watcher that has empty dependent key set.

Closes #822
  • Loading branch information
sav007 authored and digitalbuddha committed May 13, 2018
1 parent 06be422 commit 283a43f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.apollographql.apollo;

import com.apollographql.apollo.api.Input;
import com.apollographql.apollo.api.Response;
import com.apollographql.apollo.api.internal.Optional;
import com.apollographql.apollo.cache.CacheHeaders;
Expand Down Expand Up @@ -40,6 +41,7 @@
import static com.apollographql.apollo.fetcher.ApolloResponseFetchers.CACHE_ONLY;
import static com.apollographql.apollo.fetcher.ApolloResponseFetchers.NETWORK_ONLY;
import static com.google.common.truth.Truth.assertThat;
import static junit.framework.Assert.fail;

public class ApolloWatcherTest {
private ApolloClient apolloClient;
Expand Down Expand Up @@ -338,4 +340,39 @@ public void testQueryWatcherNotCalled_WhenCanceled() throws Exception {
assertThat(heroNameList.get(0)).isEqualTo("R2-D2");
assertThat(heroNameList.size()).isEqualTo(1);
}

@Test
public void emptyCacheQueryWatcherCacheOnly() throws Exception {
final List<EpisodeHeroNameQuery.Hero> watchedHeroes = new ArrayList<>();
EpisodeHeroNameQuery query = new EpisodeHeroNameQuery(Input.fromNullable(Episode.EMPIRE));
apolloClient.query(query)
.responseFetcher(CACHE_ONLY)
.watcher()
.enqueueAndWatch(new ApolloCall.Callback<EpisodeHeroNameQuery.Data>() {
@Override public void onResponse(Response<EpisodeHeroNameQuery.Data> response) {
if (response.data() != null) {
watchedHeroes.add(response.data().hero());
}
}

@Override public void onFailure(ApolloException e) {
fail(e.getMessage());
}
});

server.enqueue(mockResponse("EpisodeHeroNameResponseWithId.json"));
apolloClient.query(query).enqueue(new ApolloCall.Callback<EpisodeHeroNameQuery.Data>() {
@Override public void onResponse(Response<EpisodeHeroNameQuery.Data> response) {
assertThat(response.data()).isNotNull();
assertThat(response.data().hero()).isNotNull();
}

@Override public void onFailure(ApolloException e) {
fail(e.getMessage());
}
});

assertThat(watchedHeroes).hasSize(1);
assertThat(watchedHeroes.get(0).name()).isEqualTo("R2-D2");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ public Set<String> mergeWith(Record otherRecord) {
return changedKeys;
}

/**
* @return A set of all field keys. A field key incorporates any GraphQL arguments in addition to the field name.
*/
public Set<String> keys() {
Set<String> keys = new HashSet<>();
for (Map.Entry<String, Object> field : fields.entrySet()) {
keys.add(key() + "." + field.getKey());
}
return keys;
}

/**
* @return A map of fieldName to fieldValue. Where fieldValue is a GraphQL Scalar or {@link CacheReference} if it is a
* GraphQL Object type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected Set<String> performMerge(@Nonnull final Record apolloRecord, @Nonnull
final Record oldRecord = lruCache.getIfPresent(apolloRecord.key());
if (oldRecord == null) {
lruCache.put(apolloRecord.key(), apolloRecord);
return Collections.emptySet();
return apolloRecord.keys();
} else {
Set<String> changedKeys = oldRecord.mergeWith(apolloRecord);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class RealApolloQueryWatcher<T> implements ApolloQueryWatcher<T> {
private final ApolloCallTracker tracker;
final ApolloStore.RecordChangeSubscriber recordChangeSubscriber = new ApolloStore.RecordChangeSubscriber() {
@Override public void onCacheRecordsChanged(Set<String> changedRecordKeys) {
if (!Utils.areDisjoint(dependentKeys, changedRecordKeys)) {
if (dependentKeys.isEmpty() || !Utils.areDisjoint(dependentKeys, changedRecordKeys)) {
refetch();
}
}
Expand Down

0 comments on commit 283a43f

Please sign in to comment.