Fragments
Apollo Android supports GraphQL fragments. Fragments allow you to define a set of fields 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 fragments:
println("Mission site: ${launch.fragments.launchFragment.site}")
To reuse a fragment, use it in any other query:
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.