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
Yosuke Kurami
May 27, 2016
76
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
2.1k
フロントエンドテストの育て方
quramy
12
3.8k
App Router 悲喜交々
quramy
8
740
上手に付き合うコンポーネントテスト
quramy
6
2.4k
Patched fetch did not work
quramy
6
790
GraphQL あるいは React における自律的なデータ取得について
quramy
18
5.9k
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
Unsuck your backbone
ammeep
672
58k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
3.8k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
190
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
350
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.4k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
370
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
460
Scaling GitHub
holman
464
140k
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!