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

サーバサイドKotlinでGraphQLをやってみよう - Kotlin Fest 2019 ...

Shiraji
August 24, 2019

サーバサイドKotlinでGraphQLをやってみよう - Kotlin Fest 2019 - / Server side Kotlin + GraphQL

Shiraji

August 24, 2019
Tweet

More Decks by Shiraji

Other Decks in Programming

Transcript

  1. Who am I? • Kotlin + GraphQL 1+ year •

    Open Source kotlin-graphql-sample • Love Kotlin • Works for Ubie, Inc @shiraji @shiraj_i
  2. GraphQLͱ͸Կʁ • A query language for your API • εΩʔϚͱΫΤϦ

    • GraphQL Fundation • OSS • e.g. GitHub API v4
  3. εΩʔϚͱΫΤϦ type Query { user(id: ID!) : User } type

    Mutation { createUser(name: String!) } type User { id: ID! name: String! account: String }
  4. εΩʔϚͱΫΤϦ type Query { user(id: ID!) : User } type

    User { id: ID! name: String! account: String }
  5. εΩʔϚͱΫΤϦ type Query { user(id: ID! ) a : User

    } type User {a id: ID! name: String! account: String }a
  6. εΩʔϚ type Query { users: [User!]! } type User {

    id: ID! name: String! first_name: String! twitter: String! icon: String! }
  7. ΫΤϦ query user() { name } query user() { first_name

    } query user() { twitter } query user() { Id } query user() { Icon }
  8. εΩʔϚ type Drug { id: String! name: String! yjCode: String!

    description: String } type Disease { icd: String! name: String! }
  9. εΩʔϚ data class Drug( val id: String, val name: String,

    val yjCode: String, val description: String? ) data class Disease( val icd: String, val name: String )
  10. Query type Query { drug(id: String!) : Drug drugs(yjCode: String!)

    : [Drug!]! diseases(icd: String!) : [Disease!]! }
  11. εΩʔϚ @Component class DrugQueryResolver : GraphQL Query Resolver {a fun

    drugs(yjCode: String ! ) : List< Drug > = TODO() }a
  12. εΩʔϚ @Component class DrugQueryResolver : GraphQL Query Resolver { fun

    drugs(yjCode: String ! ) : List< Drug > = TODO() }
  13. Query type Query { drug(id: String! ) a : a

    Drug drugs(yjCode: String! ) a : a [Drug!]! diseases(icd: String! ) a : a [Disease!]! }
  14. Query @Component class DrugQueryResolver : GraphQLQueryResolver { fun drug(id: String

    ) a : a Drug? = TODO() fun drugs(yjCode: String ) a : a List< Drug > = TODO() } @Component class DiseaseQueryResolver : GraphQLQueryResolver { fun diseases(icd: String ) a : a List< Disease > = TODO() }
  15. ෮श • εΩʔϚ͕ॏཁ • type͕͋Δ • KotlinͰ͸data classͰදݱͰ͖Δ • type

    Query(ͱMutation)͕ಛघ • GraphQLQueryResolverͷϝιουʹ Ϛοϐϯά͞ΕΔ type Drug { id: ID! name: String! description: String } type Query { drugs(code: String) : [Drug]! }
  16. ෮श໰୊ type User { name: String } data class User(

    val name: String ) data class User( val name: String? ) data class User( val name: List<String> )
  17. ෮श໰୊ type User { name: String } data class User(

    val name: String ) data class User( val name: String? ) data class User( val name: List<String> )
  18. typeͷؔ܎ type Drug { id: ID! name: String! yjCode: String!

    description: String kinkiDiseases: [Disease!]! } type Disease { icd: String! name: String! kinkiDrugs: [Drug!]! } data class Drug( val id: String, val name: String, val yjCode: String, val description: String? ) data class Disease( val icd: String, val name: String )
  19. type Drug { id: ID! name: String! yjCode: String! description:

    String kinkiDiseases: [Disease!]! } data class Drug( val id: String, val name: String, val yjCode: String, val description: String? ) εΩʔϚͱKotlinͷϚοϐϯά·ͱΊ
  20. type Drug { id: ID! name: String! yjCode: String! description:

    String kinkiDiseases: [Disease!]! } class DrugResolver : GraphQLResolver<Drug> { fun kinkiDiseases(drug: Drug): List<Disease> {…} } εΩʔϚͱKotlinͷϚοϐϯά·ͱΊ
  21. type Drug { id: ID! name: String! yjCode: String! description:

    String kinkiDiseases: [Disease!]! } data class Drug( val id: String, val name: String, val yjCode: String, val description: String? ) class DrugResolver : GraphQLResolver<Drug> { fun kinkiDiseases(drug: Drug): List<Disease> {…} } εΩʔϚͱKotlinͷϚοϐϯά·ͱΊ
  22. DataFetchingEnvironment @Component class DrugQueryResolver : GraphQLQueryResolver { fun drug( id:

    String, environment: DataFetchingEnvironment): Drug? { TODO() }a }a
  23. DataFetchingEnvironment @Component class DrugQueryResolver : GraphQLQueryResolver { fun drug( environment:

    DataFetchingEnvironment, id: String): Drug? { TODO() }a }a ເͷ࿩Ͱ͢
  24. GraphQLContextBuilder @Bean fun contextBuilder(): GraphQLContextBuilder { return object : DefaultGraphQLContextBuilder()

    { override fun build( httpServletRequest: HttpServletRequest, httpServletResponse: HttpServletResponse ): GraphQLContext { }c }b }a
  25. GraphQLContextBuilder @Bean fun contextBuilder(): GraphQLContextBuilder { return object : DefaultGraphQLContextBuilder()

    { override fun build( httpServletRequest: HttpServletRequest, httpServletResponse: HttpServletResponse ): GraphQLContext { val authHeader = httpServletRequest.getHeader(“Authorization") }c }b }a
  26. GraphQLContextBuilder @Bean fun contextBuilder(): GraphQLContextBuilder { return object : DefaultGraphQLContextBuilder()

    { override fun build( httpServletRequest: HttpServletRequest, httpServletResponse: HttpServletResponse ): GraphQLContext { val authHeader = httpServletRequest.getHeader("Authorization") val token = authHeader?.removePrefix("Bearer ") val userId = TODO() // ͳΜͱ͔͢Δ }c }b }a
  27. GraphQLContextBuilder @Bean fun contextBuilder(): GraphQLContextBuilder { return object : DefaultGraphQLContextBuilder()

    { override fun build( httpServletRequest: HttpServletRequest, httpServletResponse: HttpServletResponse ): GraphQLContext { val authHeader = httpServletRequest.getHeader("Authorization") val token = authHeader?.removePrefix("Bearer ") val userId = TODO() return AuthContext(httpServletRequest, userId) }c }b }a
  28. DataFetchingEnvironment @Component class DrugQueryResolver : GraphQLQueryResolver { fun drug(id: String,

    environment: DataFetchingEnvironment): Drug? { TODO(“DrugΛऔಘ͢Δॲཧ”) }b }a
  29. DataFetchingEnvironment @Component class DrugQueryResolver : GraphQLQueryResolver { fun drug(id: String,

    environment: DataFetchingEnvironment): Drug? { environment.getContext<AuthContext>().userId ?: throw Exception(“ೝূ͞Εͯͳ͍Α”) TODO(“DrugΛऔಘ͢Δॲཧ”) }b }a
  30. Kotlin + GraphQLೝূ·ͱΊ • DataFetchingEnvironment͸֤छResolverͰΞΫηεՄೳ • ಠࣗͷContextΛ࡞੒Մೳ • GraphQLContextΛܧঝ͢Δ •

    Context͸GraphQLContextBuilder#build಺Ͱੜ੒ • Contextʹೝূ৘ใ࣋ͨͤͯResolver಺Ͱೝূ͢Δ