TypeScriptとGraphQLで実現する 型安全なAPI実装 / TSKaigi 2024
by
Kazuhito Hokamura
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
2024/5/11 TSKaigi @hokaccha TypeScriptͱGraphQLͰ࣮ݱ͢Δ ܕ҆શͳAPI࣮
Slide 2
Slide 2 text
Ubie, Inc @hokaccha @hokaccha Kazuhito Hokamura
Slide 3
Slide 3 text
ࠓͷ༰ͪ͜ΒͰಡΊ·͢ Ubie ςοΫϒϩά
Slide 4
Slide 4 text
ͳͥAPIʹܕΛ͚͍ͭͨͷ͔
Slide 5
Slide 5 text
Α͋͘Δ API ϦΫΤετͷίʔυ
Slide 6
Slide 6 text
any ͔ΒͷܕΩϟετͱɺفΓ 🙇҆શفئ🙏
Slide 7
Slide 7 text
فΓ͕௨͡ͳ͍ͱͷإΑΓݟͨͭ
Slide 8
Slide 8 text
֎෦I/Oʹର͢Δܕ͚͕ෆेͩͱ ੩తܕ͚ͷԸܙΛे׆͔ͤͳ͍
Slide 9
Slide 9 text
API ʹܕΛ͚ͭΔ #ͱ 1. API ͷ༷ΛεΩʔϚͰఆٛ͢Δ 2. εΩʔϚ͔Β࣮ݴޠͷܕΛੜ͢Δ 3. ੜ͞ΕͨܕΛ࣮ͬͯ͢Δ • ΫϥΠΞϯτͷ࣮ • αʔόʔͷ࣮
Slide 10
Slide 10 text
API ʹܕΛ͚ͭΔٕज़ • OpenAPI • gRPC • GraphQL ࠓ͜Ε
Slide 11
Slide 11 text
GraphQLͷεΩʔϚఆٛ schema.graphq
Slide 12
Slide 12 text
εΩʔϚ͔ΒܕΛੜ schema.graphq generated.ts
Slide 13
Slide 13 text
ੜͨ͠ܕΛ࣮ͬͯ͢Δ client.ts server.ts ˞ίʔυΠϝʔδ
Slide 14
Slide 14 text
GraphQL Codegen
Slide 15
Slide 15 text
ΫϥΠΞϯταΠυ
Slide 16
Slide 16 text
GraphQL Codegen Client Preset
Slide 17
Slide 17 text
codegen.ts
Slide 18
Slide 18 text
🙅 εΩʔϚ͔Βੜͨ͠ܕΛ͏ 🙆 ΫΤϦ͔Βੜͨ͠ܕΛ͏
Slide 19
Slide 19 text
εΩʔϚ͔Βੜͨ͠ܕΛ͏ εΩʔϚ ੜ͞Εͨܕ ΫϥΠΞϯτ࣮
Slide 20
Slide 20 text
εΩʔϚ͔Βੜͨ͠ܕΛ͏ εΩʔϚ ੜ͞Εͨܕ ΫϥΠΞϯτ࣮ JNBHF6SM͕ܕʹ͋Δ͚ͲϨεϙϯεʹͳ͍
Slide 21
Slide 21 text
εΩʔϚ͔Βੜͨ͠ܕΛ͏ εΩʔϚ ੜ͞Εͨܕ ΫϥΠΞϯτ࣮ JNBHF6SM͕ܕʹ͋Δ͚ͲϨεϙϯεʹͳ͍
Slide 22
Slide 22 text
ΫΤϦ͔Βੜͨ͠ܕΛ͏
Slide 23
Slide 23 text
ΫΤϦจࣈྻ͔Β (SBQI2-$PEFHFO͕ܕΛੜ͢Δ
Slide 24
Slide 24 text
͜ͷΛ(SBQI2-ͷΫϥΠΞϯτʹ͢ͱ ΫΤϦͷ݁Ռʹਖ਼͘͠ܕ͕ͭ͘
Slide 25
Slide 25 text
ೖྗΫΤϦ͔Β࡞ΒΕͨ ܕͰܕݕ͕ࠪޮ͘
Slide 26
Slide 26 text
TypedDocumentNode • GraphQL Codegen͕ੜ͢ΔΫΤϦͷܕ͖AST • ରԠ͢ΔΫϥΠΞϯτʹ৯ΘͤΔͱܕ͕͘ • ϝδϟʔͲ͜ΖͷΫϥΠΞϯτରԠ͍ͯ͠Δ • Apollo Client • urql • graphql-request ͜Ε
Slide 27
Slide 27 text
Fragment Masking
Slide 28
Slide 28 text
ͷલʹFragment Colocation
Slide 29
Slide 29 text
No content
Slide 30
Slide 30 text
ը૾Λදࣔ͠ͳ͍Α͏ʹมߋ
Slide 31
Slide 31 text
ফ͠Εͯ ؾ͚ͳ͍
Slide 32
Slide 32 text
ଞͷΫΤϦͰࢀর͞ΕͯΔ͔
Slide 33
Slide 33 text
ͯ͢ͷࢠ͕ඞཁͱ͢ΔσʔλΛτοϓϨϕϧͷ ΫΤϦ͕͍ͬͯΔඞཁ͕͋Δͱ͍͏ઃܭͷ
Slide 34
Slide 34 text
No content
Slide 35
Slide 35 text
No content
Slide 36
Slide 36 text
͜ͷ'SBHNFOUఆ͔ٛΒܕΛੜ
Slide 37
Slide 37 text
ੜ͞Εͨܕ
Slide 38
Slide 38 text
Fragment Colocation • FragmentΛίϯϙʔωϯτͱಉ͡ॴʹஔ͘ • ίϯϙʔωϯτ͕ࣗؔ৺ͷ͋ΔϑΟʔϧυ͚ͩཧ͢ΕΑ͍ • ͷࢠͷFragment͚ͩΛ͍ͬͯΕΑ͍
Slide 39
Slide 39 text
Fragment Masking
Slide 40
Slide 40 text
No content
Slide 41
Slide 41 text
͜͜Ͱ͔͠OBNFΛࢦఆͯ͠ͳ͍
Slide 42
Slide 42 text
͜͜ͰࢀরͰ͖ͯ͠·͏
Slide 43
Slide 43 text
࣮͜ΕܕΤϥʔʹͳΔ
Slide 44
Slide 44 text
͜Μͳײ͡ʹܕΛมߋ
Slide 45
Slide 45 text
͜ͷؔΛ௨͞ͳ͍ͱ'SBHNFOUͷ σʔλΛࢀরͰ͖ͳ͍
Slide 46
Slide 46 text
Fragment Masking • FragmentΛఆٛͨ͠ίϯϙʔωϯτҎ֎͔ΒFragmentͷσʔλʹ ΞΫηεͰ͖ͳ͍Α͏ʹ͢Δ • ͜ΕʹΑͬͯFragment Colocation͕ڧ੍ͤ͞Δ • ܕ͕ෳࡶʹͳΔͱ͍͏σϝϦοτ͋Δ
Slide 47
Slide 47 text
αʔόʔαΠυ
Slide 48
Slide 48 text
GraphQL Codegen TypeScript Resolvers
Slide 49
Slide 49 text
GraphQL Codegen TypeScript Resolvers 4FSWFS1SFTFUบ͕ڧΊ
Slide 50
Slide 50 text
ϝδϟʔͳϥΠϒϥϦʹରԠ • graphql-js • ͜Ε͕શͯͷϕʔε • Apollo Server • GraphQL Yoga
Slide 51
Slide 51 text
NestJS͚ͩগ͠ಛघ Ubie ςοΫϒϩά
Slide 52
Slide 52 text
schema.graphql
Slide 53
Slide 53 text
resolvers.ts model.ts
Slide 54
Slide 54 text
resolvers.ts model.ts %#͔ΒҾ͍ͯNPEFMͷܕΛฦ͢
Slide 55
Slide 55 text
codegen.ts
Slide 56
Slide 56 text
No content
Slide 57
Slide 57 text
ܕ͕͍ͭͯΑ͔ͬͨʂʂ ͱ͍͔ͳ͍
Slide 58
Slide 58 text
No content
Slide 59
Slide 59 text
DPNQBOZΛؚ·ͳ͍
Slide 60
Slide 60 text
DPNQBOZͷऔಘ͕ແବ
Slide 61
Slide 61 text
No content
Slide 62
Slide 62 text
DPNQBOZ͕ΫΤϦʹؚ·ΕΔͱ͖͚࣮ͩߦ͞ΕΔ
Slide 63
Slide 63 text
No content
Slide 64
Slide 64 text
No content
Slide 65
Slide 65 text
codegen.ts 3FTPMWFSͰѻ͏ܕΛมߋͰ͖Δ
Slide 66
Slide 66 text
No content
Slide 67
Slide 67 text
·ͱΊ
Slide 68
Slide 68 text
·ͱΊ • APIͷ࣮ʹܕΛ͚ͭͯفΓ͔Β։์͞ΕΑ͏ʂ • GraphQL CodegenͰΫϥΠΞϯτɾαʔόʔͷ࣮ʹܕΛ͚ͭΒΕΔʂ • ಋೖίετͦΕͳΓʹ͔͔Δ͚ͲϝϦοτѹతʹେ͖͍ͧʂ
Slide 69
Slide 69 text
͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ