Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
フロントエンドとバックエンドのコミュニケーションをスムーズにするスキーマ駆動開発
Search
shimakaze-git
December 10, 2023
Programming
580
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
フロントエンドとバックエンドのコミュニケーションをスムーズにするスキーマ駆動開発
shimakaze-git
December 10, 2023
More Decks by shimakaze-git
See All by shimakaze-git
DjangoとFastAPIによる 実践認証技術
shimakaze_soft
0
1.1k
クリーンアーキテクチャのリポジトリパターン - Pythonでの実装
shimakaze_soft
1
6.3k
lt20221030.pdf
shimakaze_soft
0
190
Dependabotを使って 運用しているおはなし
shimakaze_soft
0
2.3k
DjangoCongressJP 2019/2020 & 今年にPyConJP初登壇できたはなし
shimakaze_soft
0
480
GAEによるPythonWEBアプリケーションの高速開発
shimakaze_soft
0
3.3k
FlaskとDjango以外のAPI開発の選択肢
shimakaze_soft
0
540
Python で Dependency Injection(DI) をやるには?
shimakaze_soft
1
3.8k
FalconAPI開発にいいよ!
shimakaze_soft
0
780
Other Decks in Programming
See All in Programming
為什麼你並不需要ViewModel / No, you don't need a ViewModel
lovee
1
490
Detecting Compromised CI with eBPF and Cilium Tetragon
lizrice
0
180
テーブルをDELETEした
yuzneri
0
140
自動化したのに回らないテスト運用の壁ーAI時代の品質責任と生産性
mfunaki
0
130
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
690
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
2
140
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
550
freeeにおけるEvalsの実践例の紹介
freee
PRO
0
110
全PRの83%がAIレビューだけでマージできるようになった開発組織はその後どうなったか
athug
1
1.6k
変わらないものが、変わるものを決める — 意図駆動開発 × イベントソーシング × イミュータブル | What Doesn't Change Decides What Can — IDD × Event Sourcing × Immutability
tomohisa
0
1.6k
170k Jobs a Day on GKE: Scaling Mercari's CI Platform - and What's Next for AI-Native Development
junyaokabe
0
110
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
800
Featured
See All Featured
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
520
Done Done
chrislema
186
16k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
890
Being A Developer After 40
akosma
91
590k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
66
57k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.4k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
470
Ruling the World: When Life Gets Gamed
codingconduct
0
290
Navigating Team Friction
lara
192
16k
Git: the NoSQL Database
bkeepers
PRO
432
67k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
380
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Transcript
フロントエンドとバックエンドのコミュニケーション をスムーズにするスキーマ駆動開発 エンジニアの輪@東京 2023/12/10 @shimakaze_soft shimakaze-soft 1
Who are you? おまえだれよ? Twitter: @shimakaze_soft GitHub: shimakaze-git toB系のプロダクトを開発するスタートアップでテックリード的な立場 趣味
サッカー観戦 歴史 shimakaze-soft 2
バックエンド & インフラエンジニア Django, FastAPI Pulumi OSS PyConJP 2023 当日Staff
DjangoCongress 2018~2021(Staff) shimakaze-soft 3
スキーマ駆動開発ってご存知ですか? shimakaze-soft 4
フロントエンドとバックエンドのエンジニア間でこん なやり取りありませんか? shimakaze-soft 5
フロントエンド バックエンドエンジニアが作成したAPIのレスポンスとリクエストボディのスキー マが複雑すぎてフロントの実装が大変 バックエンド フロントエンドが要求したスキーマがかなり複雑すぎて、バックエンドの処理が 大変すぎる。パフォーマンスの観点でもあまり良くないスキーマを要求される。 shimakaze-soft 6
例 : ユーザーの一覧のページを作成するとする shimakaze-soft 7
フロントエンドエンジニアが欲しいレスポンスのスキ ーマ { "users": [ { "id": 1, "name": "test1"
}, { "id": 2, "name": "test2" } ] } shimakaze-soft 8
バックエンドエンジニアが作成したレスポンスのスキ ーマ { "ids": [ 1, 2 ], "names": [
"test1", "test2" ] } shimakaze-soft 9
フロントエンド側の整形処理がかなり大変になる { "ids": [ 1, 2 ], "names": [ "test1",
"test2" ] } shimakaze-soft 10
shimakaze-soft 11
APIのスキーマをどうするかは揉めがち shimakaze-soft 12
API全般の開発の主導権はバックエンドエンジニアが持つ事が多い ビジネスロジックをどちらで持つかにも関わる パフォーマンスの観点で最適なスキーマもある バックエンド主導のスキーマに決まりやすい shimakaze-soft 13
フロントエンドとバックエンドのスキーマの違いがも たらす課題 バックエンドがAPIが完成するまでAPIを叩くところの実装を待たなければいけな い こういうレスポンスが返ってくる。こういうリクエストボディを投げるという想定 で実装しなければいけない 実際に作られたAPIのスキーマが事前に話し合われたものと違った shimakaze-soft 14
どう解決する? shimakaze-soft 15
API設計書をExcelで作る? API設計書と実際の実装が違っていたら?それをどうやって確認す る? shimakaze-soft 16
OpenAPIによるスキーマ駆動開発 shimakaze-soft 17
OpenAPIとは shimakaze-soft 18
OpenAPIとは 旧称はSwaggerと呼ばれていた。 APIの設計、開発、および利用に関する標準化された仕様の一つ。 この仕様は、RESTfulなAPIの記述を可能にし、APIのエンドポイント、リクエスト・レ スポンスの形式、認証方法などを文書化するための仕様。 JSONやYAML形式でAPIの設計情報を表現し、開発者やクライアントが理解しやすい 形でAPIを提供することができます。 OpenAPIの仕様に基づいてドキュメントを生成す るツールも提供されている。 shimakaze-soft
19
/api/users というエンドポイントを想定 openapi: 3.0.0 info: title: Sample API Response version:
1.0.0 paths: /users: get: summary: Get Users responses: '200': description: Successful response content: application/json: schema: type: object properties: users: type: array items: type: object properties: id: type: integer example: 1 name: type: string example: "test" shimakaze-soft 20
モックのAPIを構築できる OpenAPIのファイルを元にモックサーバーを構築できる version: "3" services: mock: image: stoplight/prism:3 container_name: auth_api_mock
platform: linux/amd64 tty: true ports: - 7501:4010 volumes: - ./openapi/openapi.yml:/openapi.yml:cached command: | mock -h 0.0.0.0 /openapi.yml shimakaze-soft 21
実際にAPIにリクエストを行う $ curl 'http://localhost:7501/users' エンドポイントで以下が返ってくる { users: [ {"id": 1,
"name": "test"}, {"id": 2, "name": "test"}, ] } shimakaze-soft 22
フロントエンドエンジニアもAPI設計をする shimakaze-soft 23
具体的な開発プロセス YMLでOpenAPI形式のAPIのスキーマを定義する そのファイルをGitHubのPRに出す shimakaze-soft 24
PRを出す際のルール フロントエンドの人が作成した場合はバックエンドの人がReview バックエンドの人が作成した場合はフロントエンドの人がReview 必ずバックエンドとフロントエンドの両者がPRを見る PRがマージされた = そのスキーマにお互いが合意した shimakaze-soft 25
急遽スキーマを変更したくなる場合がある バックエンドの実装を修正せずに、スキーマを修正してフロントエンドの実装が 可能 shimakaze-soft 26
ymlのスキーマとバックエンドの実装の差分をどうす るか 設計書として作成したymlのスキーマとバックエンドの実際の実装に差分が出る可能性 の問題どうする? shimakaze-soft 27
OpenAPIの差分比較ツールを使用 shimakaze-soft 28
oasdiff 二つのOpenAPI形式のファイルを比較して差分がないかをチェックしてくれるツール # Mac $ brew tap tufin/homebrew-tufin $ brew
install oasdiff $ go install github.com/tufin/oasdiff@latest shimakaze-soft 29
バックエンドの実装からスキーマを取り出す 今回はFastAPIを想定 shimakaze-soft 30
FastAPIにはOpenAPIの機能がデフォルトである shimakaze-soft 31
FastAPIにはOpenAPIの機能がデフォルトである openapi.json をダウンロード' shimakaze-soft 32
oasdiffを使用して差分を比較する $ oasdiff diff openapi.json openapi.yml --format text shimakaze-soft 33
実装とスキーマに差分が出る New Endpoints: 10 PUT /api/users GET /api/usrs Deleted Endpoints:
None Modified Endpoints: None GET /api/users/{usre_id} Responses changed Modified response: 200 Content changed Modified media type: application/json Schema changed Properties changed Modified property: general Required changed New required property: stores Deleted required property: store Properties changed New property: stores Deleted property: store shimakaze-soft 34
apidog apidogというツールもおすすめ https://apidog.com/jp/ shimakaze-soft 35
エンジニア募集 技術スタック バックエンド: Django, FastAPI フロントエンド: Next.js, Typescript インフラ: AWS,
Pulumi https://jobs.forkwell.com/recustomer/jobs/23462 shimakaze-soft 36