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

Kotlin + GraphQL

Kotlin + GraphQL

92269d94f7821a50f77fa031952387f4?s=128

Arnab Datta

April 09, 2021
Tweet

Transcript

  1. GRAPHQL+KOTLIN +

  2. ABOUT ME As a teenager, Arnab dreamt of being a

    standup comedian, but somehow ended up finding the world of programming instead. He can be found coding away on his mac or dancing (sometimes both at the same time). Feel free to grab him and ask him any questions you want, especially if they involve fan theories about Game of Thrones.
  3. • First and foremost: fixes so many of the problems

    that Java suffered from • Null safety built in the type safety • Just…so many small utilities everywhere • Functions are first class citizens
  4. • First choice language for all Android development at Google

    since 2019 • #4 on Stackoverflow’s list of most loved languages • 2nd most popular language on the JVM in 2020
  5. KOTLIN

  6. THE TYPE SYSTEM

  7. DATA CLASSES

  8. EXTENSION FUNCTIONS (ADD FUNCTIONALITY TO EXISTING CLASSES)

  9. TYPEALIASES

  10. WHAT IS GRAPHQL?

  11. WHAT IS IT NOT

  12. IT IS NOT A GRAPH DATABASE

  13. IT IS NOT A FACEBOOK THING*

  14. IT IS NOT A JAVASCRIPT THING

  15. GRAPHQL IS A SPEC

  16. JUST LIKE ANY SPEC, IT HAS MULTIPLE IMPLEMENTATIONS

  17. WE WILL BE FOCUSING ON GRAPHQL-JAVA WITH KOTLIN

  18. BUT WHY GRAPHQL? CAN’T WE JUST KEEP ON USING REST?

  19. GET people/1 { “id": “1”, “name”: “Arnab Datta” } Arnab

    Datta THE REST WAY
  20. LETS EXPAND Arnab Datta Friends: Kim, Jessica GET people/1 {

    “id": “1”, “name”: “Arnab Datta”, “friends”: [“2”, “3”] } GET people/2 { “id”: “2”, “name”: “Kim”, “friends”: [“1”] } GET people/3 { “id": “1”, “name”: “Jessica”, “friends”: [“1”] }
  21. • Multiple requests for one view • What happens if

    the IDs provided by the first endpoint are incorrect? • No guarantee that we can access the IDs provided by the first endpoint • We are wasting bandwidth on getting the friends of people we only need the name of.
  22. Arnab Datta Friends: Kim, Jessica GET personWithFriends/1 { “id": “1”,

    “name”: “Arnab Datta”, “friendNames”: [“Kim”, “Jessica”] } LETS “FIX” IT
  23. • Single request per view • Access problems fixed •

    All done…right?
  24. Frontend dev: What was the name of the endpoint for

    people? Backend dev: /people/:id
  25. Frontend dev: What was the name of the endpoint for

    people? Backend dev: /people/:id Frontend dev: It only supplies the friends names. I want to create a link to their friends, so I need their IDs too Backend dev: Okay, I’ll make a PR.
  26. Frontend dev: What was the name of the endpoint for

    people? Backend dev: /people/:id Frontend dev: It only supplies the friends names. I want to create a link to their friends, so I need their IDs too Backend dev: Okay, I’ll make a PR. Frontend dev: Do you mind adding their phone numbers too? Backend dev: Okay, I’ll make a PR.
  27. CAN WE DO BETTER?

  28. WHAT IS A PERSON? data class Person( val name: String,

    val id: UUID, val friends: List<Person> )
  29. WHAT IF THIS WAS ALMOST ALL THE CODE YOUR API

    NEEDED?*
  30. HOW ABOUT ENTITIES THAT ARE SOMEWHAT SIMILAR?

  31. WHAT ABOUT PETS? sealed class Pet( abstract val name: String,

    abstract val id: UUID, abstract fun makeSound(): String ) data class Dog(override val name: String, override val id: UUID): Pet() { override fun makeSound() = “woof” } …
  32. WHAT ABOUT PETS? sealed class Pet( abstract val name: String,

    abstract val id: UUID, abstract fun makeSound(): String ) data class Dog(override val name: String, override val id: UUID): Pet() { override fun makeSound() = “woof” } … This can be your API code.
  33. people (id: “1”) { name } Arnab Datta THE GRAPHQL

    WAY
  34. Arnab Datta Friends: Kim, Jessica THE GRAPHQL WAY people(id: “1”)

    { name friends { name } }
  35. LETS CODE!

  36. TO SUM UP • GraphQL + Kotlin allows your backend

    developers to just worry about the data model • Put the client first without continuously poking your backend developers for every view • Flexibility without sacrificing simplicity • The same type-safety that Kotlin offers
  37. Slides can be found at https://speakerdeck.com/arnabkd/ Code can be found

    at https://github.com/arnabkd/graphql-kotlin- presentation Learn GraphQL at https://www.howtographql.com GraphQL documentary: https://www.youtube.com/watch? v=783ccP__No8