33
loading...
This website collects cookies to deliver better user experience
build.gradle
:buildscript {
repositories {
...
}
dependencies {
classpath "com.apollographql.apollo:apollo-gradle-plugin:2.4.1"
...
}
}
allprojects {
repositories {
...
// RxJava repo
maven { url "https://oss.jfrog.org/libs-snapshot" }
}
}
build.gradle
:dependencies {
...
// Apollo
implementation "com.apollographql.apollo:apollo-runtime:2.4.1"
implementation "com.apollographql.apollo:apollo-rx3-support:2.4.1"
// RxJava
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
}
apply plugin: "com.apollographql.apollo"
apply plugin: “com.apollographql.apollo”
at the end of your gradle file, otherwise you’ll run into compiler issues later.src/main/graphql/com/example
, but replace example with your own package name. After that, run the following command to fetch a GraphQL schema:./gradlew downloadApolloSchema
--endpoint="https://api.spacex.land/graphql/"
--schema="src/main/graphql/com/example/schema.json"
Replace ./gradlew
with gradlew.bat
if you're on Windows.
schema.json
for us. Next we'll write our GraphQL query, which we’ll use to fetch a list of rockets. Create a file called rockets.graphql
in the same directory as schema.json
and paste the following:query RocketsQuery {
rockets {
id
name
description
}
}
Server
. Here's the shell of that class:class Server {
private static ApolloClient apolloClient;
private static ApolloClient getApolloClient() {
if (apolloClient == null) {
//Build the Apollo Client
String serverUrl = "https://api.spacex.land/graphql/";
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
apolloClient = ApolloClient.builder()
.serverUrl(serverUrl)
.okHttpClient(okHttpClient)
.build();
}
return apolloClient;
}
}
ApolloClient
instance. We'll use this helper method in our queries so we don't have to create a client with every query.rockets
query:public static Observable<Response<RocketsQuery.Data>> fetchRockets() {
ApolloQueryCall<RocketsQuery.Data> call = getApolloClient()
.query(new RocketsQuery());
return Rx3Apollo.from(call)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.filter((dataResponse -> dataResponse.getData() != null));
}
ApolloQueryCall
object with RocketsQuery
, the class that was generated from the GraphQL file we wrote earlier. This object defines the GraphQL query we want to make against the SpaceX API.Rx3Apollo
to convert the call into an RxJava object, which lets us do all our Rx magic on it. subscribeOn
specifies that we want this request to be run on a background thread. With observeOn
, we say that we want to handle the response on the main thread because we'll update the UI using the response. Finally, we add a filter to toss out null responses to prevent any null pointer exceptions.Observable
that we can then use in our Activity code to handle loading and error states and update the UI when we get a response.fetchRockets
method we created to populate a list:// Show loading
ProgressBar progressBar = findViewById(R.id.main_progressBar);
progressBar.setVisibility(View.VISIBLE);
Server.fetchRockets().subscribeWith(new DisposableObserver<Response<RocketsQuery.Data>>() {
@Override
public void onNext(@NonNull Response<RocketsQuery.Data> dataResponse) {
if (dataResponse.getData() != null) {
RocketsAdapter adapter = new RocketsAdapter(dataResponse.getData().rockets(),MainActivity.this);
recyclerView.setAdapter(adapter);
}
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
// Query's done, so hide loading
progressBar.setVisibility(View.GONE);
}
});
Server
class to do a rockets query and create a DisposableObserver
to handle the result. This is one of the benefits of using RxJava with Apollo Android. DisposableObserver
has three methods we can use to handle the result: onNext
, onError
, and onComplete
. onNext
is called when a valid response is received, and onError
is called if an error occurs. Regardless of the outcome, onComplete
is called once the request is completed. This lets us define our loading, error, and data states separately, which greatly simplifies our code. In this snippet, we attach the rockets to a custom adapter(code here) and then hide the loading indicator. Notice that the loading part is handled in onComplete
, because we want to hide it even if an error occurs.