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

GraphQL with Kotlin

GraphQL with Kotlin

How to query and build a schema with GraphQL using Kotlin.

B3f560d34c14a9113e5024bc34ac26a0?s=128

Mohit S

April 26, 2017
Tweet

Transcript

  1. GraphQL +Kotlin Mohit Sarveiya

  2. — Query Language for your API developed at Facebook. —

    Polyglot implementation for client and server - Java, Javascript, Python, etc… — Reduces API requests needed and only gives you that data you asked for. What is GraphQL?
  3. Problem — Too many API requests per month. — Would

    be good to get data for a 
 month in request.
  4. GraphQL Overview query GetMonthData { campaign { date range posts

    { title created date } … } } Return Result Fetches Data Validates Client Server
  5. GraphQL Overview query GetMonthData { campaigns { title posts {

    title } … } } Return Result Fetches Data Validates Query Result { campaigns { [ { title: “Campaign 1” posts: [ title: “Post 1” ] } … ] } }
  6. DATABASE REST API CLIENTS GraphQL Server

  7. DATABASE REST API GRAPHQL SERVER CLIENTS GraphQL Server

  8. 1. How to query the sever 2. Build a GraphQL

    server in Kotlin
  9. Star Wars Database Droids (R2D2, C3PO,…) — name, height, friends,

    
 primary function.. Humans (Luke, Leia, Solo,…) — name, height, friends, planet…
  10. Simple Query query Hero { hero { name } }

  11. Simple Query query Hero { hero { name } }

    GraphQL Object defined in schema. Field on hero object.
  12. Simple Query query Hero { hero { name } }

    { data: { hero: { name: “R2 -D2” } } } Query Result
  13. 1. How to construct queries on Android in Kotlin

  14. None
  15. 1. Add Gradle Plugin. 2. Add schema definition to app

    in a JSON file. 3. Add queries to files with a “.graphql” file extension. 4. Use autogenerate classes from the “.graphql” files to make calls. Integrating Apollo Android Client
  16. 1. Add Apollo Gradle Plugin

  17. 2. Add Introspection of Schema (schema.json)

  18. 3. Add Queries to file with “.graphql” ext File: queries.graphql

  19. 4. Use ApolloClient object to make request

  20. Arguments & Variables

  21. Get Details for Droid with id - 2000.

  22. Get Details for Droid for any id

  23. Handling arguments on Android

  24. Result Query

  25. Enum Arguments

  26. Supplying Enum Arguments on Android

  27. Aliases & Fragments

  28. Aliases

  29. Aliases

  30. Aliases

  31. Aliases

  32. Fragments

  33. Fragments

  34. Fragments

  35. Mutations

  36. Create a Review for an Episode

  37. Mutating on Android

  38. Mutation Result

  39. 2. Building a GraphQL server

  40. None
  41. None
  42. — GraphQL Object type — Scalar types (String, Int, Float,

    Boolean) — List type — Interface type — Enum type — Union type — Input Object type GraphQL Type System
  43. Star Wars Database Droids (R2D2, C3PO,…) — name, height, friends,

    
 primary function.. Humans (Luke, Leia, Solo,…) — name, height, friends, planet…
  44. — Common fields between Human and Droid — name —

    height — friends — appearsIn Create an interface
  45. Character Interface

  46. Droid GraphQL Object

  47. Human GraphQL Object

  48. Enum Types

  49. Schema Structure

  50. Query Type

  51. Parsing Queries and Returning Results

  52. None
  53. — Client and server Java GraphQL implementations are not stable.

    — No Kotlin only GraphQL implementation. — Learning curve. — Need to setup GraphQL server on top of your infrastructure. This requires more work. Disadvantages
  54. Thanks! May the force be with you! Twitter: @heyitsmohit Github:

    http://github.com/mysa