Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GraphQL IRL (Android Makers)

GraphQL IRL (Android Makers)

Roberto Orgiu

April 22, 2019
Tweet

More Decks by Roberto Orgiu

Other Decks in Technology

Transcript

  1. film
    character
    species
    homeworld
    episode

    View full-size slide

  2. film
    character
    species
    homeworld
    episode
    GR APHQL
    IN REAL L IFE
    ROBERTO ORGIU - @_TIWIZ

    View full-size slide

  3. Fun fact
    GraphQL.replace(REST)
    usually returns false

    View full-size slide

  4. Architectural
    Pattern
    Query
    Language

    View full-size slide

  5. Leverage
    Protocol
    Use own
    Conventions

    View full-size slide

  6. Full request
    Client
    Performances

    View full-size slide

  7. Multiple
    Endpoints
    Single
    Endpoint

    View full-size slide

  8. What is GraphQ
    What is GraphQL?
    D E E P D I V E I N T O

    View full-size slide

  9. WHY APOLLO?
    Compile time safety
    Caching
    Community
    Code generation

    View full-size slide

  10. buildscriptT{
    repositoriesT{
    google()T
    }A
    dependenciesT{
    classpathA"com.android.tools.build:gradle:$tools_version"
    classpathB"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }B
    }C
    allprojectsT{
    repositoriesB{
    google()B
    }D
    }E

    View full-size slide

  11. buildscriptT{
    repositoriesT{
    google()T
    jcenter()
    }A
    dependenciesT{
    classpathA"com.android.tools.build:gradle:$tools_version"
    classpathB"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "com.apollographql.apollo:gradle-plugin:$apollo_version"
    }B
    }C
    allprojectsT{
    repositoriesB{
    google()B
    jcenter()
    }D
    }E

    View full-size slide

  12. buildscriptT{
    repositoriesT{
    google()T
    jcenter()
    }A
    dependenciesT{
    classpathA"com.android.tools.build:gradle:$tools_version"
    classpathB"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "com.apollographql.apollo:gradle-plugin:$apollo_version"
    }B
    }C
    allprojectsT{
    repositoriesB{
    google()B
    jcenter()
    }D
    }E

    View full-size slide

  13. apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    android {
    ...
    }

    View full-size slide

  14. apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    apply plugin: 'com.apollographql.android'
    android {
    ...
    }

    View full-size slide

  15. dependencies {
    ...
    implementation "com.apollographql.apollo:apollo-runtime:$apollo_version"
    implementation "com.apollographql.apollo:apollo-rx2-support:$apollo_version"
    ...
    }

    View full-size slide

  16. DOWNSIDES
    Apollo-plugin
    schema.json
    *.graphql
    X X

    View full-size slide

  17. testing
    Compile time safety
    MockWebServer

    View full-size slide

  18. > Task :apollo:generateDebugApolloIR FAILED
    .../android/apollo/Query.graphql: Cannot query field "nam" on
    type "YourType". Did you mean "name"?
    error: Validation of GraphQL query document failed
    FAILURE: Build failed with an exception.

    View full-size slide

  19. @get:Rule
    val mockWebServer = MockWebServerRule()
    mockWebServer.enqueueResponse("/response.json")
    val testApolloClient =
    ApolloClient.builder()
    .serverUrl(mockWebServer.url)
    .okHttpClient(client)
    .build()

    View full-size slide

  20. query filmById($filmId: ID){
    film(filmID :$filmId){
    title
    episodeID
    characterConnectionT{
    charactersT{
    name
    homeworldT{
    name
    }A
    speciesT{
    name
    classification
    }B
    filmConnectionT{
    filmsT{
    title
    episodeID
    }C
    }D
    }E
    }F
    }G
    }H
    A u t o m a t i c
    P e r s i s t e d Q u e r i e s

    View full-size slide

  21. query filmById($filmId: ID){
    film(filmID :$filmId){
    title
    episodeID
    characterConnectionT{
    charactersT{
    name
    homeworldT{
    name
    }A
    speciesT{
    name
    classification
    }B
    filmConnectionT{
    filmsT{
    title
    episodeID
    }C
    }D
    }E
    }F
    }G
    }H
    A u t o m a t i c
    P e r s i s t e d Q u e r i e s
    variables {
    "filmId" : 1
    }A
    query: query filmById($filmId: ID) {
    film(filmId :$filmId) {
    title
    episodeID
    characterConnection {
    name
    homeworld{
    name
    }B
    species {
    name
    classification
    }C
    filmConnection {
    films {
    title
    episodeID
    }D
    }E
    }F
    }G
    }H
    operationName: filmById

    View full-size slide

  22. query filmById($filmId: ID){
    film(filmID :$filmId){
    title
    episodeID
    characterConnectionT{
    charactersT{
    name
    homeworldT{
    name
    }A
    speciesT{
    name
    classification
    }B
    filmConnectionT{
    filmsT{
    title
    episodeID
    }C
    }D
    }E
    }F
    }G
    }H
    A u t o m a t i c
    P e r s i s t e d Q u e r i e s
    variables {
    "filmId" : 1
    }A
    query: query filmById($filmId: ID) {
    film(filmId :$filmId) {
    title
    episodeID
    characterConnection {
    name
    homeworld{
    name
    }B
    species {
    name
    classification
    }C
    filmConnection {
    films {
    title
    episodeID
    }D
    }E
    }F
    }G
    }H
    operationName: filmById
    operationName: filmById
    variables {
    "filmId" : 1
    }T

    View full-size slide

  23. query filmById($filmId: ID){
    film(filmID :$filmId){
    title
    episodeID
    characterConnectionT{
    charactersT{
    name
    homeworldT{
    name
    }A
    speciesT{
    name
    classification
    }B
    filmConnectionT{
    filmsT{
    title
    episodeID
    }C
    }D
    }E
    }F
    }G
    }H
    A u t o m a t i c
    P e r s i s t e d Q u e r i e s
    variables {
    "filmId" : 1
    }A
    query: query filmById($filmId: ID) {
    film(filmId :$filmId) {
    title
    episodeID
    characterConnection {
    name
    homeworld{
    name
    }B
    species {
    name
    classification
    }C
    filmConnection {
    films {
    title
    episodeID
    }D
    }E
    }F
    }G
    }H
    operationName: filmById
    operationName: filmById
    variables {
    "filmId" : 1
    }T

    View full-size slide

  24. A u t o m a t i c
    P e r s i s t e d Q u e r i e s
    operationName: filmById
    variables {
    "filmId" : 1
    }T
    extensions: {
    persistedQuery: {
    "version": 1,
    "sha256Hash": "queryHash"
    }
    }

    View full-size slide

  25. Android Back end
    Query Hash
    Query Hash Not Found
    Whole Query + Query Hash
    Response

    View full-size slide

  26. @get:Rule
    valTmockWebServerT=TMockWebServerRule()
    mockWebServer.enqueueResponse(“/response.json")
    valTurlT=TmockWebServer.url
    valßapolloClientT=
    ApolloClient.builder()
    .serverUrl(url)
    .okHttpClient(client)
    .build()

    View full-size slide

  27. valßapolloClientT=
    ApolloClient.builder()
    .serverUrl(url)
    .okHttpClient(client)
    .build()

    View full-size slide

  28. valßapolloClientT=
    ApolloClient.builder()
    .serverUrl(url)
    .okHttpClient(client)
    .enableAutoPersistedQueries(true)
    .build()

    View full-size slide

  29. Tricks
    Tricks
    J U S T S O M E
    query SampleQuery {
    assetID: sourceId
    fingerprint: modified
    headlineInfo: headline {
    __typename
    headline: default
    }A
    lastUpdated: lastModification
    }B

    View full-size slide

  30. Tricks
    Tricks
    L A B E L F I E L D S C A N G O
    A L O N G D I S T A N C E
    J U S T S O M E
    query SampleQuery {
    assetID: sourceId
    fingerprint: modified
    headlineInfo: headline {
    __typename
    headline: default
    }A
    lastUpdated: lastModification
    }B

    View full-size slide

  31. Tricks
    Tricks
    A P O L L O A D D S
    T Y P E N A M E E V E R Y W H E R E
    J U S T S O M E
    query SampleQuery {
    assetID: sourceId
    fingerprint: modified
    headlineInfo: headline {
    __typename
    headline: default
    }A
    lastUpdated: lastModification
    }B

    View full-size slide

  32. Tricks
    Tricks
    S C H E M A A N D Q U E R I E S
    C A N B E M O V E D
    J U S T S O M E
    apollo {
    schemaFilePath = "/path/my-schema.json"
    outputPackageName = "pkg.graphql.api"
    }

    View full-size slide

  33. Tricks
    Tricks
    S U P P O R T S F O R J A V A
    B E A N S E M A N T I C S
    J U S T S O M E
    class Type {
    public Another getAnother() { ... }
    }
    //build.gradle
    apollo {
    useJavaBeansSemanticNaming = true
    }

    View full-size slide

  34. Tricks
    Tricks
    R E M E M B E R T O
    B E O N L I N E ¯ \ _ ( ツ ) _ / ¯
    J U S T S O M E

    View full-size slide