Join us from October 8-10 in New York City to learn the latest tips, trends, and news about GraphQL federation and API platform engineering.Join us for GraphQL Summit 2024 in NYC
Docs
Start for Free
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
Rate articleRateEdit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company