Docs
Launch GraphOS Studio
You're viewing documentation for a previous version of this software. Switch to the latest stable version.

Fragments


Apollo Android supports GraphQL fragments. allow you to define a set of that can be reusable in your queries.

Launch.graphql
fragment launchFragment on Launch {
id
site
mission {
name
}
}
query LaunchDetails($id:ID!) {
launch(id: $id) {
...launchFragment
}
}

Apollo Android will generate a LaunchFragment class that can be reused in different queries:

LaunchFragment.kt
data class LaunchFragment(
val __typename: String = "Launch",
val id: String,
val site: String?,
val mission: Mission?
)

Your generated models will have a .fragments property to access the :

println("Mission site: ${launch.fragments.launchFragment.site}")

To reuse a , use it in any other :

Launch.graphql
// ...
query LaunchList {
launches {
launches {
...launchFragment
}
}
}

You can define your fragment in any .graphql file. The compiler merges all .graphql files so it doesn't matter if you have multiple files or put everything in the same file.

Previous
Using without apollo-runtime
Next
Inline Fragments
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company