Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Exploring GraphQL on Android with Apollo

Exploring GraphQL on Android with Apollo

Presentation about GraphQL basics and how to integrate with GraphQL APIs on Android, using the Apollo Client.

Presented at Android DevConference 2017.

Ubiratan Soares

August 25, 2017
Tweet

More Decks by Ubiratan Soares

Other Decks in Programming

Transcript

  1. by

  2. vs

  3. No more overfetching GraphQL clients can fetch only the data

    they really need for a given purpose 3
  4. 4 No more underfetching GraphQL clients can retrieve all the

    data they need into a single HTTP call : no more chained and/or parallel HTTP requests
  5. QUERY (IP) LOCDATA city : city location : location LOCATION

    latitude : String longitude : String time_zone : String CITY geoname_id : String names : cityNames ACCEPTS RETURNS . . . . . . CITYNAMES de : String en : String ru : String . . .
  6. { getLocation(ip: “172.59.226.170") { country { names { en }

    geoname_id iso_code } location { latitude longitude } } } Operation Root Query parameter Data that actually matters
  7. { getLocation(ip: “172.59.226.170") { country { names { en }

    geoname_id iso_code } location { latitude longitude } } } "For the available data, return the English name for the country"
  8. { getLocation(ip: “172.59.226.170") { country { names { en }

    geoname_id iso_code } location { latitude longitude } } } "For the available data, return the latitude and the longitude from the geolocation info"
  9. { "data": { "getLocation": { "country": { "names": { "en":

    "Brazil" }, "geoname_id": "3469034", "iso_code": "BR" }, "location": { "latitude": "-23.5733", "longitude": "-46.6417" } } } } QUERY
  10. MORE ABOUT OPERATIONS • Data payload will mirror the type

    hierarchy defined on schema • All data types for queries/mutations must be defined on schema • Operation can accept parameters and/or variables • We can form better, readable blocks for operations using aliases and fragments • Etc
  11. CLIENT INTENT REST + HTTP GraphQL + HTTP GraphQL Operation

    READ RESOURCE GET POST QUERY CREATE RESOURCE POST POST MUTATION UPDATE RESOURCE PUT POST MUTATION REMOVE RESOURCE DELETE POST MUTATION
  12. APOLLO + ANDROID • Full-featured tooling + library, inspired by

    Retrofit • Custom OkHttpClient support • Code generation for queries / mutations : request models and payloads • Cache-ready client, including L0, L1 e L2 support • RxJava support • Etc
  13. query LocationQuery ($ip: String!) { getLocation(ip: $ip) { country {

    names { en } geoname_id iso_code } location { latitude longitude } } } Generated class member To-be-generated class name Operation Type
  14. query LocationQuery($ip: String!) { getLocation(ip: $ip) { country { names

    { en } geoname_id iso_code } location { latitude longitude } } } queryname.graphql
  15. fun fetchGeolocation(): Observable<GeolocationInformation> { val ipAddress = "8.8.8.8" val locationQuery

    = LocationQuery.builder() .ip(ipAddress) .build() val call = apolloClient.query<Data, Data, Variables>(locationQuery) return Rx2Apollo .from<Data>(call) .map { data -> toGeolocationInfo(data, ipAddress) } }
  16. fun fetchGeolocation(): Observable<GeolocationInformation> { val ipAddress = "8.8.8.8" val locationQuery

    = LocationQuery.builder() .ip(ipAddress) .build() val call = apolloClient.query<Data, Data, Variables>(locationQuery) return Rx2Apollo .from<Data>(call) .map { data -> toGeolocationInfo(data, ipAddress) } }
  17. fun fetchGeolocation(): Observable<GeolocationInformation> { val ipAddress = "8.8.8.8" val locationQuery

    = LocationQuery.builder() .ip(ipAddress) .build() val call = apolloClient.query<Data, Data, Variables>(locationQuery) return Rx2Apollo .from<Data>(call) .map { data -> toGeolocationInfo(data, ipAddress) } }
  18. fun fetchGeolocation(): Observable<GeolocationInformation> { val ipAddress = "8.8.8.8" val locationQuery

    = LocationQuery.builder() .ip(ipAddress) .build() val call = apolloClient.query<Data, Data, Variables>(locationQuery) return Rx2Apollo .from<Data>(call) .map { data -> toGeolocationInfo(data, ipAddress) } }
  19. private fun toGeolocationInfo( dataResponse: Response<Data>, ipAddress: String): GeolocationInformation { val

    data = dataResponse.data()?.getLocation val location = data?.location return GeolocationInformation( ip = ipAddress, countryName = data?.country?.names?.en, cityName = data?.city?.names?.en, latitude = location?.latitude, longitutde = location?.longitude, timezone = location?.time_zone ) }
  20. private fun toGeolocationInfo( dataResponse: Response<Data>, ipAddress: String): GeolocationInformation { val

    data = dataResponse.data()?.getLocation val location = data?.location return GeolocationInformation( ip = ipAddress, countryName = data?.country?.names?.en, cityName = data?.city?.names?.en, latitude = location?.latitude, longitutde = location?.longitude, timezone = location?.time_zone ) } Generated by Apollo
  21. FINAL REMARKS • GraphQL is just a spec • GraphQL

    aims to solve some of well know issues from REST
  22. FINAL REMARKS • GraphQL is just a spec • GraphQL

    aims to solve some of well know issues from REST • GraphQL is mobile friendly, but it brings its own set of challenges (for free, rs)
  23. FINAL REMARKS • GraphQL is just a spec • GraphQL

    aims to solve some of well know issues from REST • GraphQL is mobile friendly, but it brings its own set of challenges (for free, rs) • Apollo is an OSS effort for easy integration of GraphQL clients, and it has a full-featured Android native API
  24. REFERENCES Oficial GraphQL Website http://graphql.org How to GraphQL https://www.howtographql.com Github

    Training on GraphQL https://services.github.com/on-demand/graphql Apollo Client for Android - Documentation http://dev.apollodata.com/android
  25. UBIRATAN SOARES Computer Scientist by ICMC/USP Software Engineer, curious guy

    Google Developer Expert for Android Teacher, speaker, etc, etc