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
そろそろGraphQLの話をしよう
Search
tt
December 20, 2018
Programming
320
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
そろそろGraphQLの話をしよう
GraphQL と Apollo のお話
tt
December 20, 2018
More Decks by tt
See All by tt
戦略的なフロントエンドテストを実施するために
tatsushitoji
0
110
Our favorite Dependency updates has been deprived
tatsushitoji
2
540
try Undux but...
tatsushitoji
0
290
Other Decks in Programming
See All in Programming
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
720
ルールを書いて終わらせないハーネスエンジニアリング
yug1224
5
1.9k
今さら聞けない .NET CLI
htkym
0
200
人間の目はかわらない、だからJPEGは30年もつ
yuzneri
12
19k
S3 を使うアプリケーションをローカル完結で動かすことに全力を注いでみた / Running S3 Apps Offline
contour_gara
0
520
How I Won Prize Money at a Hackathon Using Codex and Symphony Alpha
yasei_no_otoko
0
110
AI時代に設計が 最大の生産性レバーになる 意図駆動開発とデータを消さない設計|Don't Delete Your Data or Your Intent — Design as the Deepest Lever in the AI Era
tomohisa
1
1k
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
3
800
KotlinConf Extended South Korea 2026 Keynote
l2hyunwoo
0
110
テーブルをDELETEした
yuzneri
0
140
AIが無かった頃の素敵な出会いの話
codmoninc
1
460
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
240
Featured
See All Featured
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
210
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
410
It's Worth the Effort
3n
188
29k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
280
The Invisible Side of Design
smashingmag
301
52k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6.1k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
450
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
370
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
800
Between Models and Reality
mayunak
4
390
Transcript
そろそろGraphQLの話をしよう 2018/12/20 Mix Leap Study #30 Webフロントエンド 平成最後の冬の陣 @tatsushitoji
Hello mix Leap ! • 田路 竜士 @tatsushitoji • 社内スタートアップおじさん
• Frontend Engineer • Functional Programming
GraphQLとは?
GraphQLとは? • FacebookによるOSSで、2015年に発表 • 柔軟にデータを取得できるように設計されたAPI用のクエリ言語 • データの形式のみの定義のため言語やデータI/Oの方法に依存しない
GraphQLとは? • FacebookによるOSSで、2015年に発表 • 柔軟にデータを取得できるように設計されたAPI用のクエリ言語 • データの形式のみの定義のため言語やデータI/Oの方法に依存しない 手っ取り早く試すならGitHub GraphQLAPI v4
Rest API + Redux user post DB API DB
GraphQL + Apollo DB API DB GraphQLサーバ
• リクエストエンドポイントの選択 • リクエストの組み立て(パラメータ、ヘッダなど) • 状態管理 フロントエンドで持っていた ロジック
• リクエストエンドポイントの選択 • リクエストの組み立て(パラメータ、ヘッダなど) • 状態管理 フロントエンドで持っていた ロジック GraphQL +
Apollo(Client) で吸収できる
Apollo
Apollo Engine • 実行状況を監視したり、エラートラッキング、キャッシュの利 用状況の監視などを行うツールとクラウドサービス Apollo Server • GraphQLサーバを構築できるライブラリ Apollo
Client • フロントエンドでGraphQLをシンプルに扱うためのライブラリ Apollo CLI • 開発時に $ apollo *** でいろいろできるツール
Apollo Client supports …
ベーシックワークフロー 1. スキーマ定義 2. モックサーバ実装 3. フロントエンド実装
スキーマ定義 type Todo { id: ID! title: String! completed: Boolean!
createdAt: String! } type Query { todo(id: ID!): Todo! todos: [Todo!] } input CreateTodoInput { id: ID! title: String! completed: Boolean! createdAt: String! }
モックサーバ実装 • resolvers がサーバとしての実装の本体 • GetTodo の場合、 type Todo を満たす値もしくは
Promise<Todo> を返却すれば良い • $ node server.js で対話型のプレイグラウンドでクエリ を実行できる
フロントエンド実装 • create react app —typescript で type safe な環境
• Apollo client • 一応 lint や code formatter も
所感
Apollo Server の mock 便利 const server = new ApolloServer({
typeDefs: fs .readFileSync(path.join(!__dirname, "!../schema.graphql")) .toString(), mocks: true, !// resolvers: resolvers, }); ↑スキーマ定義をしておけば、定義した型で適当な値を返してくれる
Function as child Components <Query query={GET_TODOS} fetchPolicy="cache-and-network"> {({ data, loading,
error }) !=> { if (error !|| loading) { return <p>{error ? `Error! ${error}` : 'loading!!...'}!</p>; } if (data) { return data.todos.map((todo: any, index: number) !=> ( <List>{todo.title}!</List> )); } }} !</Query> ↑を実現するためにReduxでのaction, reducer, connect相当を内部でやってくれている
Code generator !/* tslint:disable !*/ !// This file was automatically
generated and should not be edited. !// ==================================================== !// GraphQL query operation: GetTodos !// ==================================================== export interface GetTodos_todos { id: string; title: string; completed: boolean; createdAt: string; } ↑schemaとGraphQLサーバから型を生成してくれる
Function as child Components <Query<GetTodos, {}> query={GET_TODOS} fetchPolicy="cache-and- network"> {({
data, loading, error }) !=> { if (error !|| loading) { return <p>{error ? `Error! ${error}` : 'loading!!...'}!</p>; } if (data !&& data.todos) { return data.todos.map((todo: any, index: number) !=> ( <List key={index}>{todo.title}!</List> )); } }} !</Query> ↑生成された型をGenericsで渡すだけでchildのfunctionの引数まで型推論が効く
年末年始はGraphQL どうですか?