Inline Fragments
Apollo Android supports GraphQL inline fragments. They are not to be confused with regular fragments that are used to reused fields. Inline fragments are used to access polymorphic types:
GraphQL
Hero.graphql
1query HeroForEpisode($ep: Episode!) {
2 hero(episode: $ep) {
3 name
4 ... on Droid {
5 primaryFunction
6 }
7 ... on Human {
8 height
9 }
10 }
11}
The Hero class will contain AsDroid
and AsHuman
nested classes to access the Droid fields and human fields respectively:
Kotlin
1println("Droid primaryFunction: ${hero.asDroid?.primaryFunction}")
2println("Human height: ${hero.asHuman?.height}")