Slide 1

Slide 1 text

1 The Power of Conversation aircall.io Our ongoing journey from REST to GraphQL on Android Julien Salvi - Android GDE | Android @ Aircall droidcon London 2023 󰏅 @JulienSalvi

Slide 2

Slide 2 text

2 The Power of Conversation aircall.io Bonjour ! Julien Salvi Lead Android Engineer @ Android GDE “Android dev in shorts!” @JulienSalvi

Slide 3

Slide 3 text

3 The Power of Conversation aircall.io Intro & context GraphQL and Apollo Kotlin REX: GraphQL migration Conclusion & Resources Summary

Slide 4

Slide 4 text

4 The Power of Conversation aircall.io Introduction and context

Slide 5

Slide 5 text

5 The Power of Conversation aircall.io “ ” What’s GraphQL?

Slide 6

Slide 6 text

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”

Slide 7

Slide 7 text

7 The Power of Conversation aircall.io “ ” Why GraphQL at Aircall?

Slide 8

Slide 8 text

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.

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

10 The Power of Conversation aircall.io “ ” How we use GraphQL?

Slide 11

Slide 11 text

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.

Slide 12

Slide 12 text

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 💚

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

14 The Power of Conversation aircall.io Now let’s see how we brought GraphQL to Android with Apollo 🚀

Slide 15

Slide 15 text

15 The Power of Conversation aircall.io GraphQL and Apollo Kotlin +

Slide 16

Slide 16 text

16 The Power of Conversation aircall.io “ ” How to setup Apollo Kotlin?

Slide 17

Slide 17 text

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.

Slide 18

Slide 18 text

18 The Power of Conversation aircall.io

Slide 19

Slide 19 text

19 The Power of Conversation aircall.io GraphQL schema

Slide 20

Slide 20 text

20 The Power of Conversation aircall.io

Slide 21

Slide 21 text

21 The Power of Conversation aircall.io

Slide 22

Slide 22 text

22 The Power of Conversation aircall.io

Slide 23

Slide 23 text

23 The Power of Conversation aircall.io

Slide 24

Slide 24 text

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 but… https://plugins.jetbrains.com/plugin/8097-graphql Add the following graphql.config.yml at the root:

Slide 25

Slide 25 text

25 The Power of Conversation aircall.io 💡Pro tip IntelliJ ● … There is another one! GraphQL plugin by Apollo 🚀 https://plugins.jetbrains.com/plugin/8097-graphql Add the following graphql.config.yml at the root:

Slide 26

Slide 26 text

26 The Power of Conversation aircall.io Apollo client setup

Slide 27

Slide 27 text

27 The Power of Conversation aircall.io GraphQL requests

Slide 28

Slide 28 text

28 The Power of Conversation aircall.io Your GraphQL API is now ready to go in your Android app 🚀

Slide 29

Slide 29 text

29 The Power of Conversation aircall.io REX: GraphQL migration 🔬 󰳘

Slide 30

Slide 30 text

30 The Power of Conversation aircall.io “ ” Apollo 💚 Kotlin

Slide 31

Slide 31 text

31 The Power of Conversation aircall.io Apollo 💚 Kotlin ● Apollo is 100% Kotlin and multiplatform. ● It has a great support for coroutines and Kotlin flows. ● Generates Kotlin code from the schema (data classes for models and Query/Mutations)

Slide 32

Slide 32 text

32 The Power of Conversation aircall.io

Slide 33

Slide 33 text

33 The Power of Conversation aircall.io

Slide 34

Slide 34 text

34 The Power of Conversation aircall.io For Kotlin code gen

Slide 35

Slide 35 text

35 The Power of Conversation aircall.io “ ” Beware the timeout

Slide 36

Slide 36 text

36 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.

Slide 37

Slide 37 text

37 The Power of Conversation aircall.io “ ” Lazy loading

Slide 38

Slide 38 text

38 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.

Slide 39

Slide 39 text

39 The Power of Conversation aircall.io “ ” Monitoring

Slide 40

Slide 40 text

40 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

Slide 41

Slide 41 text

41 The Power of Conversation aircall.io Monitoring demo!

Slide 42

Slide 42 text

42 The Power of Conversation aircall.io “ ” Nested queries Great power comes with great responsibilities 🦸

Slide 43

Slide 43 text

43 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

Slide 44

Slide 44 text

44 The Power of Conversation aircall.io Nested queries

Slide 45

Slide 45 text

45 The Power of Conversation aircall.io 7-10s query 😱

Slide 46

Slide 46 text

46 The Power of Conversation aircall.io Optimized query 😎

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

48 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 the 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.

Slide 49

Slide 49 text

49 The Power of Conversation aircall.io How is this exception triggered? 🤔

Slide 50

Slide 50 text

50 The Power of Conversation aircall.io

Slide 51

Slide 51 text

51 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 👀

Slide 52

Slide 52 text

52 The Power of Conversation aircall.io Auth has to be passed in the webSocket Url

Slide 53

Slide 53 text

53 The Power of Conversation aircall.io With this method only resetting the client could do the trick 😣

Slide 54

Slide 54 text

54 The Power of Conversation aircall.io Using a lambda will help us resetting the URL without resetting the client 😃

Slide 55

Slide 55 text

55 The Power of Conversation aircall.io Subscriptions But no ongoing develop for that feature 🥲 So let’s contribute to Apollo Kotlin 🚀

Slide 56

Slide 56 text

56 The Power of Conversation aircall.io “ ” Error handling

Slide 57

Slide 57 text

57 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…

Slide 58

Slide 58 text

58 The Power of Conversation aircall.io

Slide 59

Slide 59 text

59 The Power of Conversation aircall.io Work in progress 🚧

Slide 60

Slide 60 text

60 The Power of Conversation aircall.io With Apollo 4.x

Slide 61

Slide 61 text

61 The Power of Conversation aircall.io “ ” Schema automation

Slide 62

Slide 62 text

62 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.

Slide 63

Slide 63 text

63 The Power of Conversation aircall.io Schema automation

Slide 64

Slide 64 text

64 The Power of Conversation aircall.io Schema automation

Slide 65

Slide 65 text

65 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.

Slide 66

Slide 66 text

66 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.

Slide 67

Slide 67 text

67 The Power of Conversation aircall.io “ ” Apollo issue? Apollo community 💚💪

Slide 68

Slide 68 text

68 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 🤩

Slide 69

Slide 69 text

69 The Power of Conversation aircall.io Conclusion and Resources

Slide 70

Slide 70 text

70 The Power of Conversation aircall.io Key takeaways 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.

Slide 71

Slide 71 text

71 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

Slide 72

Slide 72 text

72 The Power of Conversation aircall.io Thank you! Have fun with GraphQL 😃 Julien Salvi - Android GDE | Android @ Aircall droidcon London 2023 󰏅 @JulienSalvi