$30 off During Our Annual Pro Sale. View Details »

Du GraphQL 100% Kotlin avec Apollo-Android

Du GraphQL 100% Kotlin avec Apollo-Android

mbonnin

March 05, 2020
Tweet

More Decks by mbonnin

Other Decks in Programming

Transcript

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

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

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

    Moderne Typage fort Null safe Des outils ❤
  4. Moderne 4 • Query Language • 2012: Facebook • 2015:

    Release publique • 2018: Linux Foundation • https://github.com/graphql/graphql-spec
  5. • Objets • Scalaires ◦ Boolean ◦ Int ◦ Float

    ◦ String Typage fort 5 type User { login: String bio: String isBountyHunter: Boolean status: UserStatus issueComments: List<IssueComment> } User Github
  6. • Objets • Scalaires ◦ Boolean ◦ Int ◦ Float

    ◦ String Typage fort 6 type User { login: String bio: String isBountyHunter: Boolean status: UserStatus issueComments: List<IssueComment> } User Github
  7. • Objets • Scalaires ◦ Boolean ◦ Int ◦ Float

    ◦ String Typage fort 7 type User { login: String bio: String isBountyHunter: Boolean status: UserStatus issueComments: List<IssueComment> } User Github
  8. • Objets • Scalaires ◦ Boolean ◦ Int ◦ Float

    ◦ String Typage fort 8 type User { login: String bio: String isBountyHunter: Boolean status: UserStatus issueComments: List<IssueComment> } User Github
  9. • Objets • Scalaires ◦ Boolean ◦ Int ◦ Float

    ◦ String Typage fort 9 type User { login: String bio: String isBountyHunter: Boolean status: UserStatus issueComments: List<IssueComment> } User Github
  10. • Mais aussi: ◦ Interfaces ◦ Listes ◦ Enums ◦

    Unions Typage fort 10 type User implements Node { id: ID! login: String bio: String [...] } User Github
  11. Null safety ❗ 11 type User { login: String❗ bio:

    String isBountyHunter: Boolean❗ status: UserStatus issueComments: List<IssueComment>❗ }
  12. Schema 12 { "data" { "__schema": { "types": [ {

    "kind": "OBJECT", "name": "User", "description": "A user is an individual..", "fields": [...], "interfaces": [...], }, [...] ] } }
  13. 13 OUTILS https://apis.guru/graphql-voyager/ https://developer.github.com/v4/explorer/

  14. En pratique... 14

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

  16. 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? )
  17. 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
  18. Runtime 18 val client = ApolloClient.builder() .serverUrl("https://api.github.com/graphql") .build() val viewer

    = runBlocking { client.query(UserQuery()) .toDeferred() .await() .data() .viewer() }
  19. • Parsing Json • Requête HTTP • Coroutines • Cache

    Runtime 19 • Android/JVM • MPP ?
  20. • Rajout d’une dépendance • https://github.com/square/okhttp/issu es/4723 Java -> Kotlin

    MPP 20
  21. • 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
  22. • Modèles + Parsing ✅ • Runtime + Cache ❌

    Java -> Kotlin MPP 22
  23. Kotlin + Graphql = ❤ Typage fort du backend au

    frontend Bientôt MPP 23
  24. Merci ! @martinbonnin 24

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

  26. • HTTP cache • Normalized cache ◦ In Memory ◦

    SQLLite Cache 26
  27. runBlocking { client.subscribe(UserSubscription()) .toFlow() .collect { // do something with

    data } } Subscriptions 27