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.

D4b7a3e2ed10f86e0b52498713ba2601?s=128

Ubiratan Soares

August 25, 2017
Tweet

Transcript

  1. 2.
  2. 3.

    by

  3. 4.
  4. 5.
  5. 6.
  6. 7.

    vs

  7. 9.
  8. 10.
  9. 11.
  10. 12.
  11. 13.
  12. 14.
  13. 16.
  14. 26.

    No more overfetching GraphQL clients can fetch only the data

    they really need for a given purpose 3
  15. 27.

    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
  16. 29.
  17. 31.
  18. 32.
  19. 33.
  20. 34.
  21. 36.

    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 . . .
  22. 38.

    { getLocation(ip: “172.59.226.170") { country { names { en }

    geoname_id iso_code } location { latitude longitude } } } Operation Root Query parameter Data that actually matters
  23. 39.

    { 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"
  24. 40.

    { 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"
  25. 41.

    { "data": { "getLocation": { "country": { "names": { "en":

    "Brazil" }, "geoname_id": "3469034", "iso_code": "BR" }, "location": { "latitude": "-23.5733", "longitude": "-46.6417" } } } } QUERY
  26. 42.
  27. 43.
  28. 44.

    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
  29. 45.

    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
  30. 47.
  31. 48.
  32. 49.

    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
  33. 50.
  34. 52.

    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
  35. 53.

    query LocationQuery($ip: String!) { getLocation(ip: $ip) { country { names

    { en } geoname_id iso_code } location { latitude longitude } } } queryname.graphql
  36. 55.
  37. 64.

    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) } }
  38. 65.

    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) } }
  39. 66.

    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) } }
  40. 67.

    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) } }
  41. 68.

    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 ) }
  42. 69.

    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
  43. 70.
  44. 74.

    FINAL REMARKS • GraphQL is just a spec • GraphQL

    aims to solve some of well know issues from REST
  45. 75.

    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)
  46. 76.

    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
  47. 77.

    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
  48. 79.

    UBIRATAN SOARES Computer Scientist by ICMC/USP Software Engineer, curious guy

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