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

サーバー間 GraphQL と webmock-graphql の話 / server-to-...

qsona
April 19, 2024

サーバー間 GraphQL と webmock-graphql の話 / server-to-server graphql and webmock-graphql

tsukiji.graphql x ハッカー鮨 で発表した資料です。
https://tsukiji-graphql.connpass.com/event/314173/

p.23 [Signed Query は GraphQL の Trusted Document の新しい実装パターンです - スタディサプリ Product Team Blog](https://blog.studysapuri.jp/entry/2024/04/15/100000)
p.25 https://twitter.com/koichik/status/1779697260227711305

qsona

April 19, 2024
Tweet

More Decks by qsona

Other Decks in Technology

Transcript

  1. • @qsona • Software Engineer at SHE Inc. • ։ൃ/ϓϩμΫτ૊৫ͷϦʔυΛ͍ͯ͠·͢

    • ΊͬͪΌ͍͍νʔϜɾձࣾͩΑ • (࠷ۙ͋Μ·Γίʔυ͔͍ͯͳ͍͚Ͳָ͠ΜͰ·͢!) whoami
  2. • provider ࢹ఺ • API ͷͲͷ෦෼͕࢖ΘΕ͍ͯΔ͔͕Θ͔Δ 
 => ޙํޓ׵ੑͷͳ͍มߋΛ͠΍͍͢ •

    ϑϩϯτ޲͚ͷ API ͱ΋ଟ͘ͷ࣮૷Λ࢖͍·ΘͤΔ • consumer ࢹ఺ • ཉ͍͠ܕͷσʔλ͕ฦͬͯ͘Δ৴པੑ͕͋Δ αʔόʔؒ GraphQL API ௨৴ͷεεϝ
  3. • provider (server) • ͋Μ·ΓมΘΒͳ͍ • ϑϩϯτ޲͚ͱ࣮૷Λڞ௨Խͤ͞Δ৔߹͸޻෉͕͍Δ • consumer (client)

    • Ωϟογϡͷ؅ཧ͕جຊతʹ͸ෆཁ (ͳͷͰͦͷ෼͸ָ) • ϓϩάϥϛϯάݴޠ ... ಛʹɺ࠷ۙϑϩϯτΤϯυͰ͸ಈతܕ෇͚ݴޠ͸ 
 ͋Μ·Γ࢖ΘΕͯͳ͍͕ɺαʔόʔͰ͸࢖ΘΕ͍ͯΔ αʔόʔؒ GraphQL ͱɺ 
 ϑϩϯτ-αʔόʔ GraphQL ͷ࣮૷؍఺Ͱͷҧ͍
  4. • Ruby ͷ GraphQL ΫϥΠΞϯτϥΠϒϥϦ͸ɺҰԠੈͷதʹଘࡏ͢Δ͕ • github-community-projects/graphql-client • shkan18/graphlient •

    ݸਓతʹ͸࢖ͬͯ΋࢖Θͳͯ͘΋Α͍ • ࢖Θͳ͍৔߹: ͨͩͷ HTTP ΫϥΠΞϯτΛͪΐͬͱ wrap ͢Δ • ࢖Θͳͯ͘΋͍͍ཧ༝ • ಈతܕ෇͚ݴޠͰ͋Δ => ܕੜ੒͕ෆཁ • ΩϟογϡϚωδϝϯτ͕ෆཁ GraphQL ΫϥΠΞϯτϥΠϒϥϦΛ࢖͏΂͖?
  5. • (1) HTTP ϨϕϧͰ stub ͢Δ • Ruby Ͱ͸ bblimke/webmock

    ͕ఆ൪ • stub_request(:post, url).with().to_return() • (2) API call Λϥοϓͨ͠ΫϥεΛ࡞ΓɺͦΕΛ stub ͢Δ • RSpec ͩͱ allow().to receive().and_return() ֎෦ API ݺͼग़͠ͷςετύλʔϯ
  6. • [͓͢͢Ί] (1) HTTP ϨϕϧͰ stub ͢Δ • [not͓͢͢Ί] (2)

    API call Λϥοϓͨ͠ΫϥεΛ࡞ΓͦΕΛ stub ͢Δ • جຊతʹɺΫΤϦ͸1ϢʔεέʔεҎ֎Ͱ࢖͍ճ͞ͳ͍΄͏͕͍͍ • => ΫΤϦ͸ϢʔεέʔεʹରԠͯ͠ϕλॻ͖ͨ͠΄͏͕͍͍ • ϥοϓͨ͠ΫϥεΛ࡞Δҙຯ͸͋Μ·Γͳ͍ɺΉ͠ΖϚΠφεͩΑ ֎෦ API ݺͼग़͠ͷςετύλʔϯ
  7. webmock Λ࢖͏ stub_request(:post, "another-backend.com/graphql"). with(body: { query: FooOperation::QUERY, variables: {

    bazId: "1" } }). to_return(body: { data: { a: 1, b: { c: 2, d: 3 } } }.to_json)
  8. • ͍ΖΜͳͱ͜ΖͰ stub_request ͕ඞཁʹͳΔ • ୯ମςετ, request spec, etc... •

    ͦͷݺͼग़͕͠ԞͷํͰ͋Ε͹͋Δ΄Ͳ stub ͢ΔՕॴ͕૿͑Δ • ΫΤϦʹϑΟʔϧυΛ௥Ճ/࡟আͨ͠ͱ͖ɺ 
 શͯͷ stub_request Λमਖ਼͢Δඞཁ͕͋Δ • େมͩ͠ɺΑ͘๨ΕΔ => ςετͷ৴པੑ͕Լ͕Δ webmock Λ࢖͏ => ϝϯςφϯε͕ਏ͍
  9. • ϝϯς΋͞Εͯͳ͍͠ README ΋ॻ͔Εͯͳ͍ • ୭͕͔ͭ͏Μ͡Ό (લ৬Ͱ͸࢖ΘΕͯΔ͸ͣ) • ͜ͷΠϕϯτ·Ͱʹ४උ͢Δ༧ఆͩͬͨͷͰ͕͢ 


    ࣌ؒͳ͔ͬͨɾɾɾ͝ΊΜͳ͍͞ɾɾɾ • ͍ͣΕʹͤΑχον͗͢ΔͷͰ͜ͷ࿩୊͸͜͜·Ͱw • ڵຯ͋Δํ࠙਌ձͰͥͻ! ޻ࣄத...
  10. • ຊ൪؀ڥͰୟ͚ΔΫΤϦΛɺ૝ఆ͞Ε͍ͯΔ΋ͷ͚ͩʹ੍ݶͰ͖Δ • => ηΩϡϦςΟతͳϦεΫͷܰݮ • ϖΠϩʔυʹΫΤϦશମΛࡌͤΔඞཁ͕ͳ͘ͳΔ • hash ஋ͱ͔Ͱ΍ΓͱΓ͢Δ

    • => (mutation Ͱ͸ͳ͘) query ͷͱ͖͸ 
 GET ϝιου͕࣮࣭తʹ࢖͑ΔΑ͏ʹͳΓɺΩϟογϡͳͲͰ༗ར Persisted Queries ͷεεϝ
  11. • API ͷެ։ઌ͕޿͍ํ͔Βॱʹ 
 Automatic Persisted Queries > Signed Query

    > 
 Persisted Queries 
 ͕༗ޮͳͷͰ͸ͳ͍͔ ߟ࡯