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
Data-fetching in React applications with Relay ...
Search
Marc-Andre Giroux
August 10, 2016
Programming
700
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Data-fetching in React applications with Relay & GraphQL
Marc-Andre Giroux
August 10, 2016
More Decks by Marc-Andre Giroux
See All by Marc-Andre Giroux
It Depends - Examining GraphQL Myths & Assumptions
xuorig
0
140
So you Want to Distribute your GraphQL Schema?
xuorig
4
880
So you Want to Distribute your GraphQL Schema?
xuorig
0
690
GraphQL Schema Design @ Scale
xuorig
5
2.2k
Continuous Evolution of GraphQL Schemas @ GitHub
xuorig
3
2.2k
GraphQL à Shopify
xuorig
0
290
Exploring GraphQL
xuorig
0
310
GraphQL @ Shopify
xuorig
6
1.8k
GraphQL on Rails
xuorig
2
400
Other Decks in Programming
See All in Programming
【やさしく解説 設計編 #0】DDDのコード、読めるのに分からない人へ
panda728
PRO
2
270
AI 輔助遺留系統現代化的經驗分享
jame2408
1
1.2k
Claude Opus 4.6以後の受託開発エンジニアの変化(Claude Code開発ノウハウ大公開スペシャルbyクラスメソッド)
iidatakuma
1
820
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
190
SREは、MCPとSRE Agentをこう使え!
kazumax55
0
150
任せる範囲はこう広がった / How the Scope of AI Delegation Has Expanded
nrslib
1
270
フィードバックで育てるAI開発
kotaminato
1
120
なぜ関数型プログラミングで「型」と「証明」が語られるのか #fp_matsuri
kajitack
3
990
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
4
1.1k
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
0
120
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
170
これからAgentCoreを触る方へトレンドはGatewayです
har1101
6
500
Featured
See All Featured
The Curse of the Amulet
leimatthew05
2
13k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.2k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.2k
Why Our Code Smells
bkeepers
PRO
340
58k
Building Applications with DynamoDB
mza
96
7.1k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Skip the Path - Find Your Career Trail
mkilby
1
170
Unsuck your backbone
ammeep
672
58k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
970
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
360
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.9k
WCS-LA-2024
lcolladotor
0
730
Transcript
Data-fetching in React applications with Relay & GraphQL 1
A beautiful component 2
A beautiful simple component 3
React { <Shop> <Product> <ProductImage> 4
{ Shop component 5
Product component { 6
ProductImage component { 7
What if we needed to add a description? { 8
What kind of data is needed ? Shop Product Image
9
Let’s fetch that using reusable endpoints ? /shop/1 /products/1 /products/2
image/sabc images/def 10
Let’s fetch that using ad-hoc endpoints ? /shop_cart_page /shop_cart_page?include=images /shop_cart_page_with_images?num_images=2
/shop_cart_page_single_prices_and_descriptions_version1.2 /shop_cart_page_with_prices_and_descriptions_but_no_images 11 op_cart_page_with_prices_and_descriptions_and_images_but_only_one_300
12
GraphQL 13
query { myShop { name } } Simple query 14
Response { “myShop” { “name”: “My Kool Shop” } }
15
Query { myShop { name location { city address }
products(orderby: PRICE) { name price } } } 16
Response { “myShop” { “name”: “My Kool Shop”, “location”: {
“city”: “Montreal”, “address”: “5333 Avenue Casgrain”, }, “products”: [{ “name”: “A Chair”, “price”: 10000 }, { “name”: “A Table”, “price”: 20000 }] } } 17
Type System { myShop { name location { city address
} products(orderby: PRICE) { name price } } } type QueryRoot { myShop: Shop } 18
Type System enum ProductOrderEnum { PRICE, POPULARITY, ALPHABETICAL } type
Shop { name: String location: Address products(orderby: OrderEnum): [Product] } { { myShop { name location { city address } products(orderby: POPULARITY) { name price } } } 19
Type System type Address { city: String address: String }
{ { myShop { name location { city address } products(orderby: POPULARITY) { name price } } } 20
Type System type Product { name: String price: Int }
{ { myShop { name location { city address } products(orderby: POPULARITY) { name price } } } 21
Fragments { { myShop { name …locationFragment } } fragment
locationFragment on Shop { location { city address } } 22
Mutations { mutation { createProduct(name: “Kanye West Shoes”) { name
} } 23
How Relay can help { Relay React GraphQL 24
Query colocation { query Fragment Fragment Fragment Fragment 25
Query colocation { Shop Product Product Image 26
Relay Containers / Higher Order Components { Relay.createContainer(ShopComponent, { fragments:
{ … } }); Container Shop 27
Shop Component (Relay) { 28
Shop component (Relay) { 29
Product component (Relay) { 30
ProductImage component (Relay) { 31
How the *&^#@$ does that even work? { 32
Step 1: Making sense out of fragments { fragments: {
image: () => Relay.QL` fragment on Image { url } `, }, 33
Relay.QL (pre-transform) { fragments: { image: () => Relay.QL` fragment
on Image { url } `, }, 34
Relay.QL (transformed) { fragments: { image: function image() { return
function () { return { children: [{ fieldName: 'url', kind: 'Field', metadata: {}, type: 'String' }, { fieldName: 'id', kind: 'Field', metadata: { isGenerated: true, isRequisite: true }, type: 'ID' }], id: _reactRelay2.default.QL.__id(), kind: 'Fragment', metadata: {}, name: 'ProductImage_ImageRelayQL', type: 'Image' }; }(); } 35
Step 2: Query diff ( The Store ) { RelayRecord
Store • Client Side Cache • Normalized / Flattened Store 36
Step 2: Query diff { fragments: { product: () =>
Relay.QL` fragment on Product { name image { url } } `, }, 37
Step 2: Query diff { fragments: { product: () =>
Relay.QL` fragment on Product { name description image { url } } `, }, 38
Step 2: Query diff { fragments: { product: () =>
Relay.QL` fragment on Product { description } `, }, 39
Step 2: Query diff { fragments: { product: () =>
Relay.QL` fragment on Product { description } `, }, 40
Step 3: Split Deferred queries { fragments: { product: ()
=> Relay.QL` fragment on Product { name description ${Images.getFragment('product').defer()}} } `, }, 41
Step 3: Split Deferred queries { 42
Step 3: Split Deferred queries { 43
Step 4: Subtract in-flight queries { 44
Step 4: Subtract in-flight queries { 45
{ myShop { name location { city address } products(orderby:
PRICE) { name price } } } Final step: Printing { 46
Back to the Store { 47 Relay Record Store Cached
Records Queued Records
Mutations { class AddProductMutation extends Relay.Mutation { getMutation() { return
Relay.QL`mutation { addProduct }`; } getVariables() { return { productName: this.props.product.name }; } getFatQuery() { … } getConfigs() { … } getOptimisticResponse() { … } } 48
And so much more { 49 Subscriptions @stream Garbage collection
Server side rendering Connections
For more info: { 50 https://facebook.github.io/relay https://github.com/facebook/graphql
Thank you! Try it out yourself :) @__xuorig__ http://mgiroux.me xuorig
51