$30 off During Our Annual Pro Sale. View Details »

GraphQLサーバを作る苦しみと解決手法

mackee
July 21, 2020

 GraphQLサーバを作る苦しみと解決手法

mackee

July 21, 2020
Tweet

More Decks by mackee

Other Decks in Technology

Transcript

  1. GraphQL͓͞Β͍ • Web޲͚ΫΤϦݴޠ • ಉ͡໾ׂΛ͢Δ΋ͷ: OpenAPI, gRPC, JSON-RPC… • GitHubͳͲͷWeb

    APIͰ࠾༻ྫ͋Γ • `/graphql`Έ͍ͨͳ୯ҰͷΤϯυϙΠϯτʹΫΤϦΛ౤͛Δ • Ϩεϙϯε͸JSON͕Α͘࢖ΘΕΔ(͕ɺผʹԿͰ΋ྑ͍͸ͣ)
  2. GraphQLɺԿ͕͏Ε͍͠ͷʁ • ܕ͕͋ͬͯૉ੖Β͍͠ • ܕ໊ʹ`!`͕ͭ͘ͱnot nullable • ඞཁͳfieldͷߜΓࠐΈ΍ɺҰॹʹऔΓ͍ͨϦιʔεͷҰׅऔಘ΋Ͱ͖Δ • ࣹӨ΍batch

    requestΈ͍ͨͳ࢓૊ΈΛ࡞Γࠐ·ͣʹࡁΉ • पลπʔϧ/ϥΠϒϥϦ͕͍ͬͺ͍͋ͬͯૉ੖Β͍͠ • GraphiQL, apollo-client/apollo-server ͳͲͳͲ
  3. GraphQLɺԿ͕͏Ε͍͠ͷʁ • ܕ͕͋ͬͯૉ੖Β͍͠ • ܕ໊ʹ`!`͕ͭ͘ͱnot nullable • ඞཁͳfieldͷߜΓࠐΈ΍ɺҰॹʹऔΓ͍ͨϦιʔεͷҰׅऔಘ΋Ͱ͖Δ • ࣹӨ΍batch

    requestΈ͍ͨͳ࢓૊ΈΛ࡞Γࠐ·ͣʹࡁΉ • पลπʔϧ/ϥΠϒϥϦ͕͍ͬͺ͍͋ͬͯૉ੖Β͍͠ • GraphiQL, apollo-client/apollo-server ͳͲͳͲ ←ͤ΍ͳͰ΋αʔόɺPerl΍ͶΜ… ɹundefେ׻ܴ΍ͶΜ… ↑͑ɺͭ·Γൃߦ͞ΕΔSQL͕ݻఆͰ͖ͳ͍ʁʁ ɹࠔΔͷͰ͸ʁʁʁ ↑ͦΕPerlʹ͋Δͷʁʁʁʁʁ
  4. GraphQLΛຊ֨తʹ࢖͏લ·Ͱͷҹ৅(2) • GitHub API v4(GraphQL)͕ग़ͨͱ͖ʹΘʔ͍ͱ৮ͬͯΈͨͱ͖ͷײ૝ • GraphQLݴޠΛ֮͑Δͷ͕೉ղ(ʹݟ͑ͨ) • ୯ͳΔJSONͷ೿ੜͰ͸ͳ͍ಠࣗDSL •

    ͪΐͬͱෳࡶͰωετ͕ਂͯ͘ྔ͕ଟ͍ΫΤϦΛൃߦ͢Δͱ500͕ฦ͖ͬͯͨ • ʮGitHubͰ͢Β͜͏ͳΔΜ͔ͩΒզʑʹ͸…ʯ • ͨͿΜࠓ͸ComplexityΛܭࢉͯ͠200Ͱฦͤͳ͍Αͱݴͬͯ͘ΔͷͰ͸ͳ͍͔
  5. PerlͷGraphQL.pm • ࠷ઌ୺ͷϞμϯPerl • Function::Parameters • Return::Type • ܕ͕ΨνΨνʹॻ͔Ε͍ͯΔ •

    ͔͠͠Perl͸ಈతܕ෇͚ݴޠͳͷͰಈతʹ ܕνΣοΫ͠·͢ • ↑͕͜͜ϘτϧωοΫʹͳΔ • ܕνΣοΫΛແޮԽ͢ΔΑ͏ʹ Function::ParametersΛ͍͡ΔͱΫΤϦ୯Ґ Ͱݟͯ3ഒߴ଎Խ͞Εͨ
  6. GraphQL APIͷߴ଎ԽͷҊ • GraphQL.pm͓ΑͼFunction::ParametersΛ͍ͬͯ͡Pull RequestΛग़͢ • ܕνΣοΫແޮԽ͸ຊମΛ͍͡Δ͔͠ແ͍ • ͕ɺ͜ͷPull Request͕Authorʹཧղ͞ΕΔࣗ৴͕ͳ͍…͋ͱͬ͞͞ͱղܾ͍ͨ͠

    • Ωϟογϡ͢Δ • GraphQLͰ࢖ΘΕΔresolver΍Ϧιʔε୯ҐͷΩϟογϡͰ͸ແҙຯ • GraphQLΛύʔεͨ࣌͠఺Ͱෛ͚͕֬ఆ͍ͯ͠Δ • GraphQLΛGraphQL.pmͰύʔε͢ΔલʹϨεϙϯεΩϟογϡΛฦ͢ => ࠾༻
  7. ෳࡶͳΫΤϦʹର͢Δख๏ • Query Complexity • ΫΤϦͷResolverΛ࣮ߦ͢Δલʹॏ͍ΫΤϦ͡Όͳ͍͔ௐ΂Δ • ۩ମతʹ͸ϖʔδϯάΛڧ੍͢ΔͳͲͯ͠औಘ͢Δ࠷େ݅਺ΛΫΤϦ Ͱ֬ఆͤ͞Δ =>

    ᮢ஋Λ΋͏͚ͯ஄͘ • ϖʔδϯάܗࣜ => Relay Server Specification • Ҿ਺΍ϨεϙϯεܗࣜʹσϑΝΫτελϯμʔυ͕͋ΔͷͰ͜ΕΛ࢖͏
  8. ·ͱΊ • GraphQLʹର͢Δ఍߅ײ͸Կͩͬͨͷ͔͕গ͠Θ͔ͬͨ • ͨͿΜࠓ·Ͱͷαʔόͷ࡞ΓํΛ੍ݶ͢Δ࢓༷ • ৽͍͠ύϥμΠϜͱ͢ΔͱɺڵຯΛ࣋ͬͯऔΓ૊Ίͨ • ৽͍ٕ͠ज़Λ࠾༻͢Δ͜ͱ͸ɺͦͷٕज़ͷະདྷʹϕοτ͢Δ͜ͱ •

    ϕοτ͢Δͱ͍͏͜ͱ͸ɺ͍͟ͱͳΕ͹OSSʹPull RequestΛग़ͨ͠Γɺϓ ϩδΣΫτ಺ʹ޿ΊΔΑ͏ͳओମతͳಈ͖͕ٻΊΒΕΔ • ͜͏΍ͬͯࣄྫΛग़͢ͷ΋ߩݙ͔΋͠Εͳ͍ͱࢥͬͯ΍ͬͯ·͢
  9. ͭΒ͍΍ͭ: root queryͷnode • Relay Server Specificationʹྫࣔ͞Εͯ ͍Δ΍ͭ • ผʹඞਢͰ͸ͳ͍͕ɺΫϥΠΞϯτଆ͸

    ͋Δͱศར • ΫϥΠΞϯταΠυͷΩϟογϡߋ৽ͷ ͨΊʹNodeͩͬͨΒͳΜͰ΋ฦͤΔ΍ͭ • αʔόαΠυ͸`ID`͚ͩͰͲͷܕ͔Λ൑ఆ ͠ͳ͚Ε͹ͳΒͳ͍