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

GraphQL + Kotlin

GraphQL + Kotlin

This talk was given for the Kotlin Meetup group in Oslo on the 20th of August 2019. We went through a brief introduction to GraphQL and then coded up a small GraphQL endpoint using Kotlin.

Avatar for Arnab Datta

Arnab Datta

August 20, 2019
Tweet

More Decks by Arnab Datta

Other Decks in Programming

Transcript

  1. 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.
  2. 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”] } LETS EXPAND GET people/3 { “id": “1”, “name”: “Jessica”, “friends”: [“1”] }
  3. 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”] } THE REST WAY GET people/3 { “id": “1”, “name”: “Jessica”, “friends”: [“1”] }
  4. • 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.
  5. Arnab Datta Friends: Kim, Jessica GET personWithFriends/1
 { “id": “1”,

    “name”: “Arnab Datta”, “friendNames”: [“Kim”, “Jessica”] } LETS “FIX” IT
  6. Frontend dev: What was the name of the endpoint for

    people? Backend dev: /people/:id
  7. 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.
  8. 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.
  9. WHAT IS A PERSON? data class Person( val name: String,

    val id: UUID, val friends: List<Person> )
  10. 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” } …
  11. 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.