use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class NormalizedCacheTestCase method masterDetailFailIncomplete.
@Test
public void masterDetailFailIncomplete() throws Exception {
enqueueAndAssertResponse(server, "HeroAndFriendsNameWithIdsResponse.json", apolloClient.query(new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(Episode.NEWHOPE))).responseFetcher(NETWORK_ONLY), new Predicate<Response<HeroAndFriendsNamesWithIDsQuery.Data>>() {
@Override
public boolean test(Response<HeroAndFriendsNamesWithIDsQuery.Data> response) throws Exception {
return !response.hasErrors();
}
});
assertResponse(apolloClient.query(new CharacterDetailsQuery("1002")).responseFetcher(CACHE_ONLY), new Predicate<Response<CharacterDetailsQuery.Data>>() {
@Override
public boolean test(Response<CharacterDetailsQuery.Data> response) throws Exception {
assertThat(response.data()).isNull();
return true;
}
});
}
use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class Rx2ApolloTest method queryWatcherNotUpdatedSameQuerySameResults.
@Test
@SuppressWarnings("CheckReturnValue")
public void queryWatcherNotUpdatedSameQuerySameResults() throws Exception {
server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
TestObserver<EpisodeHeroNameQuery.Data> observer = new TestObserver<>();
Rx2Apollo.from(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).watcher()).map(new Function<Response<EpisodeHeroNameQuery.Data>, EpisodeHeroNameQuery.Data>() {
@Override
public EpisodeHeroNameQuery.Data apply(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
return response.data();
}
}).subscribeWith(observer);
server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).responseFetcher(NETWORK_ONLY).enqueue(null);
observer.assertValueCount(1).assertValueAt(0, new Predicate<EpisodeHeroNameQuery.Data>() {
@Override
public boolean test(EpisodeHeroNameQuery.Data data) throws Exception {
assertThat(data.hero().name()).isEqualTo("R2-D2");
return true;
}
});
}
use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class Rx2ApolloTest method queryWatcherUpdatedDifferentQueryDifferentResults.
@Test
@SuppressWarnings("CheckReturnValue")
public void queryWatcherUpdatedDifferentQueryDifferentResults() throws Exception {
server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
TestObserver<EpisodeHeroNameQuery.Data> observer = new TestObserver<>();
Rx2Apollo.from(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).watcher()).map(new Function<Response<EpisodeHeroNameQuery.Data>, EpisodeHeroNameQuery.Data>() {
@Override
public EpisodeHeroNameQuery.Data apply(Response<EpisodeHeroNameQuery.Data> response) throws Exception {
return response.data();
}
}).subscribeWith(observer);
server.enqueue(mockResponse("HeroAndFriendsNameWithIdsNameChange.json"));
apolloClient.query(new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(NEWHOPE))).enqueue(null);
observer.assertValueCount(2).assertValueAt(0, new Predicate<EpisodeHeroNameQuery.Data>() {
@Override
public boolean test(EpisodeHeroNameQuery.Data data) throws Exception {
assertThat(data.hero().name()).isEqualTo("R2-D2");
return true;
}
}).assertValueAt(1, new Predicate<EpisodeHeroNameQuery.Data>() {
@Override
public boolean test(EpisodeHeroNameQuery.Data data) throws Exception {
assertThat(data.hero().name()).isEqualTo("Artoo");
return true;
}
});
}
use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class QueryRefetchTest method refetchWatchers.
@Test
@SuppressWarnings("CheckReturnValue")
public void refetchWatchers() throws Exception {
server.enqueue(mockResponse("ReviewsEmpireEpisodeResponse.json"));
server.enqueue(mockResponse("CreateReviewResponse.json"));
server.enqueue(mockResponse("ReviewsEmpireEpisodeResponseUpdated.json"));
final AtomicReference<Response<ReviewsByEpisodeQuery.Data>> empireReviewsWatchResponse = new AtomicReference<>();
ApolloQueryWatcher<ReviewsByEpisodeQuery.Data> queryWatcher = apolloClient.query(new ReviewsByEpisodeQuery(Episode.EMPIRE)).watcher().refetchResponseFetcher(NETWORK_FIRST).enqueueAndWatch(new ApolloCall.Callback<ReviewsByEpisodeQuery.Data>() {
@Override
public void onResponse(@Nonnull Response<ReviewsByEpisodeQuery.Data> response) {
empireReviewsWatchResponse.set(response);
}
@Override
public void onFailure(@Nonnull ApolloException e) {
}
});
CreateReviewMutation mutation = new CreateReviewMutation(Episode.EMPIRE, ReviewInput.builder().stars(5).commentary("Awesome").favoriteColor(ColorInput.builder().build()).build());
Rx2Apollo.from(apolloClient.mutate(mutation).refetchQueries(queryWatcher.operation().name())).test();
assertThat(server.getRequestCount()).isEqualTo(3);
Response<ReviewsByEpisodeQuery.Data> empireReviewsQueryResponse = empireReviewsWatchResponse.get();
assertThat(empireReviewsQueryResponse.data().reviews()).hasSize(4);
assertThat(empireReviewsQueryResponse.data().reviews().get(3).stars()).isEqualTo(5);
assertThat(empireReviewsQueryResponse.data().reviews().get(3).commentary()).isEqualTo("Awesome");
queryWatcher.cancel();
}
use of com.apollographql.apollo.api.Response in project apollo-android by apollographql.
the class RxApollo method from.
/**
* Converts an {@link ApolloCall} to a Observable. The number of emissions this Observable will have is based on the
* {@link ResponseFetcher} used with the call.
*
* @param call the ApolloCall to convert
* @param <T> the value type
* @param backpressureMode The {@link rx.Emitter.BackpressureMode} to use.
* @return the converted Observable
*/
@Nonnull
public static <T> Observable<Response<T>> from(@Nonnull final ApolloCall<T> call, Emitter.BackpressureMode backpressureMode) {
checkNotNull(call, "call == 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);
call.cancel();
}
});
call.enqueue(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);
}
}
@Override
public void onStatusEvent(@Nonnull ApolloCall.StatusEvent event) {
if (!canceled.get()) {
if (event == ApolloCall.StatusEvent.COMPLETED) {
emitter.onCompleted();
}
}
}
});
}
}, backpressureMode);
}
Aggregations