GraphQL で変わったこと・変わらなかったこと / graphql changing and unchanging

942bb606679caf4c57b38927f83178e1?s=47 qsona
June 02, 2020

GraphQL で変わったこと・変わらなかったこと / graphql changing and unchanging

942bb606679caf4c57b38927f83178e1?s=128

qsona

June 02, 2020
Tweet

Transcript

  1. GraphQL ͰมΘͬͨ͜ͱɾ มΘΒͳ͔ͬͨ͜ͱ 2020-06-02 ٢঵ࣉ.pm22 @qsona

  2. whoami • @qsona • Web Developer at Quipper Limited •

    ͖ͪͽʔॳࢀՃ!!! ͏Ε͍͠!!! ٢঵ࣉԕ͍!!!
  3. ࠓ೔ͷ࿩ • ͜Ε·ͰԿ౓͔ GraphQL & Rails Ͱ API ։ൃΛͯ͠
 ຊ൪ϦϦʔεͨ͠

    • RESTful ࣌୅ͱʮมΘͬͨ͜ͱɾมΘΒͳ͔ͬͨ͜ͱʯ
 ʹ͍ͭͯ࿩͍ͨ͠
  4. લఏ • Web API ͸ Internal use Λ૝ఆ͢Δ • πʔϧɾΤίγεςϜͷ࿩͸Ұ੾͠ͳ͍

    • APIͷઃܭɾ࣮૷ͷ࿩͚ͩ • ਅͷREST (Hypermedia API) ͷ࿩͸͠ͳ͍
  5. Web API ͷઃܭख๏ • Usecase ϕʔε • Resource ϕʔε

  6. Usecase-based Web API • ΫϥΠΞϯτͷϢʔεέʔεʹ߹Θͤͯ Web API Λ࡞Δ • Ϣʔεέʔε

    ≒ ը໘ • ໰୊఺ • ࣅͨΑ͏ͳAPI͕ཚཱ͠΍͍͢ • ຖ౓API࡞Δͷ͕໘౗ =>
 ෳ਺ͷϢʔεέʔεʹରԠ͢Δ ਆAPI ͕Ͱ͖Δ܏޲
  7. Resource-based Web API • ΞϓϦέʔγϣϯͦͷ΋ͷͷ࢓༷ʹ߹Θͤͯ Web API Λ࡞Δ • (جຊతʹ͸

    API ఏڙଆͷ౎߹Ͱ࡞Δ)
  8. Resource-based Web API • ໰୊఺: ΫϥΠΞϯτͷϢʔεέʔεʹ߹Θͳ͍͕࣌͋Δ • 1ը໘Ͱෳ਺ͷAPIϦΫΤετ͕ඞཁʹͳͬͨΓ • ٯʹ1ϦΫΤετதͰෆཁͳϑΟʔϧυ΋ؚΊͯऔͬͯ͠·͏͜ͱ͕͋Δ

    • (=> ύϑΥʔϚϯεͷѱԽ) • Resource ͕ࡉ͔͘ͳΓ͗ͯ͢ (ςʔϒϧ୯ҐͳͲ)
 ΫϥΠΞϯτ͔Β࢖͍ʹ͍͘͜ͱ͕͋Δ
  9. Resource • ݸਓతʹɺ
 Recourse-based API ʹ͓͍ͯ
 JSONͷ1֊૚͝ͱͷ෦෼Λ Resource ͱݺΜͰ͍·͢

  10. RESTful ࣌୅ͷࢦ਑

  11. RESTful ࣌୅ͷࢦ਑ • جຊతʹ Resource ϕʔεΛબ୒͢Δ • Resource Λద੾ͳཻ౓Ͱఏڙ͢Δ •

    ςʔϒϧ୯ҐͰ͸ͳ͍ • ΞϓϦέʔγϣϯ্ͷυϝΠϯϞσϧΛߟ࡯͠ɺͦΕʹ߹ΘͤΔ • Ͳ͏ͯ͠΋ඞཁͳΒผ్ Usecase ༻ͷ endpoint Λੜ΍͢
  12. GraphQL • Schema ͕ Type ͷ άϥϑߏ଄ʹͳ͍ͬͯΔ • query Ͱ

    field Λࢦఆ͢Δ͜ͱͰɺ
 άϥϑͷதͷऔಘ͢Δ෦෼ΛࢦఆͰ͖Δ
  13. GraphQL ࣌୅ͷࢦ਑ • GraphQL Type ͕ Resource Λද͢ • query

    ͕ Usecase Λද͢ • Query Type ͷ field ͕ endpoint Ͱ͋Δ • ͭ·ΓɺResource ϕʔεͷAPIͱ Usecase ϕʔεͷAPIͷ
 ྑ͍ͱ͜ΖΛཱ྆Ͱ͖Δ!
  14. RESTful => GraphQL Ͱ มΘΒͳ͔ͬͨ͜ͱ

  15. มΘΒͳ͔ͬͨ͜ͱ • ΞϓϦέʔγϣϯͷυϝΠϯΛ͖ͪΜͱߟ࡯ͯ͠ɺ
 Resource Λద੾ͳཻ౓ʹ͢Δ͜ͱ • Fat Controller, Fat Serializer

    Λආ͚Δ͜ͱ • Model ෦෼ͷઃܭख๏
  16. RESTful => GraphQL Ͱ มΘͬͨ͜ͱ

  17. มΘͬͨ͜ͱ • ઃܭɾ࣮૷ϨϕϧͰมΘΔ͜ͱ͸΄ͱΜͲͳ͔ͬͨ • RESTful ͷͱ͖ͱରԠ͢Δ֓೦͕͋Δ • ͱ͸͍͑ɺසग़ͷ໰୊͸...? • N+1

    ໰୊ • HTTP endpoint ͕શͯ POST /graphql ʹͳΔ໰୊
  18. N+1໰୊ • Query.users Λ resolve • ͦͷ1݅ͣͭʹରͯ͠ User.jobs Λ
 resolve

  19. N+1໰୊ • ಉ͡໰୊͸ RESTful Ͱ΋ى͖Δ • GraphQL ʹ͓͍ͯͳͥΫϩʔζΞο ϓ͞ΕΔ?

  20. N+1໰୊ • GraphQL ʹ͓͍ͯ͸ӈͷΑ͏ʹɺ
 ΫϥΠΞϯτ͔Β2௨ΓͷऔΓํ͕γϡο ͱͰ͖Δɺͭ·Γศར • ศར͔ͩΒى͖͍ͯΔ͚ͩ • ղܾ๏:

    Batch Loader ͱݺ͹ΕΔ࢓૊Έ • Ruby: shopify/graphql-batch
  21. HTTP endpoint ͕શͯ POST /graphql ʹͳΔ໰୊ • Persisted Query ͱݺ͹ΕΔ࢓૊ΈͰղܾͰ͖Δ

    • ࣄલ / ϥϯλΠϜ ʹΫΤϦΛొ࿥͢Δ
  22. ઃܭ/(୯७ͳ)࣮૷ΑΓ֎ͷ࿩ The NEXT of REST - onk.ninja https://blog.onk.ninja/2017/09/21/the_next_of_rest

  23. ͜͜ͰҰ۟ ා͘ͳ͍ GraphQL is Better REST