use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class QueryRefetchTest method refetchNoPreCachedQuery.
@Test
@SuppressWarnings("CheckReturnValue")
public void refetchNoPreCachedQuery() throws Exception {
CreateReviewMutation mutation = new CreateReviewMutation(Episode.EMPIRE, ReviewInput.builder().stars(5).commentary("Awesome").favoriteColor(ColorInput.builder().build()).build());
server.enqueue(mockResponse("CreateReviewResponse.json"));
server.enqueue(mockResponse("ReviewsEmpireEpisodeResponse.json"));
RealApolloCall call = (RealApolloCall) apolloClient.mutate(mutation).refetchQueries(new ReviewsByEpisodeQuery(Episode.EMPIRE));
Rx2Apollo.from(call).test();
assertThat(server.getRequestCount()).isEqualTo(2);
assertResponse(apolloClient.query(new ReviewsByEpisodeQuery(Episode.EMPIRE)).responseFetcher(CACHE_ONLY), new Predicate<Response<ReviewsByEpisodeQuery.Data>>() {
@Override
public boolean test(Response<ReviewsByEpisodeQuery.Data> response) throws Exception {
assertThat(response.data().reviews()).hasSize(3);
assertThat(response.data().reviews().get(2).stars()).isEqualTo(5);
assertThat(response.data().reviews().get(2).commentary()).isEqualTo("Amazing");
return true;
}
});
}
use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class QueryRefetchTest method refetchPreCachedQuery.
@Test
@SuppressWarnings("CheckReturnValue")
public void refetchPreCachedQuery() throws Exception {
enqueueAndAssertResponse(server, "ReviewsEmpireEpisodeResponse.json", apolloClient.query(new ReviewsByEpisodeQuery(Episode.EMPIRE)).responseFetcher(NETWORK_FIRST), new Predicate<Response<ReviewsByEpisodeQuery.Data>>() {
@Override
public boolean test(Response<ReviewsByEpisodeQuery.Data> response) throws Exception {
return !response.hasErrors();
}
});
assertResponse(apolloClient.query(new ReviewsByEpisodeQuery(Episode.EMPIRE)).responseFetcher(CACHE_ONLY), new Predicate<Response<ReviewsByEpisodeQuery.Data>>() {
@Override
public boolean test(Response<ReviewsByEpisodeQuery.Data> response) throws Exception {
assertThat(response.data().reviews()).hasSize(3);
assertThat(response.data().reviews().get(2).stars()).isEqualTo(5);
assertThat(response.data().reviews().get(2).commentary()).isEqualTo("Amazing");
return true;
}
});
CreateReviewMutation mutation = new CreateReviewMutation(Episode.EMPIRE, ReviewInput.builder().stars(5).commentary("Awesome").favoriteColor(ColorInput.builder().build()).build());
server.enqueue(mockResponse("CreateReviewResponse.json"));
server.enqueue(mockResponse("ReviewsEmpireEpisodeResponseUpdated.json"));
RealApolloCall call = (RealApolloCall) apolloClient.mutate(mutation).refetchQueries(new ReviewsByEpisodeQuery(Episode.EMPIRE));
Rx2Apollo.from(call).test();
assertThat(server.getRequestCount()).isEqualTo(3);
assertResponse(apolloClient.query(new ReviewsByEpisodeQuery(Episode.EMPIRE)).responseFetcher(CACHE_ONLY), new Predicate<Response<ReviewsByEpisodeQuery.Data>>() {
@Override
public boolean test(Response<ReviewsByEpisodeQuery.Data> response) throws Exception {
assertThat(response.data().reviews()).hasSize(4);
assertThat(response.data().reviews().get(3).stars()).isEqualTo(5);
assertThat(response.data().reviews().get(3).commentary()).isEqualTo("Awesome");
return true;
}
});
}
use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class ApolloParseInterceptor method parse.
@SuppressWarnings("unchecked")
InterceptorResponse parse(Operation operation, okhttp3.Response httpResponse) throws ApolloHttpException, ApolloParseException {
String cacheKey = httpResponse.request().header(HttpCache.CACHE_KEY_HEADER);
if (httpResponse.isSuccessful()) {
try {
OperationResponseParser parser = new OperationResponseParser(operation, responseFieldMapper, scalarTypeAdapters, normalizer);
Response parsedResponse = parser.parse(httpResponse.body().source()).toBuilder().fromCache(httpResponse.cacheResponse() != null).build();
if (parsedResponse.hasErrors() && httpCache != null) {
httpCache.removeQuietly(cacheKey);
}
return new InterceptorResponse(httpResponse, parsedResponse, normalizer.records());
} catch (Exception rethrown) {
logger.e(rethrown, "Failed to parse network response for operation: %s", operation);
closeQuietly(httpResponse);
if (httpCache != null) {
httpCache.removeQuietly(cacheKey);
}
throw new ApolloParseException("Failed to parse http response", rethrown);
}
} else {
logger.e("Failed to parse network response: %s", httpResponse);
throw new ApolloHttpException(httpResponse);
}
}
use of com.apollographql.apollo.api.Response 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.api.Response 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