Slide 1

Slide 1 text

GraphQL ͱ Prisma ͔Βߟ͑Δ ࣍ͷN೥Λݟਾٕ͑ͨज़બఆ @qsona 2021-11-27 JSConf JP

Slide 2

Slide 2 text

whoami • @qsona • Web Engineer at ελσΟαϓϦ (ϦΫϧʔτ) • Node.js / Rails / GraphQL / Microservices

Slide 3

Slide 3 text

@qsona ͱ Node.js • ιϑτ΢ΣΞΤϯδχΞͱͯ͠ॳΊͯݱ৔ͰίʔυΛॻ͍ͨͷ͕
 αʔόʔαΠυ Node.js Ͱͨ͠ (!!) • Node v0.10, EcmaScript 5 • Node ֶԂͷίϛϡχςΟʹҭͯͯ΋ΒͬͨͷͰɺ
 ࠓ೔ͷ৔ͰొஃͰ͖ͯͱͯ΋͏Ε͍͠Ͱ͢!

Slide 4

Slide 4 text

ຊൃදͷ֓ཁ • ͜ͷൃද͸ɺ
 @_yasaichi ͞ΜʹΑΔൃද
 ʮActive Record͔Βߟ͑Δ
 ࣍ͷ10೥Λݟਾٕ͑ͨज़બఆʯ (2021/09) ΁ͷ
 Ξϯαʔൃද (?) Ͱ͢ https://speakerdeck.com/yasaichi/architecture-decision-for-the-next-10-years-at-pixta

Slide 5

Slide 5 text

͓͜ͱΘΓ • ͜ͷࢿྉʹ͓͚Δจ੹͸͢΂ͯ @qsona ʹ͋Γ·͢ • ઌߦൃදͷ @_yasaichi ͞ΜͷҙਤɾχϡΞϯεΛ
 Ͱ͖Δ͚ͩਖ਼͘͠἞Ή౒ྗΛ͍ͯ͠·͕͢ɺ
 ಺༰ʹٙ໰౳͋Δ৔߹ɺݩͷࢿྉΛ௚઀ࢀর͍ͯͩ͘͠͞

Slide 6

Slide 6 text

ઌߦൃදʹ͓͚Δٕज़બఆࣄྫ • @_yasaichi ͞Μͷॴଐ͢Δ૊৫Ͱ͸ɺ΋ͱ΋ͱ Ruby on Rails Λ׆༻͓ͯ͠ Γɺಛʹ Active Record ͕΋ͨΒ͢։ൃੜ࢈ੑΛߴ͘ධՁ͍ͯ͠Δ • ͔͠͠ɺϑϩϯτΤϯυ (TypeScript) ͱόοΫΤϯυ (Ruby) Ͱ։ൃݴޠ͕ ҧ͏͜ͱʹΑΔɺೝ஌ෛՙͷ૿େ͕՝୊ʹͳͬͨ • ૊৫શମͱͯ͠ɺόοΫΤϯυΛ Node.js + TypeScript Ͱ։ൃ͢Δબ୒ࢶΛ
 ৽ͨʹऔΓೖΕΔܾஅΛͨ͠

Slide 7

Slide 7 text

ઌߦൃදʹ͓͚Δٕज़બఆࣄྫ • Node.js + TypeScript Ͱͷ O/R Mapper ϥΠϒϥϦͱͯ͠ɺ Prisma ͷ࠾༻Λݕ౼ͨ͠ • ͔͠͠ɺ։ൃதظҎ߱ͷ։ൃੜ࢈ੑͷ௿ԼΛݒ೦͠ɺ
 Ұ୴࠾༻ΛݟૹΔܾஅΛͨ͠ • ৄ͍͠ཧ༝͸ɺຊൃදͷୈೋ෦Ͱ৮Ε·͢

Slide 8

Slide 8 text

ຊൃදͰͷٕज़બఆࣄྫ • @qsona ͕ॴଐ͢ΔɺελσΟαϓϦ։ൃͷࣄۀ෦ (چ Quipper) ಺Ͱͷɺ
 ৽نαʔϏε։ൃࣄྫ • ͦͷதͷҰ෦ͷ(ϚΠΫϩ)αʔϏεͰɺ
 Node.js + TypeScript + Prisma Λ࠾༻ͨ͠ (2020/08 ͝Ζͷҙࢥܾఆ) • ผͷαʔϏεͰ͸ Ruby on Rails ΋Ҿ͖ଓ͖࠾༻ • ※ ઌߦൃදʹൺ΂ͯɺείʔϓͷڱ͍ҙࢥܾఆͰ͋Δ఺ʹ஫ҙ

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

ೋͭͷٕज़બఆͷൺֱ • ͳͥ @qsona ͷॴଐ͢ΔνʔϜͰ͸
 Prisma Λ࠾༻ͨ͠ͷ͔? • ͳͥ @_yasaichi ͞Μͷॴଐ͢Δ૊৫Ͱ͸
 Prisma ͷ࠾༻Λݟૹͬͨͷ͔? • @qsona ͷॴଐ͢ΔνʔϜͰ͸ɺ@_yasaichi ͞Μ͕ද໌ͨ͠ݒ೦ʹ
 ରॲͰ͖͍ͯΔͷ͔?

Slide 11

Slide 11 text

ຊൃදͷߏ੒ • ४උ: Prisma ͱ͸ɺͦͷ։ൃମݧ • ୈҰ෦: ελσΟαϓϦʹ͓͚Δ GraphQL + Prisma ࠾༻ࣄྫͱ ٕज़બఆͷཧ༝ • ୈೋ෦: Active Record ͱରൺͯ͠ߟ͑Δ
 Prisma ͱ։ൃதظҎ߱ͷ։ൃੜ࢈ੑ

Slide 12

Slide 12 text

४උ: Prisma ͱ͸ɺͦͷ։ൃମݧ

Slide 13

Slide 13 text

https://www.prisma.io/

Slide 14

Slide 14 text

Prisma ͱ͸ • Database toolkit (O/R Mapper ΛؚΉ) • Node.js + TypeScript ͕લఏ • PostgreSQL, MySQL, MongoDB ͳͲʹରԠ • ͜ͷൃදͷείʔϓͰ͸ PostgreSQL / MySQL Λ૝ఆ

Slide 15

Slide 15 text

Prisma Λར༻ͨ͠։ൃϑϩʔ • ·ͣ Prisma Schema Λهड़͢Δ https://www.prisma.io/docs/concepts/components/prisma-schema

Slide 16

Slide 16 text

Prisma Λར༻ͨ͠։ൃϑϩʔ • Prisma schema ͔ΒҎԼͷ2͕ͭࣗಈੜ੒͞ΕΔ • DB ϚΠάϨʔγϣϯͷίʔυ (Prisma Migrate) • ΫΤϦϏϧμʔ (ORM) ͷίʔυ (Prisma Client) Rails Ϣʔβʔ޲͚৘ใ: ridgepole (cookpad) ͱಉ͡։ൃϑϩʔ

Slide 17

Slide 17 text

Prisma Λར༻ͨ͠։ൃϑϩʔ • Prisma Client Λར༻ͯ͠
 ΞϓϦέʔγϣϯͷϩδοΫ ΛίʔσΟϯά͢Δ https://www.prisma.io/docs/concepts/components/prisma-client

Slide 18

Slide 18 text

Prisma Λར༻ͨ͠։ൃମݧ ࢲݟ: ։ൃମݧ͕ඇৗʹྑ͍ • Prisma schema Λத৺ͱͨ͠ɺҰ؏ͨ͠։ൃମݧ • ࠩ෼ Migration ·ͰΧόʔͯ͘͠ΕΔ • TypeScript ͷ੩తܕ͕όνόνʹޮ͘ • ࢖ͬͯΈΕ͹Θ͔Γ·͢ʂʂʂʂ (౤͛΍Γ)

Slide 19

Slide 19 text

ୈҰ෦: ελσΟαϓϦʹ͓͚Δ GraphQL + Prisma ࠾༻ࣄྫͱ ٕज़બఆͷཧ༝

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

https://studysapuri.jp/course/junior/release-2022-spring/

Slide 22

Slide 22 text

৽ن։ൃϓϩδΣΫτͷ঺հ • ελσΟαϓϦͷ͏ͪɺதֶੜͷϢʔβʔΛର৅ʹ
 αʔϏεΛϦχϡʔΞϧ͢ΔϓϩδΣΫτ • ։ൃ͢Δ΋ͷ • iOS / Android / PC Web Apps • Backend APIs • ίϯςϯπΛ؅ཧ͢ΔγεςϜ ֶशαʔϏεΛϑϧεΫϥονͰ ࡞ΔΠϝʔδ

Slide 23

Slide 23 text

্ஈͷٕज़తҙࢥܾఆ ߴ͍։ൃੜ࢈ੑΛ໨ࢦͯ͠ɺҎԼͷٕज़తҙࢥܾఆΛߦͬͨ • GraphQL Λશ໘తʹ׆༻͢Δ • Clients <=> API Gateway <=> Backend Services • Backend ͸
 main αʔϏεͱ content αʔϏεʹ෼཭ͯ͠։ൃΛਐΊΔ

Slide 24

Slide 24 text

ͦΕͧΕͷαʔϏεͷ੹຿ • content αʔϏε (ࠓճͷ࿩ͷϝΠϯ) • ֶशʹඞཁͳʮίϯςϯπʯΛ؅ཧ͢Δ੹຿Λ࣋ͭ • ໰୊ / ಈը / ͦΕΒͷ֊૚ߏ଄ ͳͲ • main αʔϏε • Ϣʔβʔͷֶशཤྺͷσʔλ΍ɺͦͷଞ
 ϢʔβʔσʔλΛ؅ཧ͢Δ੹຿Λ࣋ͭ • Ruby on Rails Λ࠾༻

Slide 25

Slide 25 text

ΞʔΩςΫνϟਤ (Ұ෦ൈਮ)

Slide 26

Slide 26 text

GraphQL Λશ໘తʹ࠾༻ͨ͠ཧ༝ • εΩʔϚۦಈ։ൃʹΑΓɺطଘ ͷ૊৫త՝୊ΛղܾͰ͖Δ • Client ͔Β Backend ·Ͱશͯ GraphQL ʹ͢Δ͜ͱͰ
 ։ൃੜ࢈ੑΛߴΊΔ͜ͱ͕Ͱ͖Δ

Slide 27

Slide 27 text

ࢀߟ: ॳखͰͷαʔϏε෼ׂͷੋඇ • αʔϏε։ൃͷॳखͰ͍͖ͳΓ(ϚΠΫϩ)αʔϏεʹ෼ׂ͢Δͷ͸
 ϦεΫ΋ߴ͘ɺҰൠతʹ͸નΊΒΕͳ͍ • ελσΟαϓϦͷ৔߹ɺ໿10೥ͷֶशαʔϏε։ൃܦݧ͔Β
 ϏδωευϝΠϯͷڥքΛ͋Δఔ౓ཧղ͍ͯ͠Δ • ࠓճ content αʔϏεΛ෼཭͢Δ͜ͱ͸ɺϦεΫΑΓ΋
 Ϧλʔϯ͕ଟ͍ͱ݁࿦͚ͮͨ

Slide 28

Slide 28 text

content αʔϏεͷٕज़બఆ • GraphQL • ݴޠ/؀ڥ: Node.js + TypeScript • ORM ϥΠϒϥϦ: Prisma

Slide 29

Slide 29 text

content αʔϏεͷٕज़બఆཧ༝ • νʔϜͷೳྗɾΦʔφʔγοϓ • GraphQL (ΤίγεςϜؚΉ) ͱͷ૬ੑ • ੩తܕ෇͚ (ΤίγεςϜؚΉ) ͱͷ૬ੑ • Read ͱ Write Ͱେผ͞ΕΔ
 αʔϏεಛੑͱͷ૬ੑ TypeScript + Node.js Λબ୒ͨ͠ ੵۃతཧ༝ Rails ActiveRecord Λආ͚Δ ফۃతཧ༝͕ͩ ͜ͷൃදʹ͓͚Δຊ୊

Slide 30

Slide 30 text

νʔϜͷೳྗɾΦʔφʔγοϓ ҎԼͷཧ༝ͰɺRuby on Rails ͱ Node.js + TypeScript ͷ2୒ʹߜͬͨ • Ruby on Rails • ελσΟαϓϦʹ͓͍ͯελϯμʔυͳબ୒Ͱ͋Γɺී௨ʹ࠾༻Մೳ • Node.js + TypeScript • qsona ͕ αʔόʔαΠυ Node.js ͷܦݧ͋Γɺ೤ҙ΋ڧ͍ • νʔϜͱͯ͠ TypeScript Λ໰୊ͳ͘࢖͑Δ (Web Frontend Ͱར༻) • ͦͷଞ͸ɺνʔϜͱͯ͠ೳྗ΍೤ҙΛ͍࣋ͬͯΔબ୒͸ಛʹͳ͔ͬͨ • (͜ΕҎ߱͸ Ruby on Rails ͱ Node.js + TypeScript ͱͷൺֱͰߟ͑Δ)

Slide 31

Slide 31 text

GraphQL (ΤίγεςϜؚΉ) ͱͷ૬ੑ • Ruby Ͱ΋໰୊ͳ͘ GraphQL Server Λ࣮૷Ͱ͖Δ • ڧ͍ͯݴ͏ͳΒ͹ TypeScript + Node.js ͷ΄͏͕ɺ
 ΑΓ GraphQL ͱ਌࿨ੑ͕ߴ͍ • ฒߦॲཧͱͷ૬ੑ͕ྑ͍ • JavaScript / TypeScript + GraphQL ͷํ͕ɺ
 ΤίγεςϜ͕੝Γ্͕͍ͬͯΔ

Slide 32

Slide 32 text

੩తܕ෇͚ (ΤίγεςϜؚΉ) ͱͷ૬ੑ αʔϏεͷಛੑ͔ΒɺTypeScript ͷ੩తܕ෇͚͕༗རͱߟ͑ͨ • ΄ͱΜͲͷܕఆٛΛɺࣗಈੜ੒ʹΑͬͯແྉͰಘΒΕΔ • ςʔϒϧఆٛ (Prisma) • GraphQL Schema (graphql-codegen/typescript-resolvers) • ίϯςϯπσʔλ (json-schema-to-typescript) • ೖྗࢧԉπʔϧͷͨΊʹ JSON Schema Λผ్ఆ͍ٛͯ͠Δ
 (ৄ͘͠͸ࢿྉࢀর) • ʮ໰୊ʯͷσʔλߏ଄ʹ TypeScript ͷ Union Types ͕Ϛον͢Δ • ໰୊ͷछྨ͝ͱʹɺ࣋ͭΧϥϜ͕มΘΔ • ੩తܕ෇͚ͷॿ͚ΛआΓͣʹѻ͏ͱɺϓϩάϥϛϯά্ͷϛε͕ى͖΍͍͢ https://speakerdeck.com/qsona/developing-masterdata-management-tool-by-using-vscode-extension

Slide 33

Slide 33 text

Read ͱ Write Ͱେผ͞ΕΔ
 αʔϏεಛੑͱͷ૬ੑ αʔϏεͷಛ௃ͱͯ͠ɺWrite ͱ Read ͷ܏޲͕શ͘ҟͳΔ • ϝΠϯͷτϥϑΟοΫͷ΄ͱΜͲ͕ Read • ΤϯυϢʔβʔ (ੜె) ͸ίϯςϯπΛॻ͖ࠐ·ͳ͍ • Write ͸ίϯςϯπνʔϜ͕৽͍͠ίϯςϯπσʔλΛຊ൪ϦϦʔε͢Δͱ͖͚ͩ • ଟ͘ݟੵ΋ͬͯ΋1೔਺ճఔ౓

Slide 34

Slide 34 text

Read ͱ Write Ͱେผ͞ΕΔ
 αʔϏεಛੑͱͷ૬ੑ αʔϏεͷಛ௃ͱͯ͠ɺWrite ͱ Read ͷ܏޲͕શ͘ҟͳΔ • υϝΠϯϩδοΫͷ΄ͱΜͲ͕ Write ʹू·Δ • Read ଆ͸ɺDB ʹೖ͍ͬͯΔσʔλߏ੒Λॊೈʹɺ͔ͭ
 ύϑΥʔϚϯεྑ͘ΫΤϦͰ͖ΔΑ͏ʹ͢Δ͜ͱ͕؊ͱͳΔ • Read ଆʹυϝΠϯϩδοΫ͸গͳ͍

Slide 35

Slide 35 text

Read ͱ Write Ͱେผ͞ΕΔ
 αʔϏεಛੑͱͷ૬ੑ • ͜ͷαʔϏεಛੑʹର͠ɺ
 CQS (Command-Query Separation) Pattern Λద༻Ͱ͖Δͱߟ͑ͨ • Command (ॻ͖ࠐΈܥ) ͱ 
 Query (ಡΈࠐΈܥ) ʹ
 γεςϜ্ͷίʔυΛେผ͢Δύλʔϯ τϥϑΟοΫ υϝΠϯ
 ϩδοΫ 3FBE େ গ 8SJUF ۃখ ଟ

Slide 36

Slide 36 text

ิ଍: ͜ͷΑ͏ͳಛੑ͸͋Δఔ౓ීวత ͦͷ๻ͷܦݧ্ɺ"DUJWF3FDPSEΛ࢖ͬͯΞϓϦέʔγϣϯΛ։ൃ͢Δ৔ ߹ɺࢀরܥͷػೳʢݕࡧͱ͔ʣ͸ύϑΥʔϚϯε໘Ͱ೰Ή͜ͱ͕ଟ͘ɺొ ࿥ɾߋ৽ܥͰ͸ػೳ໘ʢ࢓༷ͷෳࡶԽɺίʔυͷՄಡੑɺόάࠞೖ཰ʣͷ೰ Έ͕ଟ͘ͳΓ͕ͪͰ͋Δɻͱ͍͏͔ɺ͜Ε͸͓ͦΒ͘3BJMTʹݶͬͨ࿩Ͱ͸ ͳ͘ɺ͔ͩΒͦ͜$234ͱ͍͏ΞʔΩςΫνϟύλʔϯ͕஫໨͞Ε͍ͯΔΜ ͩͱࢥ͏
 Ҿ༻ݩʮαʔϏεΫϥεʹ͍ͭͯ͸๻΋ѱ͔ͬͨͱࢥ͍ͬͯΔ͚ͲɺͦΕͰ΋$24͸࣮ݱ͍ͨ͠Μͩʯ
 !BTVFOBNJ https://qiita.com/a-suenami/items/8897e2e36fe2836c6949

Slide 37

Slide 37 text

CQS Pattern ͱ ActiveRecord • ࢲݟ: CQS Pattern ͱ Rails Active Record ͱͷ૬ੑ͕͋·Γྑ͘ͳ͍ • Active Record ͸جຊతʹ1ςʔϒϧɾ1Ϟσϧ͕લఏ • CQS ύλʔϯͰ͸ɺCommand / Query ͦΕͧΕͰผͷϞσϧΛ࢖͏ • Rails ActiveRecord ΛࣗྗͰ֦ு͢Δ͜ͱ͸Ͱ͖Δ͕ɺ
 Rails Way ͔Β཭ΕΔͷͰ Rails ͷڧΈΛ׆͔ͤͳͦ͏ • => Rails ͷબ୒Λආ͚ΔཁҼʹͳͬͨ

Slide 38

Slide 38 text

GraphQL + Prisma + CQS Pattern • Prisma ͸ೖग़ྗʹ Plain Object Λ࢖͏ͷͰɺ
 CQS Pattern Λࣗવʹద༻͢Δ͜ͱ͕Ͱ͖ͨ

Slide 39

Slide 39 text

GraphQL + Prisma + CQS Pattern • චऀ @qsona ͸ GraphQL + Prisma + CQS Pattern ͷ
 ૊Έ߹Θͤʹେ͖ͳظ଴Λ๊͍͍ͯΔ͕ɺ
 ࣗ਎ͷྗྔෆ଍ʹΑΓɺຊൃදͰ͸े෼આ໌Ͱ͖͍ͯͳ͍ • ୅ΘΓʹ (?) ࢀߟࢿྉͱͯ͠
 ҒେͳهࣄΛ঺հ͠·͢ Smart UI ύλʔϯ͕࠶ධՁ͞ΕΔੈք (@onk) https://onk.hatenablog.jp/entry/2020/11/11/024531

Slide 40

Slide 40 text

͜Ε·Ͱͷ։ൃ;Γ͔͑Γ • ٕज़બఆ࣌ͷ΋͘ΖΈ௨Γɺ
 ͔ͳΓߴ͍։ൃੜ࢈ੑΛಘΔ͜ͱ͕Ͱ͖͍ͯΔ • TypeScript ͷ੩తܕ෇͚͸͔ͳΓ৺ڧ͍ɻ
 ͦΕ͕΄΅ແྉͰಘΒΕ͍ͯΔ • CQS ύλʔϯΛ࣠ʹͯ͠։ൃͷϨʔϧΛෑ͍ͨ͜ͱͰɺ
 ίʔυΛॻ͘৔ॴʹ໎͍͕ੜ͡ʹ͍͘

Slide 41

Slide 41 text

͜Ε·Ͱͷ։ൃ;Γ͔͑Γ • ͨͩ͠ɺ͜Ε·Ͱͷ։ൃظؒ͸໿1.5೥ɺ
 ͜ͷαʔϏεͷ։ൃʹ͋ͯΒΕͨਓ਺͸ฏۉ1ਓఔ౓ɻ
 ਖ਼ࣜϦϦʔε΋·ͩ • => ։ൃॳظͷஈ֊ͱݴ͑Δ • ͜ͷΞʔΩςΫνϟ͸ɺ։ൃதظҎ߱๚ΕΔ
 υϝΠϯϩδοΫͷෳࡶԽΛड͚ೖΕɺ։ൃੜ࢈ੑΛอͪଓ͚Δ͜ͱ͕
 Ͱ͖Δͷ͔?

Slide 42

Slide 42 text

ୈೋ෦: Active Record ͱରൺͯ͠ߟ͑Δ Prisma ͱ։ൃதظҎ߱ͷ։ൃੜ࢈ੑ

Slide 43

Slide 43 text

Active Record ͷ։ൃੜ࢈ੑ • ࠶ͼɺઌߦൃදΛҰ෦঺հ͠ͳ͕ΒਐΊ·͢ • Rails Active Record ͓Αͼ
 Prisma ͕ಋ͘ΞʔΩςΫνϟͱ
 ͦͷ։ൃੜ࢈ੑʹ͍ͭͯ

Slide 44

Slide 44 text

Active Record ͷ։ൃੜ࢈ੑ • ઌߦൃදͰ͸ɺ
 Ruby on Rails ͷ Active Record Λར༻ͨ͠։ൃͷੜ࢈ੑΛ
 ։ൃॳظ / ։ൃதظҎ߱ ͦΕͧΕͰߴ͘ධՁ͍ͯ͠Δ ᷿ͰΑ͘ ޠΒΕΔͷ͸ ͬͪ͜ ઌߦൃදͰத৺తʹޠΒΕ͍ͯΔͷ͸ ͬͪ͜ɻ ࣍ϖʔδ͔Β

Slide 45

Slide 45 text

Active Record ͷ։ൃੜ࢈ੑ • Rails Active Record ͸ɺ(໊લͷ௨Γ)
 ΞΫςΟϒϨίʔυύλʔϯΛ࣮૷͍ͯ͠Δ • ͜ΕʹΑΓɺυϝΠϯϩδοΫΛ
 υϝΠϯϞσϧͷܗͰهड़͢Δ͜ͱ͕Ͱ͖Δ • υϝΠϯϩδοΫ͕ద੾ʹ෼཭͞Εɺ
 ։ൃதظҎ߱ͷߴ͍։ൃੜ࢈ੑΛҡ࣋͠΍͍͢

Slide 46

Slide 46 text

Active Record ͷ։ൃੜ࢈ੑ • ΋ͪΖΜ Rails Active Record ͸ສೳͰ͸ͳ͍ɻ
 Ή͠ΖҰൠతʹ͸ɺRails ΞϓϦέʔγϣϯ͸
 ։ൃதظҎ߱Ͱ։ൃੜ࢈ੑ͕Լ͕Δͱ͍͏ݟํ ΋ڧ͍ • ͜ͷ՝୊ʹରͯ͠ Rails ίϛϡχςΟ͸༷ʑͳղ ܾࡦΛఏ͍ࣔͯ͠Δ • ͦͷܾఆ൛ͷҰͭͱݴ͑Δͷ͕ @_yasaichi ͞Μ ʹΑΔʮRuby on Rails ͷਖ਼ମͱ޲͖߹͍ํʯ https://speakerdeck.com/yasaichi/what-is-ruby-on-rails-and-how-to-deal-with-it

Slide 47

Slide 47 text

Prisma ͷ։ൃੜ࢈ੑ • Prisma ͸ Rails Active Record ͱ͸ҟͳΓɺ
 υϝΠϯϩδοΫΛهड़͢Δํ๏Λఏڙ͍ͯ͠ͳ͍ • ͦͷͨΊɺυϝΠϯϞσϧͰ͸ͳ͘
 τϥϯβΫγϣϯεΫϦϓτ (खଓ͖ܕతίʔυ) ͕ॻ͔Ε͕ͪ • => ։ൃதظҎ߱ɺ։ൃੜ࢈ੑͷ௿ԼΛট͘Մೳੑ͋Γ

Slide 48

Slide 48 text

https://speakerdeck.com/yasaichi/architecture-decision-for-the-next-10-years-at-pixta?slide=53

Slide 49

Slide 49 text

ࢀߟ: Prisma ଆͰͷݕ౼ঢ়گ https://github.com/prisma/prisma/issues/7161

Slide 50

Slide 50 text

@qsona ͷݱ৔Ͱ͸໰୊ʹͳ͍ͬͯΔ? • Q. content αʔϏεͷ։ൃ (Prisma Λ࠾༻) ʹ͓͍ͯɺ
 τϥϯβΫγϣϯεΫϦϓτͷංେԽʹΑΔ
 ։ൃੜ࢈ੑͷ௿Լ͸ى͖͍ͯΔ͔?

Slide 51

Slide 51 text

@qsona ͷݱ৔Ͱ͸໰୊ʹͳ͍ͬͯΔ? • Q. content αʔϏεͷ։ൃ (Prisma Λ࠾༻) ʹ͓͍ͯɺ
 τϥϯβΫγϣϯεΫϦϓτͷංେԽʹΑΔ
 ։ൃੜ࢈ੑͷ௿Լ͸ى͖͍ͯΔ͔? • A. ى͖͍ͯΔ

Slide 52

Slide 52 text

ίϯςϯπΠϯϙʔτͷίʔυ֓ཁ • େ͖͘ҎԼͷ3ͭͷؔ਺ʹ෼͔Εɺॱʹ࣮ߦ͞ΕΔ • 1. processContents ...
 CSV, YAML Ͱߏ੒͞Εͨ
 ίϯςϯπσʔλΛશͯparse͠ɺ
 1ຕͷ JS Object ʹม׵͢Δ • 2. deleteAllContents ... DB ্ͷίϯςϯπΛશ࡟আ • 3. insertContents ... 1 Ͱ࡞ͬͨσʔλΛ Prisma ܦ༝Ͱ DB ʹ౤ೖ͢Δ ໿ 700 ߦ, ࠷ਂ 4 ॏϧʔϓ͕ग़ݱ͢Δ τϥϯβΫγϣϯεΫϦϓτɻ ίʔυͷೝ஌ෛՙ͕ߴ͍

Slide 53

Slide 53 text

τϥϯβΫγϣϯεΫϦϓτͷ՝୊ • Ͳ͜ͰԿͷॲཧΛ΍͍ͬͯΔ͔͕ύοͱΘ͔Βͳ͍ • ϧʔϓ͕ଟॏʹͳ͍ͬͯͯɺม਺ͷείʔϓ͕෼͔Γʹ͍͘ • => ίʔυͷೝ஌ෛՙ͕ߴ͘ɺॳݟࡴ͠

Slide 54

Slide 54 text

ฐνʔϜ಺ͷ Repository ʹͯ 2021-01 ʹ࡞ΒΕͨ issue ͕ͩ 2021-11 ͷஈ֊Ͱ·ͩ Open. Read (GraphQL API) ͷςετ͕هड़ ͞ΕΔͳͲɺҰ෦͸վળࡁΈɻ Write ଆ͸ґવτϥϯβΫγϣϯεΫ Ϧϓτͷ··Ͱɺ՝୊͸ೝ͍ࣝͯ͠Δ

Slide 55

Slide 55 text

Q. ͳͥ͜ͷ໰୊Λղফ͍ͯ͠ͳ͍ͷ͔? • A. ༏ઌ౓͕ͦ͜·Ͱߴ͘ͳ͍͔Β • ཧ༝ (1) ؆୯ͳमਖ਼ͳΒɺ·ͩͦ͜·Ͱ೉͘͠ͳ͍ • TypeScript ͷ੩తܕ෇͚͕Α͘ޮ͍͍ͯΔ • த਎ΛΑ͘஌Δ։ൃऀ͕ɺνʔϜ಺ʹෳ਺ਓ͍Δ

Slide 56

Slide 56 text

Q. ͳͥ͜ͷ໰୊Λղফ͍ͯ͠ͳ͍ͷ͔? • A. ༏ઌ౓͕ͦ͜·Ͱߴ͘ͳ͍͔Β • ཧ༝ (2) ͜ͷίʔυΛେ͖͘मਖ਼ɾ֦ு͢Δ༧ఆ͕ͳ͍ • ͋Δେ͖ͳػೳ௥ՃͷλΠϛϯάͰಉ࣌ʹϦϑΝΫλϦϯά ͢Δɺͱ߹ҙ͍͕ͯͨ͠ɺ
 ͦͷػೳ௥Ճ͸Ϗδωεతཧ༝Ͱ༏ઌ౓͕Լ͕͍ͬͯΔ

Slide 57

Slide 57 text

Q. ͳͥ͜ͷ໰୊Λղফ͍ͯ͠ͳ͍ͷ͔? • ͭ·Γɺద੾ʹʮٕज़తෛ࠴ͷ؅ཧʯ͕ग़དྷ͍ͯΔྑ͍࿩!! ...? • ͕ɺ͍ͣΕղܾ͠ͳ͚Ε͹͍͚ͳ͍ͷ͸มΘΒͳ͍

Slide 58

Slide 58 text

Ұ୴·ͱΊͱ࿦఺੔ཧ ߴ͍։ൃੜ࢈ੑΛ ͲͷΑ͏ʹಘΔ ։ൃॳظ ։ൃதظҎ߱ Ұൠ࿦ ίʔυྔɾ࡞ۀྔ͕গͳ͘ ໎Θͣॻ͚Δ͜ͱ ن໛͕େ͖͍ίʔυΛ ద੾ʹ෼ׂɾ੔ཧͰ͖Δ͜ͱ 3BJMT "DUJWF3FDPSE 3BJMT8BZ ઃఆΑΓن໿ "DUJWF3FDPSEʹΑΓ ࣗಈͰϞσϧΛಘΒΕΔ ߴػೳͳ"DUJWF3FDPSEʹΑΓ υϝΠϯϩδοΫΛू໿Ͱ͖Δ ίʔυ੔ཧख๏ͷ஌ݟ͕͋Δ 1SJTNB 1SJTNB4DIFNB͔Β 5ZQF4DSJQUͷ੩తܕ෇͚΍ .JHSBUJPOΛࣗಈͰಘΒΕΔ େ͖ͳखؒͳ͘ɺυϝΠϯϩδοΫΛ ੔ཧ͢Δख๏͕ඞཁ ࣍ϖʔδ͔Βͷ࿦఺

Slide 59

Slide 59 text

υϝΠϯϞσϧͷදݱʹ class ͸ඞཁ͔? • Prisma ͸ɺؔ਺ͷೖग़ྗʹ Plain Object Λར༻͢Δ • ઌߦൃදΑΓҾ༻:
 "υϝΠϯϨΠϠʔҎԼΛυϝΠϯϞσϧͱ Prisma Ͱߏங͠Α͏ͱ͢Δͱɺ
 Prisma ͷฦ͢ POJO ͷσʔλΛυϝΠϯϞσϧʹ٧Ί௚͢ඞཁ͕͋Δ" • ͜ͷʮυϝΠϯϞσϧʯ͸ class ϕʔεͷ࣮૷͕૝ఆ͞Ε͍ͯΔͱࢥΘΕΔ • ٙ໰: υϝΠϯϞσϧͷදݱ͸ class Ͱͳ͍ͱग़དྷͳ͍ͩΖ͏͔? • (ࢲݸਓͷײ֮Ͱ͸ JS/TS ։ൃऀ͸ class Λ͋·Γଟ͘࢖Θͳ͍܏޲)

Slide 60

Slide 60 text

https://twitter.com/_yasaichi/status/1459394817125875717 https://keens.github.io/blog/2021/01/04/future_of_proguramming_languages/

Slide 61

Slide 61 text

https://twitter.com/a_suenami/status/1453293119760486408

Slide 62

Slide 62 text

class ͕ఏڙ͢Δศར͞ • σʔλʹؔ͢Δϝιου܈͕ू·Γɺڽू౓͕ߴ͍ঢ়ଶ • obj.method() ͷه๏ (IDE Ͱͷิ׬͕ޮ͘) • ΦϒδΣΫτͷ಺෦ঢ়ଶͷߋ৽ • ଟଶੑ (Polymorphism)

Slide 63

Slide 63 text

class Λ࢖ͬͨυϝΠϯϞσϧ Prisma ͷσʔλܕͱಉ͡ܕએݴͱ ͔ͦ͜Β٧Ίସ͑ΔͨΊͷίʔυɻ ຖ౓खಈͰ͜ͷΫϥεΛॻ͍ͨΓɺ ͜ͷΫϥεʹ٧Ίସ͑ͨΓ͢ΔίʔυΛॻ ͘ͷ͸ͱͯ΋໘౗͍͘͞ υϝΠϯϩδοΫͷίʔυɻ ҰͭͷϑΝΠϧʹ·ͱ·͍ͬͯͯ ڽू౓͕ߴ͍

Slide 64

Slide 64 text

ؔ਺܈ͷϑΝΠϧΛ࢖ͬͨυϝΠϯϞσϧ Immutable ύλʔϯΛར༻ Pick (Utility Types) Λར༻ɻ ςʔϒϧͷશΧϥϜΛऔಘ͠ͳͯ͘΋͢Ή఺Ͱ class ϕʔεͷυϝΠϯϞσϧΑΓ༗ར Ҿ਺ / ໭Γ஋ ʹ User ΛऔΔؔ਺܈Λ 1ຕͷϑΝΠϧʹ·ͱΊ͍ͯΔ

Slide 65

Slide 65 text

class ͕ఏڙ͢Δศར͞ • σʔλʹؔ͢Δϝιου܈͕ू·Γɺڽू౓͕ߴ͍ঢ়ଶ
 => ͦͷσʔλʹؔ࿈͢Δؔ਺Λ1ϑΝΠϧʹ·ͱΊΔ͜ͱͰ୅ସՄೳ • obj.method() ͷه๏ => method(obj) Ͱ(ҰԠ)୅ସՄೳ • ΦϒδΣΫτͷ಺෦ঢ়ଶͷߋ৽
 => newObj = method(obj) (Immutable ύλʔϯ) Ͱ(ҰԠ)୅ସՄೳ • ଟଶੑ (Polymorphism) => ???

Slide 66

Slide 66 text

ଟଶੑ (Polymorphism) • ࢲݟ: ଟଶੑ͕΄͍͠ͱ͖͚ͩ͸ class Λ࢖͏ͷ͕ྑ͍ͱࢥ͏

Slide 67

Slide 67 text

ଟଶੑ (Polymorphism) • Rails Active Record ʹ͓͍ͯ͸ɺ
 STI (Single Table Inheritance) ͱ Polymorphic Association ͷ2ͭͰ Polymorphism ͕αϙʔτ͞Ε͍ͯΔ • ͕ɺ͜ΕΒ͸ςʔϒϧઃܭʹ(͠͹͠͹ѱ͍)ӨڹΛٴ΅͢ͷͰɺ
 த௕ظͷ։ൃੜ࢈ੑΛߴΊΔҙຯͰ͸ਪ঑͞Εͳ͍ • ಛʹ Polymorphic Association ͸
 SQL Ξϯνύλʔϯʹ΋ܝࡌ͞Ε͍ͯΔ • ͕ͨͬͯ͠ɺPolymorphism ͕ཉ͍͠ͱ͖͸ (Rails Ͱ͋ͬͯ΋)
 Active Record Ͱ͸ͳ͍ಠࣗͷΫϥεΛ࡞੒͢Δ͜ͱ͕ଟ͍ • ͭ·ΓɺActive Record ͱͷൺֱͰ͍͑͹ɺͲͪΒ͕༗རͱ͍͏͜ͱ͸ͳ͍

Slide 68

Slide 68 text

@qsona ͷݱ৔ͷίʔυͷվળख๏ • ͱΓ͋͑ͣ Composed Method ύλʔϯͰ
 ڊେͳτϥϯβΫγϣϯεΫϦϓτΛখ෼͚ʹ͍ͯ͘͠ • গͣͭ͠༗༻ͳυϝΠϯϞσϧΛݟग़͍ͯ͘͠ • ࣮ࡍ೉͍͠ (ςʔϒϧ୯Ґͱ͸ݶΒͳ͍) • Polymorphism ͕ඞཁͳΒΫϥεΛ࢖͏! • (Ұ෦Λ࣍ϖʔδ͔Β঺հ)

Slide 69

Slide 69 text

processContentData ͷதͰɺ ໰୊ (Question) Λ processing ͢Δ෦෼ɻ ॲཧͷதͰ΋ಛʹෳࡶͳͷͰɺ parseQuestion ؔ਺ͱͯ͠෼཭͞Ε͍ͯΔ

Slide 70

Slide 70 text

parseQuestion ͷதͰ͸ɺ
 ΫϥεϕʔεͷϞσϦϯάΛߦͳ͍ͬͯΔɻ Question ͷछྨ͝ͱʹॲཧΛม͑Δ 
 Polymorphism Λར༻͍ͨͨ͠Ίɻ ೖྗଆͷσʔλߏ଄ͰΫϥεΛ࡞͍ͬͯΔͷͰɺ Active Record ύλʔϯ͔Β͸͜ͷઃܭ͸ಋग़͞ Εͳ͍

Slide 71

Slide 71 text

(·ͱΊ) ఏҊ: ؔ਺Λ࢖͏ɺclass Λͨ·ʹ࢖͏ • ؔ਺ͱɺؔ਺Λ·ͱΊͨϑΝΠϧΛ࡞Δ • ૯߹తʹ TypeScript ͱͷ૬ੑ͕ྑ͍ • ඞཁʹԠͯ͡ class Λ࡞Δ • యܕతʹ͸ Polymorphism ͕΄͍͠ͱ͖ Structural Typing Ͱ͋Δ͜ͱ Plain Object ͕ୈ1ڃͰ͋Δ͜ͱ

Slide 72

Slide 72 text

ຊൃදͷ·ͱΊ

Slide 73

Slide 73 text

ຊൃදʹ͓͚Δओு ҎԼͷٕज़ɾΞʔΩςΫνϟͷ૊Έ߹ΘͤʹΑΓ
 ։ൃॳظ / ։ൃதظҎ߱ ྆ํͰͷߴ͍։ൃੜ࢈ੑΛ୲อͰ͖ΔͷͰ͸ͳ͍͔? • ٕज़: GraphQL + Prisma (on Node.js, TypeScript) • ΞʔΩςΫνϟ: • CQS (Command-Query Separation) Pattern ͷ࠾༻Λݕ౼͢Δ • σʔλʹରԠ͢Δؔ਺܈ͱͯ͠ɺυϝΠϯϩδοΫΛ·ͱΊΔ • Polymorphism ͕ඞཁͳہ໘Ͱ͸ɺΫϥεΛར༻͢Δ

Slide 74

Slide 74 text

·ͱΊ: ຊൃදʹ͓͚Δओு • ͱ͸͍͑ɺߟ࡯͕·ͩෆे෼ͳ఺͸ଟ͍ • Έͳ͞Μ΋ʮຊ౰͔...?ʯͱࢥͬͨՕॴ͕
 ͖ͬͱ͍ͭ͘΋͋ͬͨͱࢥ͍·͢ • Prisma Λ࢖ͬͨத௕ظͷ։ൃࣄྫ͕૿͍͑ͯͬͯ
 ΞʔΩςΫνϟతͳ஌ݟΛڞ༗͠߹͑Δͱྑ͍ͱࢥ͏ • ΈΜͳ Prisma ࢖͓͏ͥ!!

Slide 75

Slide 75 text

৮ΕΒΕͳ͔ͬͨ࿩ • Prisma ͷʮ։ൃલظʯͷ։ൃੜ࢈ੑʹ͍ͭͯ...
 ࣗಈςετपล͕՝୊ • Prisma + Jest Λར༻͍ͯ͠Δ͕
 Rails + RSpec + Factory Bot ͷମݧʹ௥͍͚ͭͯͳ͍ https://zenn.dev/seya/articles/a0d2d2da20ddad https://twitter.com/sekikazu01/status/1463743468073746435

Slide 76

Slide 76 text

ँࣙ • ࠓճͷςʔϚʹ͍ͭͯߟ͑Δ͖͔͚ͬΛ࡞ͬͯͩͬͨ͘͞
 @_yasaichi ͞Μɺ͓Αͼ
 ೔ʑલ޲͖ʹίʔυվળͷٞ࿦ʹ෇͖߹ͬͯ͘ΕΔ
 ελσΟαϓϦͷ։ൃνʔϜͷ஥ؒʹײँ͍ͯ͠·͢