Here's your chance to speak at GraphQL Summit in New York City, October 8 - 10, 2024! 🏙️ Submit your proposal by May 31.
Launch GraphOS Studio
You're viewing documentation for an upcoming version of this software. Switch to the latest stable version.

4. Execute your first query

To execute your first , you need to create an instance of ApolloClient and make a new call with your query.

Create an ApolloClient

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

import com.apollographql.apollo3.ApolloClient
val apolloClient = ApolloClient.Builder()

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 to reuse the underlying OkHttp instance and the associated threadpools and connections. In a real app, you would probably want to use a dependency injection framework to share the ApolloClient.

Creating the ApolloClient is as simple as giving it the endpoint of your backend. It is in our case.

Execute the query

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

fun LaunchList(onLaunchClick: (launchId: String) -> Unit) {
LaunchedEffect(Unit) {
val response = apolloClient.query(LaunchListQuery()).execute()
Log.d("LaunchList", "Success ${}")

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

Note: for simplicity, we are executing the query 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:

2023-03-14 12:08:30.694 16611-16611/com.example.rocketreserver
D/LaunchList: Success Data(launches=Launches(launches=[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), Launch(id=90, site=CCAFS SLC 40)]))

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

Next, let's connect this data to your UI.

3. Write your first query
5. Connect your queries to your UI
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy