5. Execute your first query
5m

Overview

Let's test out our and validate that it can return data from our remote .

In this lesson we will:

  • Incorporate ApolloClient into our app
  • Add some temporary code to the LaunchListViewModel to show how to execute a

Create an ApolloClient

Create a new file named Apollo.kt in the com.example.rocketreserver package and create an instance of ApolloClient in it:

app/src/main/kotlin/com/example/rocketreserver/Apollo.kt
import com.apollographql.apollo.ApolloClient
val apolloClient = ApolloClient.Builder()
.serverUrl("https://apollo-fullstack-tutorial.herokuapp.com/graphql")
.build()

Note: apolloClient is a top-level so that it can be reused from other parts of the app later on for better performance. Reusing the apolloClient allows you to reuse the underlying OkHttp instance and the associated threadpools and connections. In a production app, you would probably want to use a dependency injection framework to share the ApolloClient.

Creating ApolloClient is as simple as giving it the endpoint of your backend. It is https://apollo-fullstack-tutorial.herokuapp.com/graphql in our case.

Execute the query

Open LaunchList.kt and update the contents of the LaunchList composable function. Use apolloClient and the generated LaunchListQuery to execute a new :

app/src/main/kotlin/com/example/rocketreserver/LaunchList.kt
@Composable
fun LaunchList(onLaunchClick: (launchId: String) -> Unit) {
LaunchedEffect(Unit) {
val response = apolloClient.query(LaunchListQuery()).execute()
Log.d("LaunchList", "Success ${response.data}")
}
LazyColumn(modifier = Modifier.fillMaxSize()) {
// ...
}
}

Thanks to Kotlin coroutines, the will be executed in a background thread even if the code looks synchronous.

Note: for simplicity, we are executing the in a LaunchedEffect block. In a real app, you may want to do this in a ViewModel and otherwise use a layered architecture.

Test your query

Hit run. You should see this in the logcat output:

(logcat)
Success Data(launches=Launches(cursor=1583556631, hasMore=true, launches=[Launch(id=110, site=KSC LC 39A), Launch(id=109, site=CCAFS SLC 40), Launch(id=108, site=VAFB SLC 4E), Launch(id=107, site=KSC LC 39A), Launch(id=106, site=CCAFS SLC 40), Launch(id=105, site=CCAFS SLC 40), Launch(id=104, site=KSC LC 39A), Launch(id=103, site=KSC LC 39A), Launch(id=102, site=KSC LC 39A), Launch(id=101, site=CCAFS SLC 40), Launch(id=100, site=CCAFS SLC 40), Launch(id=99, site=KSC LC 39A), Launch(id=98, site=CCAFS SLC 40), Launch(id=97, site=CCAFS SLC 40), Launch(id=96, site=CCAFS SLC 40), Launch(id=95, site=CCAFS SLC 40), Launch(id=94, site=KSC LC 39A), Launch(id=93, site=KSC LC 39A), Launch(id=92, site=KSC LC 39A), Launch(id=91, site=CCAFS SLC 40)]))

Android Studio with logcat output beginning with "Success Data"

This means the request was correctly executed and you now have a list of launch sites 🚀🚀🚀

Task!

Up next

Next, let's connect this data to our UI 🚀

Previous

Share your questions and comments about this lesson

This course is currently in

beta
. Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.

You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.