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

DroidCon IT - GraphQL on the JVM and beyond

DroidCon IT - GraphQL on the JVM and beyond

mbonnin

May 19, 2020
Tweet

More Decks by mbonnin

Other Decks in Programming

Transcript

  1. 1

  2. None
  3. GraphQL on the JVM and beyond DroidCon IT webinars 2020

    3
  4. GraphQL on the JVM and beyond @martinbonnin 4

  5. 1. What is GraphQL 2. Consuming GraphQL in an Android

    App 3. What’s next 5 Agenda
  6. Questions 6 https://app.sli.do/event/tp6pylrx https://wall.sli.do/event/tp6pylrx

  7. The beginnings of GraphQL 7 • 2012: Facebook

  8. REST: GET /feed 8 { "feed": [ { "type": "post",

    "title": "My cat is so funny :-P", "text": "Pancake loves to lay in the sun!", "thumbnail": "/img/fe3c57178.jpg" }, { "type": "post", "title": "My first homemade bread!", "text": "Using only flour and water", "thumbnail": "/img/85511bbb2.jpg" }, { "type": "ad", "title": "50 bread recipes" } ] }
  9. REST 9 GET /feed/desktop GET /feed/mobile GET /feed/tablet GET /feed/watch

  10. GraphQL 10 • Product oriented • Exposes a Graph Feed

    Post Post Ad Title Text XL Thumbnail S Thumbnail
  11. GraphQL 11 Feed Post Post Ad Title Text XL Thumbnail

    S Thumbnail • Product oriented • Exposes a Graph
  12. GraphQL 12 Feed Post Post Ad Title Text XL Thumbnail

    S Thumbnail • Product oriented • Exposes a Graph
  13. GraphQL 13 Feed Post Post Ad Title Text XL Thumbnail

    S Thumbnail • Product oriented • Exposes a Graph
  14. Hello GraphQL 14 query { feed { post { title

    text thumbnail } } } { "feed": { "post": { "title": "My cat is so funny :-P", "text": "Pancake loves to lay in the sun", "thumbnail": "/img/fe3c57178.jpg" } } }
  15. Principles 15 • Client requests the data. • Data is

    strongly typed. • The api is introspectable.
  16. GraphQL 16 • 2012: Facebook • 2015: Open Source release

    • 2018: Linux Foundation • https://github.com/graphql/graphql-spec
  17. Type system 17

  18. Type system 18 type User { login: String bio: String

    isBountyHunter: Boolean status: UserStatus issueComments: List<IssueComment> } User Github • Objects • Scalars ◦ Boolean ◦ Int ◦ Float ◦ String • Lists
  19. Type system 19 type User { login: String bio: String

    isBountyHunter: Boolean status: UserStatus issueComments: List<IssueComment> } User Github • Objects • Scalars ◦ Boolean ◦ Int ◦ Float ◦ String • Lists
  20. Type system 20 type User { login: String bio: String

    isBountyHunter: Boolean status: UserStatus issueComments: List<IssueComment> } User Github • Objects • Scalars ◦ Boolean ◦ Int ◦ Float ◦ String • Lists
  21. Type system 21 type User { login: String bio: String

    isBountyHunter: Boolean status: UserStatus issueComments: List<IssueComment> } User Github • Objects • Scalars ◦ Boolean ◦ Int ◦ Float ◦ String • Lists
  22. • Objects • Scalars ◦ Boolean ◦ Int ◦ Float

    ◦ String • Lists Type system 22 type User { login: String bio: String isBountyHunter: Boolean status: UserStatus issueComments: List<IssueComment> } User Github
  23. • Interfaces • Enums • Unions Type system 23 type

    User implements Node { id: ID! login: String bio: String [...] } User Github
  24. Null safety ❗ 24 type User { login: String❗ bio:

    String isBountyHunter: Boolean❗ status: UserStatus issueComments: List<IssueComment>❗ }
  25. Let’s try it with GraphiQL 25 https://developer.github.com/v4/explorer/

  26. Hello GraphQL 26 query GetViewer { viewer { login bio

    status { emoji message } } } { "data": { "viewer": { "login": "martinbonnin", "bio": "Lorem Ipsum", "status": { "emoji": ":smileyface:", "message": "Foo" } } } }
  27. Passing parameters 27 query GetViewer { viewer { login bio

    avatarUrl(size: 500) status { emoji message } } }
  28. Mutations 28 mutation { changeUserStatus(input: { emoji: ":recycle:" message: "Hello

    DroidCon!" }) { clientMutationId } }
  29. Introspection 29

  30. Schema 30 { "data" { "__schema": { "types": [ {

    "kind": "OBJECT", "name": "User", "description": "A user is an individual..", "fields": [...], "interfaces": [...], }, [...] ] } }
  31. 1. What is GraphQL 2. Consuming GraphQL in an Android

    app 3. What’s next 31
  32. Apollo-Android 32 • https://github.com/apollographql/apollo-android • Doesn’t require Android! • Java

    & Kotlin codegen. • Strong typing. • Reflection-free json parsing. • HTTP & Normalized cache. • RxJava & Coroutines support.
  33. Automatic code generation 33 query.graphql Schema.json Kotlin Models

  34. Demo 34 https://github.com/martinbonnin/DroidConITDemo/

  35. A few tips 35

  36. Authentication 36 • We use a separate Oauth endpoint. •

    Leverage the “Authorization” header. • Can be done with a OkHttp interceptor.
  37. Persisted queries 37 query { viewer { login bio status

    { emoji message } } } “OperationId”: viewerQuery
  38. Versioning 38 • Versioning is fluid. • Easy to add

    new fields, hard to remove. • Do not modify types. • Monitor your backend.
  39. Use deprecation 39 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
  40. 1. What is GraphQL 2. Consuming GraphQL in an Android

    app 3. What’s next 40
  41. Apollo 2.0 41 • Released end of April • Multiplatform

    ◦ Codegen ◦ Parsing
  42. What’s next 42 • Multiplatform runtime • Modularized network APIs

    • Modularized cache APIs
  43. Thanks ! @martinbonnin 43

  44. Questions 44 https://app.sli.do/event/tp6pylrx https://wall.sli.do/event/tp6pylrx

  45. None
  46. 46

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

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

    data } } Subscriptions 48