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
310
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
530
try Undux but...
tatsushitoji
0
290
Other Decks in Programming
See All in Programming
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
120
DynamoDBには集計系のクエリがないけどなんとかしたい
musan
1
130
脅威をエンジニアリングの糧にして――現場編 / Turning Threats into Engineering Fuel — Field Edition
nrslib
0
270
「エンジニアインターン、どうやって取った?」準備のリアルを語るLT会 Progate BAR
akiomatic
0
130
A2UI という光を覗いてみる
satohjohn
1
130
New "Type" system on PicoRuby
pocke
1
830
TAKTでAI駆動開発の品質を設計する
j5ik2o
6
1.2k
Spring Security 実践 ─ GraphQL APIで実務に役立つ 認証・認可 を学ぶ
wagyu
0
220
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
110
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
560
These Five Tricks Can Make Your Apps Greener, Cheaper, & Nicer
hollycummins
0
280
Modding RubyKaigi for Myself
yui_knk
0
920
Featured
See All Featured
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.8k
WENDY [Excerpt]
tessaabrams
11
38k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.3k
How STYLIGHT went responsive
nonsquared
100
6.2k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
300
Producing Creativity
orderedlist
PRO
348
40k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
30 Presentation Tips
portentint
PRO
1
320
RailsConf 2023
tenderlove
30
1.5k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
580
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
540
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
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 どうですか?