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
graph-api.pdf
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Yosuke Kurami
May 27, 2016
75
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
graph-api.pdf
Yosuke Kurami
May 27, 2016
More Decks by Yosuke Kurami
See All by Yosuke Kurami
TypeScript LSP の今までとこれから
quramy
1
2k
フロントエンドテストの育て方
quramy
12
3.8k
App Router 悲喜交々
quramy
8
730
上手に付き合うコンポーネントテスト
quramy
6
2.3k
Patched fetch did not work
quramy
6
780
GraphQL あるいは React における自律的なデータ取得について
quramy
18
5.8k
Next.js App Router
quramy
15
3.9k
Fragment Composition of GraphQL
quramy
17
4.8k
reg-viz VRT tools
quramy
4
1.7k
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Utilizing Notion as your number one productivity tool
mfonobong
4
320
The agentic SEO stack - context over prompts
schlessera
0
810
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
GitHub's CSS Performance
jonrohan
1033
470k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
770
The World Runs on Bad Software
bkeepers
PRO
72
12k
AI: The stuff that nobody shows you
jnunemaker
PRO
8
700
Tell your own story through comics
letsgokoyo
1
950
How to build a perfect <img>
jonoalderson
1
5.6k
Transcript
Introduction of graph API ~ GraphQL and Falcor ~ @Quramy
2016.05.27
About me Yosuke Kurami ǛQuarry TypeScript༻ͷVimϓϥΪϯ։ൃ͕झຯɻ http://vimawesome.com/plugin/tsuquyomi ຊۀWeb front-end Developer.
Angular (1 and 2)ͰSPAΛ։ൃ͍ͯ͠·͢
SPA
Single Page Application
Single Page Application Ƀ Ȑ {JSON} ɂ Ʌ HTTP
Today’s theme: Ƀ Ȑ {JSON} ɂ Ʌ HTTP
How to design API? endpointͷཻɺͲΕ͘Β͍͕దͰ͔͢? 1. ResourceʹશͯURLΛৼͬͯlinkͰଓʁ 2. Request͠қ͍Α͏ؔ࿈Resourcepopulateʁ
1. Thin endpoint Ƀ Ȑ { “id”: “123”, “title”: “graph
API”, “author”: “/api/v1/users/u0001” } ɂ Ʌ Get /api/v1/articles/123 Get /api/v1/users/u0001 { “id”: “u0001”, “name”: “quramy” }
1. Thin endpoint ֤ResourceURL LinkͰ݁߹͞Ε͍ͯΔɿ RESTful APIͷݪଇ௨Γ Endpointࣗମͷ࠶ར༻ੑߴ͍ RenderingʹෳRequest͕ඞཁ
2. Fat endpoint Ƀ Ȑ { “id”: “123”, “title”: “graph
API”, “author”: { “id”: “u0001”, “name”: “quramy” } } ɂ Ʌ Get /api/v1/articles/123
2. Fat endpoint Responseʹଟ͘ͷใΛωετͤ͞Δ: ը໘ͷ3FOEFSJOHָ ͦͯ͠ߴ . Endpointͷ࠶ར༻ੑ͍ ը໘ཁ݅ʹҾͬுΒΕͯɺ&OEQPJOUվम͕ඞཁͱͳΔ
View only knows what values to render Client͕Response༰ͷܾఆݖΛ࣋ͯΑ͍: • ϨεϙϯεʹͲͷؚ͕·Ε͍ͯΔ͖͔
• ඥ͍ͮͨؔ࿈Ϧιʔε͕ඞཁ͔Ͳ͏͔
Ideal endpoint Ƀ Ȑ { “id”: “123”, “title”: “graph api”,
“author”: { “id”: “u0001”, “name”: “quramy” } } ɂ Ʌ { “where”: “/article?id=123”, “properties”: [ “id”, “title”, “author”: { “properties”: [“id”, “name”] }] }
So, graph API
There are 2 major libraries GraphQL powered by Facebook http://graphql.org/
Falcor powered by Netflix http://netflix.github.io/falcor/
GraphQL
GraphQL: GraphQL is a query language import assert from "assert";
import { parse } from "graphql/language"; import { execute } from "graphql/execution"; import { schema } from "./schema"; execute(schema, parse(` { article(id :"123") { id, title, author { id, name } } } `)).then(response => { assert.deepEqual(response.data, { article: { id: "123", title: "graph api", author: { id: "u0001", name: "quramy" } } }); });
GrahpQLΛར༻͢ΔʹSchemaͷఆ͕ٛඞਢ ʮarticle(id: “123”)͕औಘՄೳʯͷใશͯSchemaʹఆ͓ٛͯ͠ ͘ඞཁ͕͋Δ GraphQL: GraphQL has solid Schema Ȑ
schema Inquire Implement
GraphQL: pros/cons • Pros: • JavaScriptҎ֎Ͱ৭ʑͳݴޠ(Java, golang, Ruby, etc…)Ͱར༻Մೳͳlibrary ͕͋Δɻ
• GraphiQlChrome Extensionɺ։ൃิॿπʔϧ๛ɻ • Cons: • शಘίετ͕ߴ͍. SchemaΛखΈͰΉͱࢮͶͦ͏. • Relay(React.js + GraphQLͷϑϨʔϜϫʔΫ)ߴػೳͰ͋ΔҰํɺGraphQL ࣗମͷRIͰ͋ΔGraphQL.jsը໘͔Β͏͜ͱΛߟ͑Δͱ૬γϣϘ͍ɻ ڧ੍తʹReact.jsʹϩοΫΠϯ͞Εͦ͏ɻ
GraphQL: Tools example e.g. Execution query with GraphiQL:
Falcor
Falcor: Access as the JSON way import { Model }
from "falcor"; import { dataSource } from "./dataSource"; import assert from "assert"; new Model({ source: dataSource, }).get( "article['123']['id', 'title']", "article['123'].author['id', 'name']" ).subscribe(response => { assert.deepEqual(response.json, { article: { "123": { id: "123", title: "graph api", author: { id: "u0001", name: "quramy" } } } }); });
Falcor “Virtual JSON Graph” ΛClient, ServerํͰڞ༗͢Δ ClientVirtual JSON Graphͷ෦ू߹ΛऔΓग़͢. Falcor:
Model as one JSON Object Ȑ JSON Graph
Falcor: pros/cons • Pros: • Έ͕୯७Ͱ͋Γɺशಘ͕༰қɻ • ७ਮͳData Fetching Library
Ͱ͋ΔͨΊɺClient-Side MVC͕ϩοΫΠϯ͞Ε ͳ͍ɻAngularͩΖ͏ͱVue.jsͩΖ͏ͱԿͰΈ߹ΘͤΒΕΔɻ • falcor.jsʹCaching, Batching, onChange, ͋Δͱخ͍͠ػೳҰ௨Γἧͬͯ ͍Δ • Cons: • Server-Side࣮جຊతʹNode.jsҰ(ҰԠC#࣮͋ΔΒ͍͚͠Ͳ…) • ࣮ߦ͠ͳ͍ͱJSON GraphͷܕใΛऔಘग़དྷͳ͍
Summary
GraphQL v.s. Falcor • GraphQLʹݫີͳSchemaఆ͕ٛଘࡏ͢ΔҰํ, Falcorʹ͓͚ ΔJSON Graphߏ҉(ͦͷ, ֶश࣮ίετΊ) •
Server-SideͱClient-Sideͷ࣮νʔϜ͕͞Ε͍ͯΔ߹ GraphQLબΜͩํ͕ྑ͍ͣɻ • ٯʹ, Front-End νʔϜ͕Endpointͷ࣮·Ͱग़དྷΔͷͰ͋Ε , FalcorͰඞཁॆͳέʔε͕ଟͦ͏ɻ
Graph API • طଘͷREST endpointΛϥοϓ͢Δ༻్ͷ߹ɺͦΕ΄ͲϝϦοτແ ͍ɻ ಛʹViewʹಛԽͨ͠EndpointઃܭΛ͍ͯ͠ΔέʔεͰҰGraphͷܗࣜ ʹਖ਼نԽ͢Δख͕͔͔ؒΔ͚͔ͩɻ • MicroServicesͷจ຺ʹ͓͍ͯ,
ෳͷDown Stream ServiceΛଋͶΔత ʹڧ͍(API Gateway, Backend-For-Fronend ) • ʮMonolithic ͳServiceΛෳׂ͢Δ࣌ʹFront͕ઌߦͯ͠ಋೖ͢Δʯͷ Α͏ͳಋೖݱ࣮త͔
Thank you!