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

8d887e6605353ca5fb1e05f38564ef50?s=47 Shiraji
August 24, 2019

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

8d887e6605353ca5fb1e05f38564ef50?s=128

Shiraji

August 24, 2019
Tweet

Transcript

  1. 2.

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

    Open Source kotlin-graphql-sample • Love Kotlin • Works for Ubie, Inc @shiraji @shiraj_i
  2. 20.

    GraphQLͱ͸Կʁ • A query language for your API • εΩʔϚͱΫΤϦ

    • GraphQL Fundation • OSS • e.g. GitHub API v4
  3. 22.

    εΩʔϚͱΫΤϦ type Query { user(id: ID!) : User } type

    Mutation { createUser(name: String!) } type User { id: ID! name: String! account: String }
  4. 24.

    εΩʔϚͱΫΤϦ type Query { user(id: ID!) : User } type

    User { id: ID! name: String! account: String }
  5. 25.

    εΩʔϚͱΫΤϦ type Query { user(id: ID! ) a : User

    } type User {a id: ID! name: String! account: String }a
  6. 31.

    εΩʔϚ type Query { users: [User!]! } type User {

    id: ID! name: String! first_name: String! twitter: String! icon: String! }
  7. 32.

    ΫΤϦ query user() { name } query user() { first_name

    } query user() { twitter } query user() { Id } query user() { Icon }
  8. 38.

    εΩʔϚ type Drug { id: String! name: String! yjCode: String!

    description: String } type Disease { icd: String! name: String! }
  9. 39.

    εΩʔϚ 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. 40.

    Query type Query { drug(id: String!) : Drug drugs(yjCode: String!)

    : [Drug!]! diseases(icd: String!) : [Disease!]! }
  11. 43.

    εΩʔϚ @Component class DrugQueryResolver : GraphQL Query Resolver {a fun

    drugs(yjCode: String ! ) : List< Drug > = TODO() }a
  12. 44.

    εΩʔϚ @Component class DrugQueryResolver : GraphQL Query Resolver { fun

    drugs(yjCode: String ! ) : List< Drug > = TODO() }
  13. 45.

    Query type Query { drug(id: String! ) a : a

    Drug drugs(yjCode: String! ) a : a [Drug!]! diseases(icd: String! ) a : a [Disease!]! }
  14. 46.

    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. 47.
  16. 48.
  17. 49.

    ෮श • εΩʔϚ͕ॏཁ • type͕͋Δ • KotlinͰ͸data classͰදݱͰ͖Δ • type

    Query(ͱMutation)͕ಛघ • GraphQLQueryResolverͷϝιουʹ Ϛοϐϯά͞ΕΔ type Drug { id: ID! name: String! description: String } type Query { drugs(code: String) : [Drug]! }
  18. 50.

    ෮श໰୊ type User { name: String } data class User(

    val name: String ) data class User( val name: String? ) data class User( val name: List<String> )
  19. 52.

    ෮श໰୊ type User { name: String } data class User(

    val name: String ) data class User( val name: String? ) data class User( val name: List<String> )
  20. 54.

    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 )
  21. 61.

    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ͷϚοϐϯά·ͱΊ
  22. 62.

    type Drug { id: ID! name: String! yjCode: String! description:

    String kinkiDiseases: [Disease!]! } class DrugResolver : GraphQLResolver<Drug> { fun kinkiDiseases(drug: Drug): List<Disease> {…} } εΩʔϚͱKotlinͷϚοϐϯά·ͱΊ
  23. 63.

    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ͷϚοϐϯά·ͱΊ
  24. 70.

    DataFetchingEnvironment @Component class DrugQueryResolver : GraphQLQueryResolver { fun drug( id:

    String, environment: DataFetchingEnvironment): Drug? { TODO() }a }a
  25. 71.

    DataFetchingEnvironment @Component class DrugQueryResolver : GraphQLQueryResolver { fun drug( environment:

    DataFetchingEnvironment, id: String): Drug? { TODO() }a }a ເͷ࿩Ͱ͢
  26. 76.

    GraphQLContextBuilder @Bean fun contextBuilder(): GraphQLContextBuilder { return object : DefaultGraphQLContextBuilder()

    { override fun build( httpServletRequest: HttpServletRequest, httpServletResponse: HttpServletResponse ): GraphQLContext { }c }b }a
  27. 77.

    GraphQLContextBuilder @Bean fun contextBuilder(): GraphQLContextBuilder { return object : DefaultGraphQLContextBuilder()

    { override fun build( httpServletRequest: HttpServletRequest, httpServletResponse: HttpServletResponse ): GraphQLContext { val authHeader = httpServletRequest.getHeader(“Authorization") }c }b }a
  28. 78.

    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
  29. 79.

    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
  30. 80.

    DataFetchingEnvironment @Component class DrugQueryResolver : GraphQLQueryResolver { fun drug(id: String,

    environment: DataFetchingEnvironment): Drug? { TODO(“DrugΛऔಘ͢Δॲཧ”) }b }a
  31. 81.

    DataFetchingEnvironment @Component class DrugQueryResolver : GraphQLQueryResolver { fun drug(id: String,

    environment: DataFetchingEnvironment): Drug? { environment.getContext<AuthContext>().userId ?: throw Exception(“ೝূ͞Εͯͳ͍Α”) TODO(“DrugΛऔಘ͢Δॲཧ”) }b }a
  32. 82.

    Kotlin + GraphQLೝূ·ͱΊ • DataFetchingEnvironment͸֤छResolverͰΞΫηεՄೳ • ಠࣗͷContextΛ࡞੒Մೳ • GraphQLContextΛܧঝ͢Δ •

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