GraphQL on Scala

GraphQL on Scala

Introduction for GraphQL and implementing GraphQL API using Scala.

93bc8fb48f57c11e417dad9d26a2fb8a?s=128

petitviolet

June 28, 2018
Tweet

Transcript

  1. (SBQI2-PO4DBMB 'SJOHF$P -UE!QFUJUWJPMFU  

  2. ͰɺͩΕʁ w )JSPLJ,PNVSBTBLJ w 8FC্Ͱ͸!QFUJUWJPMFU w 'SJOHFגࣜձࣾ w 4DBMBྺ͸೥΄Ͳ 2

  3. ࠓ೔࿩͢͜ͱ w Կʹ(SBQI2-Λ࢖͍ͬͯΔͷ͔ w ͳͥ(SBQI2-Λ࢖͍ͬͯΔͷ͔ w 4DBMBͰ(SBQI2-͢Δ w ΍ͬͯΈͯͲ͏͔ 3

  4. Կʹ(SBQI2-Λ ࢖͍ͬͯΔͷ͔ 4

  5. Կʹ(SBQI2-Λ࢖͍ͬͯΔͷ͔ w ΞυωοτϫʔΫͷ؅ཧը໘ͷαʔόαΠυ"1* w ޿ࠂͷ഑৴ઃఆΛ؅ཧ͢Δը໘ͷཪଆ w ඇެ։಺෦"1*ͱͯ͠ͷ(SBQI2- w ΫϥΠΞϯτ͸&MNͰ41" w

    2VFSZ.VUBUJPOͲͪΒ΋(SBQI2- w Ұ෦ඇ(SBQI2- 5
  6. ͳͥ(SBQI2-Λ ࢖͍ͬͯΔͷ͔ 6

  7. ͳͥ(SBQI2-Λ࢖͍ͬͯΔͷ͔ w ʮ͑ͬɺ͜ͷϖʔδදࣔ͢Δͷʹճ΋"1*Λʁʯ w Կͱ͔͍͚ͨ͠Ͳ3&45ͱંΓ߹͍͕͔ͭͳ͍ w ʮԶ͸ҰମԿΛʜʯΛආ͚͔ͨͬͨ w 63*ઃܭ΍1045165%&-&5&Ͱ೰Ήͱ͔ w

    ྺ࢙ʹֶΜͩ w 3&45ɺૢ࡞ࢦ޲ɺ+40/31$ͳͲ w ۜͷ஄ؙΛ୳͢೔ʑ 7
  8. https://speakerdeck.com/petitviolet/web-api-design  

  9. https://speakerdeck.com/petitviolet/web-api-design Ͳ͏΍Β(SBQI2-͕͍͍Β͍͠  

  10. ͳͥ(SBQI2-Λ࢖͍ͬͯΔͷ͔ w ΫϥΠΞϯτଆʹ3FBEܥͷ੹຿Λد͍ͤͨ w %%%΍ͬͯͯ3FBEܥͷཁٻʹԠ͑Δͷ͕͖͍ͭ w 9YY3FQPTJUPSZ͕pOE#Z:ZZͩΒ͚ʹ w λΠϛϯά͕ྑ͔ͬͨ w

    ৽نϓϩδΣΫτൃ଍ w αʔόαΠυʹࣗ෼͕ΞαΠϯ͞Εͨ w (SBQI2-Θ͔ΔΤϯδχΞ͕ϑϩϯτʹ΋ 10
  11. 4DBMBͰ(SBQI2-͢Δ 11

  12. 4DBMBͰ(SBQI2-͢Δ w TBOHSJBHSBQIRMTBOHSJBΛ࢖͏ w 4DBMBͷ(SBQI2-ϑϨʔϜϫʔΫ w 8FCϑϨʔϜϫʔΫ͸ಛʹ໰Θͳ͍ w *'͸+40/Ͱɺ+40/ϥΠϒϥϦ΋ෳ਺ରԠ 12

  13. งғؾ case class MyObject(id: Long, name: String) val myObjectType: ObjectType[Unit,

    MyObject] = derive.deriveObjectType() val myQuery: ObjectType[MyObjectRepository, Unit] = { ObjectType.apply( "MyQuery", fields[MyObjectRepository, Unit]( { val idArg = Argument("id", LongType) Field( "findById", OptionType(myObjectType), arguments = idArg :: Nil, resolve = ctx => ctx.ctx.findById(ctx.arg(idArg))) }, Field("all", ListType(myObjectType), resolve = ctx => Future { ctx.ctx.findAll }) ) ) } 13
  14. งғؾ case class MyObject(id: Long, name: String) val myObjectType: ObjectType[Unit,

    MyObject] = derive.deriveObjectType() val myQuery: ObjectType[MyObjectRepository, Unit] = { ObjectType.apply( "MyQuery", fields[MyObjectRepository, Unit]( { val idArg = Argument("id", LongType) Field( "findById", OptionType(myObjectType), arguments = idArg :: Nil, resolve = ctx => ctx.ctx.findById(ctx.arg(idArg))) }, Field("all", ListType(myObjectType), resolve = ctx => Future { ctx.ctx.findAll }) ) ) } (SBQI2-ͷUZQF 14
  15. งғؾ case class MyObject(id: Long, name: String) val myObjectType: ObjectType[Unit,

    MyObject] = derive.deriveObjectType() val myQuery: ObjectType[MyObjectRepository, Unit] = { ObjectType.apply( "MyQuery", fields[MyObjectRepository, Unit]( { val idArg = Argument("id", LongType) Field( "findById", OptionType(myObjectType), arguments = idArg :: Nil, resolve = ctx => ctx.ctx.findById(ctx.arg(idArg))) }, Field("all", ListType(myObjectType), resolve = ctx => Future { ctx.ctx.findAll }) ) ) } ܕͷࣗಈಋग़ 15
  16. งғؾ case class MyObject(id: Long, name: String) val myObjectType: ObjectType[Unit,

    MyObject] = derive.deriveObjectType() val myQuery: ObjectType[MyObjectRepository, Unit] = { ObjectType.apply( "MyQuery", fields[MyObjectRepository, Unit]( { val idArg = Argument("id", LongType) Field( "findById", OptionType(myObjectType), arguments = idArg :: Nil, resolve = ctx => ctx.ctx.findById(ctx.arg(idArg))) }, Field("all", ListType(myObjectType), resolve = ctx => Future { ctx.ctx.findAll }) ) ) } DPOUFYUͱͯ͠ 3FQPTJUPSZΛ࢖༻ 16
  17. งғؾ case class MyObject(id: Long, name: String) val myObjectType: ObjectType[Unit,

    MyObject] = derive.deriveObjectType() val myQuery: ObjectType[MyObjectRepository, Unit] = { ObjectType.apply( "MyQuery", fields[MyObjectRepository, Unit]( { val idArg = Argument("id", LongType) Field( "findById", OptionType(myObjectType), arguments = idArg :: Nil, resolve = ctx => ctx.ctx.findById(ctx.arg(idArg))) }, Field("all", ListType(myObjectType), resolve = ctx => Future { ctx.ctx.findAll }) ) ) } OVMMBCMF͸0QUJPOͰදݱ 17
  18. งғؾ case class MyObject(id: Long, name: String) val myObjectType: ObjectType[Unit,

    MyObject] = derive.deriveObjectType() val myQuery: ObjectType[MyObjectRepository, Unit] = { ObjectType.apply( "MyQuery", fields[MyObjectRepository, Unit]( { val idArg = Argument("id", LongType) Field( "findById", OptionType(myObjectType), arguments = idArg :: Nil, resolve = ctx => ctx.ctx.findById(ctx.arg(idArg))) }, Field("all", ListType(myObjectType), resolve = ctx => Future { ctx.ctx.findAll }) ) ) } ඇಉظॲཧ 'VUVSF ͕؆୯ 18
  19. 4DBMBͰ(SBQI2-͢Δ w 4BOHSJB͸ػೳ͕े෼ʹଗ͍ͬͯΔ w ඇಉظॲཧ w ϚΫϩʹΑΔUZQFͷࣗಈಋग़ w +40/ͱͷ૬ޓม׵ w

    DBTFDMBTTͱ(SBQI2-Λ+40/͕ܨ͙ w / ໰୊ղܾ w 'FUDIFS%FGFSSFE3FTPMWFS w 3FMBZαϙʔτ w TBOHSJBHSBQIRMTBOHSJBSFMBZ 19
  20. (SBQI2-Λ4DBMBͰ ΍ͬͯΈͯͲ͏͔ 20

  21. (SBQI2-Λ࢖ͬͯΈͯͲ͏͔ w ײ૝ͱͯ͠ɺ(SBQI2-ʹͯ͠ྑ͔ͬͨ w ͍ΖΜͳ͜ͱ͔Βղ์͞Εͨ w ʮͱΓ͋͑ͣશ෦ฦ͔͢Β͋ͱΑΖ͘͠ʯͷָ͞ w ؔ࿈ΦϒδΣΫτ͸SFTPMWFͰؤு࣮ͬͯ૷ w

    ͿͬͪΌ͚Ͳ͏࢖ͬͯΔ͔Α͘஌Βͳ͍ w 4DBMBͱ(SBQI2-ܕ࢓༷ͷ૬ੑͷྑ͞ w ૉ௚ʹදݱͰ͖Δ҆͠શʹָʹ։ൃͰ͖Δ w OPOOVMMΛίϯύΠϥͰڧ੍Ͱ͖Δ 21
  22. ͠ΜͲ͍͜ͱɺ೰ΜͰ͍Δ͜ͱ w (SBQI2-ͷֶशίετ͸ͦΕͳΓʹ͔͔Δ w εΩʔϚͷ࡞Γํʹ೰Ή w ϨΠϠʔυΞʔΩςΫνϟͰͷཱͪҐஔ w ೝূ͕Ξμϓλ૚Ͱ΍Β͟ΔΛಘͳ͍ w

    &OVNΛυϝΠϯ૚ͱڞ༗͢Δ΂͖͔ 22
  23. ·ͱΊ 23

  24. (SBQI2-PO4DBMB w 4DBMBͰ(SBQI2-ɺे෼΍ΕΔ w TBOHSJBHSBQIRMTBOHSJBͷ͓͔͛ w (SBQI2-ͷ࢓༷ΛͪΌΜͱຬͨͤΔ w (SBQI2-΍ͬͯΈͯΑ͔ͬͨ w

    ࣍ʹԿ͔࡞Δͱͯ͠΋࠾༻͍ͨ͠ w ֶशίετ͸͔͔ΔͷͰνʔϜʹΑΔ 24
  25. 5IBOLZPV 25