Slide 1

Slide 1 text

Du GraphQL 100% Kotlin Avec Apollo-Android Talking.kt - 2020 1

Slide 2

Slide 2 text

Du GraphQL 100% Kotlin avec Apollo-Android @martinbonnin 2

Slide 3

Slide 3 text

Kotlin 3 Graphql Moderne Typage fort Null safe Des outils Moderne Typage fort Null safe Des outils ❤

Slide 4

Slide 4 text

Moderne 4 ● Query Language ● 2012: Facebook ● 2015: Release publique ● 2018: Linux Foundation ● https://github.com/graphql/graphql-spec

Slide 5

Slide 5 text

● Objets ● Scalaires ○ Boolean ○ Int ○ Float ○ String Typage fort 5 type User { login: String bio: String isBountyHunter: Boolean status: UserStatus issueComments: List } User Github

Slide 6

Slide 6 text

● Objets ● Scalaires ○ Boolean ○ Int ○ Float ○ String Typage fort 6 type User { login: String bio: String isBountyHunter: Boolean status: UserStatus issueComments: List } User Github

Slide 7

Slide 7 text

● Objets ● Scalaires ○ Boolean ○ Int ○ Float ○ String Typage fort 7 type User { login: String bio: String isBountyHunter: Boolean status: UserStatus issueComments: List } User Github

Slide 8

Slide 8 text

● Objets ● Scalaires ○ Boolean ○ Int ○ Float ○ String Typage fort 8 type User { login: String bio: String isBountyHunter: Boolean status: UserStatus issueComments: List } User Github

Slide 9

Slide 9 text

● Objets ● Scalaires ○ Boolean ○ Int ○ Float ○ String Typage fort 9 type User { login: String bio: String isBountyHunter: Boolean status: UserStatus issueComments: List } User Github

Slide 10

Slide 10 text

● Mais aussi: ○ Interfaces ○ Listes ○ Enums ○ Unions Typage fort 10 type User implements Node { id: ID! login: String bio: String [...] } User Github

Slide 11

Slide 11 text

Null safety ❗ 11 type User { login: String❗ bio: String isBountyHunter: Boolean❗ status: UserStatus issueComments: List❗ }

Slide 12

Slide 12 text

Schema 12 { "data" { "__schema": { "types": [ { "kind": "OBJECT", "name": "User", "description": "A user is an individual..", "fields": [...], "interfaces": [...], }, [...] ] } }

Slide 13

Slide 13 text

13 OUTILS https://apis.guru/graphql-voyager/ https://developer.github.com/v4/explorer/

Slide 14

Slide 14 text

En pratique... 14

Slide 15

Slide 15 text

● Générateur automatique Apollo-Android 15 user.graphql Schema.json UserQuery.kt

Slide 16

Slide 16 text

Apollo-Android 16 query { viewer { login bio status { emoji message } } } data class Viewer( val __typename: String = "User", /** * The username used to login. */ val login: String, /** * The user's public profile bio. */ val bio: String?, /** * The user's description of what they're currently doing. */ val status: UserQuery.Status? )

Slide 17

Slide 17 text

Depreciation 17 query { pinnedRepositories { nodes { id } } } /** * A list of repositories this user has pinned to their profile */ @Deprecated(message = "pinnedRepositories will be removed Use ProfileOwner.pinnedItems instead. Removal on 2019-10-01 UTC.") val pinnedRepositories: PinnedRepositories

Slide 18

Slide 18 text

Runtime 18 val client = ApolloClient.builder() .serverUrl("https://api.github.com/graphql") .build() val viewer = runBlocking { client.query(UserQuery()) .toDeferred() .await() .data() .viewer() }

Slide 19

Slide 19 text

● Parsing Json ● Requête HTTP ● Coroutines ● Cache Runtime 19 ● Android/JVM ● MPP ?

Slide 20

Slide 20 text

● Rajout d’une dépendance ● https://github.com/square/okhttp/issu es/4723 Java -> Kotlin MPP 20

Slide 21

Slide 21 text

● Compatibilité source/binaire ○ Checked exception ○ Methodes et classes finales ○ Optionel par defaut ○ Les data class explosent la surface d’API ○ @JvmField ● Japicmp Java -> Kotlin MPP 21

Slide 22

Slide 22 text

● Modèles + Parsing ✅ ● Runtime + Cache ❌ Java -> Kotlin MPP 22

Slide 23

Slide 23 text

Kotlin + Graphql = ❤ Typage fort du backend au frontend Bientôt MPP 23

Slide 24

Slide 24 text

Merci ! @martinbonnin 24

Slide 25

Slide 25 text

● Graphql-kotlin (Expedia) ● https://github.com/ExpediaGroup/graphql-kotlin ● https://www.youtube.com/watch?v=7YJyPXjLdug Server side 25

Slide 26

Slide 26 text

● HTTP cache ● Normalized cache ○ In Memory ○ SQLLite Cache 26

Slide 27

Slide 27 text

runBlocking { client.subscribe(UserSubscription()) .toFlow() .collect { // do something with data } } Subscriptions 27