use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class ApolloWatcherTest method testQueryWatcherUpdated_DifferentQuery_DifferentResults.
@Test
public void testQueryWatcherUpdated_DifferentQuery_DifferentResults() throws Exception {
final List<String> heroNameList = new ArrayList<>();
server.enqueue(mockResponse("EpisodeHeroNameResponseWithId.json"));
EpisodeHeroNameQuery query = EpisodeHeroNameQuery.builder().episode(Episode.EMPIRE).build();
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());
}
});
HeroAndFriendsNamesWithIDsQuery friendsQuery = HeroAndFriendsNamesWithIDsQuery.builder().episode(Episode.NEWHOPE).build();
enqueueAndAssertResponse(server, "HeroAndFriendsNameWithIdsNameChange.json", apolloClient.query(friendsQuery).responseFetcher(NETWORK_ONLY), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {
@Override
public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
return !response.hasErrors();
}
});
watcher.cancel();
assertThat(heroNameList.get(0)).isEqualTo("R2-D2");
assertThat(heroNameList.get(1)).isEqualTo("Artoo");
}
use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class HttpCacheTest method cacheNetworkError.
@Test
public void cacheNetworkError() throws IOException, ApolloException {
server.enqueue(new MockResponse().setResponseCode(504).setBody(""));
Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery())).test().assertError(Exception.class);
checkNoCachedResponse();
enqueueResponse("/HttpCacheTestAllPlanets.json");
Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery())).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {
@Override
public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
return !response.hasErrors();
}
});
checkCachedResponse("/HttpCacheTestAllPlanets.json");
Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery()).httpCachePolicy(HttpCachePolicy.CACHE_ONLY)).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {
@Override
public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
return !response.hasErrors();
}
});
}
use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class QueryReFetcher method refetchQueries.
private void refetchQueries() {
final OnCompleteCallback completeCallback = onCompleteCallback;
final AtomicInteger callsLeft = new AtomicInteger(calls.size());
for (final RealApolloCall call : calls) {
// noinspection unchecked
call.enqueue(new ApolloCall.Callback() {
@Override
public void onResponse(@Nonnull Response response) {
if (callsLeft.decrementAndGet() == 0 && completeCallback != null) {
completeCallback.onFetchComplete();
}
}
@Override
public void onFailure(@Nonnull ApolloException e) {
if (logger != null) {
logger.e(e, "Failed to fetch query: %s", call.operation);
}
if (callsLeft.decrementAndGet() == 0 && completeCallback != null) {
completeCallback.onFetchComplete();
}
}
});
}
}
use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class ApolloStoreOperation method enqueue.
/**
* Schedules operation to be executed in dispatcher
*
* @param callback to be notified about operation result
*/
public void enqueue(@Nullable final Callback<T> callback) {
checkIfExecuted();
this.callback.set(callback);
dispatcher.execute(new Runnable() {
@Override
public void run() {
T result;
try {
result = perform();
} catch (Exception e) {
notifyFailure(new ApolloException("Failed to perform store operation", e));
return;
}
notifySuccess(result);
}
});
}
use of com.apollographql.apollo.exception.ApolloException in project apollo-android by apollographql.
the class RxApollo method from.
/**
* Converts an {@link ApolloQueryWatcher} into an Observable.
*
* @param watcher the ApolloQueryWatcher to convert
* @param backpressureMode the back pressure strategy to apply to the observable source.
* @param <T> the value type
* @return the converted Observable
*/
@Nonnull
public static <T> Observable<Response<T>> from(@Nonnull final ApolloQueryWatcher<T> watcher, @Nonnull Emitter.BackpressureMode backpressureMode) {
checkNotNull(backpressureMode, "backpressureMode == null");
checkNotNull(watcher, "watcher == null");
return Observable.create(new Action1<Emitter<Response<T>>>() {
@Override
public void call(final Emitter<Response<T>> emitter) {
final AtomicBoolean canceled = new AtomicBoolean();
emitter.setCancellation(new Cancellable() {
@Override
public void cancel() throws Exception {
canceled.set(true);
watcher.cancel();
}
});
watcher.enqueueAndWatch(new ApolloCall.Callback<T>() {
@Override
public void onResponse(@Nonnull Response<T> response) {
if (!canceled.get()) {
emitter.onNext(response);
}
}
@Override
public void onFailure(@Nonnull ApolloException e) {
Exceptions.throwIfFatal(e);
if (!canceled.get()) {
emitter.onError(e);
}
}
});
}
}, backpressureMode);
}
Aggregations