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

Our ongoing journey from REST to GraphQL on Android

Julien Salvi
September 20, 2023

Our ongoing journey from REST to GraphQL on Android

Here is the story of an ongoing migration... and what a migration! This journey moving from our REST API to the GraphQL one is a long run with much coordination across all tech teams.

This return of experience will focus on how we are dealing with this transition on Android where Retrofit coexists with Apollo, how we synchronized with backend teams to keep our GraphQL schemas up-to-date or how we are dealing with our authentication stack with Apollo Kotlin, or the issues we faced and much more.

Buckle up! Relax! The journey is just starting 🚜

Julien Salvi

September 20, 2023
Tweet

More Decks by Julien Salvi

Other Decks in Programming

Transcript

  1. 1 The Power of Conversation aircall.io Our ongoing journey from

    REST to GraphQL on Android Julien Salvi - Android GDE | Android @ Aircall DroidKaigi 2023 󰏦 @JulienSalvi
  2. 2 The Power of Conversation aircall.io Bonjour ! Julien Salvi

    Lead Android Engineer @ Android GDE PAUG, Punk & IPAs! @JulienSalvi
  3. 3 The Power of Conversation aircall.io Intro & context GraphQL

    and Apollo Kotlin REX: GraphQL migration Conclusion & Resources Summary
  4. 6 The Power of Conversation aircall.io Efficient data retrieval What’s

    GraphQL? 1 2 3 Single endpoint simplicity Strong typing and introspection GraphQL optimizes data fetching by allowing clients to specify their exact data requirements, eliminating over-fetching and under-fetching. GraphQL simplifies client-server interactions by using a single endpoint, reducing round trips and streamlining communication. GraphQL strong typing system ensures clarity in data exchange. Introspection capabilities enable powerful tools and automatic API documentation. “GraphQL is an open-source query language and runtime that provides an efficient and flexible approach to data fetching and manipulation. It was developed by Facebook and later open-sourced”
  5. 8 The Power of Conversation aircall.io Why REST to GraphQL?

    Efficiency and data aggregation The need for efficient microservice communication and a data aggregator point. Scaling of REST API Wanted to break the REST API into microservices. It was not modular enough and has scaling issues. Minimum disruption Due to Internal usage of our REST APIs, we pushed us to take advantage of Graphql with minimum disruption to our customers. Serverless alternative Desire to create a serverless alternative API from the existing monolith REST interface.
  6. 9 The Power of Conversation aircall.io Key points for GraphQL

    Data aggregation Clients can fetch multiple resources in a single request, reducing the number of network round trips. Efficient data loading GraphQL allows clients to request only the data they need, minimizing over-fetching and under-fetching. Strong typing GraphQL's type system ensures clear communication between clients and servers, reducing errors and providing reliable data validation. Federated GraphQL APIs Enables independent development, deployment, and scaling of individual services, resulting in improved maintainability and scalability of the system. Real time communication GraphQL provide a way to establish a persistent connection between the client and the server, enabling efficient and scalable real-time updates without the need for constant polling. Reduced latency GraphQL supports batching multiple requests into a single round trip, reducing network latency and improving overall performance.
  7. 11 The Power of Conversation aircall.io The tech under the

    hood • Our infra relies a lot on AWS so our GraphQL backend teams went for AppSync. • AppSync offers many capabilities like filtering, real-time or scalability and has a nice integration with ElasticSearch, DynamoDB or Cognito.
  8. 12 The Power of Conversation aircall.io The tech under the

    hood • On the Android side, we are using the Apollo Kotlin library to manipulate the GraphQL API. • offers many advantages: ◦ Great interoperability with Kotlin code base (100% Kotlin) ◦ Model generation in Kotlin ◦ Multiplatform ◦ Queries, Mutations and Subscriptions ◦ Caching system ◦ AppSync and graphql-ws support ◦ Awesome community support 💚
  9. 13 The Power of Conversation aircall.io GraphQL API journey 2020

    2021 2022 2023 First GraphQL queries in production GraphQL API introduction Monolith transition + GraphQL migration (backend) Migration of REST endpoints to GraphQL in client apps New features with GraphQL API GraphQL API in clients apps
  10. 14 The Power of Conversation aircall.io Now let’s see how

    we brought GraphQL to Android with Apollo 🚀
  11. 17 The Power of Conversation aircall.io Apollo Kotlin setup •

    Let’s see how straightforward is it to setup Apollo Kotlin in your Android projects. • Let’s see how to deal with the GraphQL schema (our source of truth). • Let’s see how to make Queries, Mutations and Subscriptions. • Let’s see how the Apollo client can coexist with the Retrofit client.
  12. 24 The Power of Conversation aircall.io 💡Pro tip IntelliJ •

    Take advantage of the GraphQL plugin by Jim Kynde Meyer to have the autocompletion/deeplink when writing your queries. https://plugins.jetbrains.com/plugin/8097-graphql Add the following graphql.config.yml at the root:
  13. 27 The Power of Conversation aircall.io Your GraphQL API is

    now ready to go in your Android app 🚀
  14. 30 The Power of Conversation aircall.io Apollo 💚 Kotlin •

    Apollo built its SDK 100% Kotlin. • It has a great support for coroutines and Kotlin flows. • Generates Kotlin code from the schema (data classes for models and Query/Mutations)
  15. 35 The Power of Conversation aircall.io Beware the timeout •

    If your REST API is using a timeout DON’T use the same one for your GraphQL API. • Sync with your backend/client teams to identify the best timeout to pass to your GraphQL client • If you’re using OkHttp, DON’T reuse the same OkClient for your REST and GraphQL API.
  16. 37 The Power of Conversation aircall.io Lazy loading • When

    only a part of your app or a feature (eg. under a feature flag) is using the GraphQL API, use the lazy loading when injecting your client. • Dagger/Hilt exposed a Lazy interface to defer the resource loading.
  17. 39 The Power of Conversation aircall.io https://github.com/ChuckerTeam/chucker Monitoring • In

    your develop or prod env. always monitor your requests. • Take advantage of the OkHttp Interceptor to monitor queries and mutation. • ⚙ Use Chucker tool in dev mode to monitor your requests • 👀 Check Nicola Corti’s talk about Chucker 4.0 at droidcon Berlin
  18. 41 The Power of Conversation aircall.io “ ” Nested queries

    Great power comes with great responsibilities 🦸
  19. 42 The Power of Conversation aircall.io Nested queries • GraphQL

    offers many capabilities and one of them is the support of nested queries. • This is pretty efficient when aggregating multiple data at once. • ⚠ But the response time might increase a lot. Always monitor! • 🤝 Sync with your backend teams to find the best solution
  20. 46 The Power of Conversation aircall.io “ ” Subscriptions are

    cool… but aren’t easy! Especially with AppSync 😅
  21. 47 The Power of Conversation aircall.io Subscriptions • Subscriptions are

    long-lasting operation which can maintain an open connection (mainly a WebSocket). • Data is pushed to socket and listened on the client side. • Keeping the connection alive isn’t easy! • … and things might get a bit more complicated with AppSync 😅 • #SpoilerAlert: We had to build our own AppSyncProtol to better handle the token refresh.
  22. 50 The Power of Conversation aircall.io Subscriptions While investigating a

    socket disconnection issue with Apollo and AppSync subs, we saw an opened issue on their repository 👀
  23. 52 The Power of Conversation aircall.io With this method only

    resetting the client could do the trick 😣
  24. 53 The Power of Conversation aircall.io Using a lambda will

    help us resetting the URL without resetting the client 😃
  25. 54 The Power of Conversation aircall.io Subscriptions But no ongoing

    develop for that feature 🥲 So let’s contribute to Apollo Kotlin 🚀
  26. 56 The Power of Conversation aircall.io Error handling • Error

    handling with GraphQL is quite different from a REST API • Errors can be exposed in the Query/Mutation, be directly accessible in the ApolloResponse class or an Exception can be thrown 🥲 • ApolloResponse exposed a list of potential errors. • So multiple source of truth and error parsing…
  27. 60 The Power of Conversation aircall.io Schema automation • At

    some point you will ne need to automate the GraphQL schema update. • More development on the backend side, more updates on the client side. • When working with multiple flavors this will be highly recommended! • Let’s see how we managed that with our CI.
  28. 63 The Power of Conversation aircall.io Schema automation • When

    the CI has passed successfully we can merge it to develop branch. • If the CI failed, we checkout and fix branch. • If we can’t fix the update, we cancel the MR and sync with the backend teams.
  29. 64 The Power of Conversation aircall.io Multiple flavors • When

    you have multiple flavors (eg. staging/prod or white labels…), you may have multiple schemas to maintain. • This will lead to multiple updates and potentially be more error-prone . • ⚠ More maintenance when dealing with multiple flavors.
  30. 66 The Power of Conversation aircall.io Apollo Community 💚 •

    The community around Apollo GraphQL is amazing! • Apollo Kotlin is an open-source project . • Maintainers are very active and very helpful when you have a question or an issue. • Open an issue on GitHub or ask a question in the Kotlin slack 🚀 • Many good features to come with Apollo 4.0.0 🤩
  31. 68 The Power of Conversation aircall.io Key takeways Why it

    was the right move • Apollo Kotlin 🚀 • Easy integration • Coroutine/Flow support • Strong typing and contract with the API • Combined queries • Apollo community 💚 • Apollo maintainers 💚 Areas to watch • Collaborate/speak with your backend teams. • Error handling. • Subscriptions aren’t always reliable. • Apollo AppSync support.
  32. 69 The Power of Conversation aircall.io GraphQL documentation https://graphql.org/ Apollo

    Kotlin https://www.apollographql.com/docs/kotlin/ Apollo Kotlin repository https://github.com/apollographql/apollo-kotlin Apollo GraphQL Summit videos https://www.apollographql.com/events/virtual-event/graphql-summit/ AWS AppSync https://aws.amazon.com/appsync/ AppSync Android SDK https://docs.amplify.aws/sdk/api/graphql/q/platform/android/ https://github.com/awslabs/aws-mobile-appsync-sdk-android Resources
  33. 70 The Power of Conversation aircall.io ありがとうございます Have fun with

    GraphQL 😃 Julien Salvi - Android GDE | Android @ Aircall DroidKaigi 2023 󰏦 @JulienSalvi