Slide 1

Slide 1 text

EXPLORING GRAPHQL WITH APOLLO Ubiratan Soares Agosto / 2017

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

by

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

IS NOT

Slide 7

Slide 7 text

vs

Slide 8

Slide 8 text

WE MUST TALK ABOUT REST

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

2000

Slide 12

Slide 12 text

2000

Slide 13

Slide 13 text

2017

Slide 14

Slide 14 text

2017

Slide 15

Slide 15 text

REST WAS NOT DESIGNED WITH NOWADAYS APPLICATIONS, DEVICES AND USERS IN MIND …

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Design Develop Measure API AS PRODUCT

Slide 18

Slide 18 text

KNOW ISSUES FROM REST

Slide 19

Slide 19 text

SCHEMA-LESS

Slide 20

Slide 20 text

VERSIONING

Slide 21

Slide 21 text

MULTIPLE ENDPOINTS TO SOLVE A SPECIFIC NEED FROM CLIENT

Slide 22

Slide 22 text

ENDPOINT EXPOSES MORE DATA THAN CLIENT NEEDS PER HTTP REQUEST

Slide 23

Slide 23 text

MAIN GOALS

Slide 24

Slide 24 text

Single Endpoint “Goodbye" versioning headaches 1

Slide 25

Slide 25 text

Well defined contract Client and server must respect a schema for effective data exchange 2

Slide 26

Slide 26 text

No more overfetching GraphQL clients can fetch only the data they really need for a given purpose 3

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

CORE CONCEPTS

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

TYPE SYSTEM AND SCHEMA

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Field name != Type name

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

QUERIES AND MUTATIONS

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

{ 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"

Slide 40

Slide 40 text

{ 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"

Slide 41

Slide 41 text

{ "data": { "getLocation": { "country": { "names": { "en": "Brazil" }, "geoname_id": "3469034", "iso_code": "BR" }, "location": { "latitude": "-23.5733", "longitude": "-46.6417" } } } } QUERY

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

HOW TO USE ON ANDROID ?

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

PREPARE YOUR QUERY OR MUTATION

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

query LocationQuery($ip: String!) { getLocation(ip: $ip) { country { names { en } geoname_id iso_code } location { latitude longitude } } } queryname.graphql

Slide 54

Slide 54 text

RETRIEVE THE ACTUAL SCHEMA

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

> brew install npm

Slide 57

Slide 57 text

> brew install npm > npm install -g apollo-codegen

Slide 58

Slide 58 text

> apollo-codegen download-schema \ https://api.graphloc.com/graphql \ --output schema.json > brew install npm > npm install -g apollo-codegen

Slide 59

Slide 59 text

PROJECT INTEGRATION

Slide 60

Slide 60 text

APPLY PLUGIN AND DEPENDENCIES

Slide 61

Slide 61 text

DEFINE YOUR WORK DIRECTORY

Slide 62

Slide 62 text

Define the package for generated classes Convention DO NOT forget to put schema here

Slide 63

Slide 63 text

class GraphQLAccess { private val apolloClient = ApolloClient .builder() .serverUrl("https://api.graphloc.com/graphql") .build() } // continue ... }

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

private fun toGeolocationInfo( dataResponse: Response, 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 ) }

Slide 69

Slide 69 text

private fun toGeolocationInfo( dataResponse: Response, 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

Slide 70

Slide 70 text

DONE

Slide 71

Slide 71 text

JUST ADD MORE QUERIES / MUTATIONS AND BE HAPPY !!!

Slide 72

Slide 72 text

FINAL REMARKS

Slide 73

Slide 73 text

FINAL REMARKS • GraphQL is just a spec

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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)

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

https://speakerdeck.com/ubiratansoares

Slide 79

Slide 79 text

UBIRATAN SOARES Computer Scientist by ICMC/USP Software Engineer, curious guy Google Developer Expert for Android Teacher, speaker, etc, etc

Slide 80

Slide 80 text

THANK YOU @ubiratanfsoares ubiratansoares.github.io https://br.linkedin.com/in/ubiratanfsoares