SlideShare une entreprise Scribd logo
1  sur  40
GraphQL & Relay 入門
chentsulin
chentsulin
C. T. Lin
Active JavaScript Open Source Developer
System Engineer Lead@Yoctol
Repos
- electron-react-boilerplate
- awesome-graphql
- koa-graphql
- sweetalert-react
Translations
- redux
- graphql
- relay
PR Welcome !
進入正題
今天的範例放在
k o a - g r a p h q l - r e l a y - e x a m p l e
( h t t p s : / / g o o . g l / g t 3 i A h )
GraphQL
Why?
RESTful didn’t scale
Problems
- A lot of Endpoints
- Breakings cause client/server Mismatch
- Overfetching and Underfetching
- Nested Resources
- Lack of Type System
叫作 QL
最強大的當然是它的
Query
{
view er {
id
t ot alC ount
}
}
ResponseRequest
{
"dat a ": {
"view er": {
"id": " VXN lc jpt ZQ = = ",
"t ot alC ount ” : 2
}
}
}
順便介紹一下
重要的
GraphiQL
還有重要的
Fragment
{
v i e w e r {
i d
t o t a l C o u n t
. . . t o d o I n f o
}
}
f r a g m e n t t o d o I n f o o n U s e r
{
t o d o s {
e d g e s {
n o d e {
i d
}
}
}
}
ResponseRequest
{
" d a t a " : {
" v i e w e r " : {
" i d " : " V X N l c j p t Z Q = = " ,
" t o t a l C o u n t " : 2 ,
" t o d o s " : {
" e d g e s " : [
{
" n o d e " : {
" i d " : " V G 9 k b z o w "
}
} ,
{
" n o d e " : {
" i d " : " V G 9 k b z o x "
}
}
]
}
}
}
}
再來就是用來處理
side effects 的
Mutation
m u t a t i o n A d d To d o ( $ i n p u t :
A d d To d o I n p u t ! ) {
a d d To d o ( i n p u t : $ i n p u t ) {
t o d o E d g e {
n o d e {
i d
t e x t
c o m p l e t e
}
}
}
}
ResponseRequest
{
" d a t a " : {
" a d d To d o " : {
" t o d o E d g e " : {
" n o d e " : {
" i d " : " V G 9 k b z o z " ,
" t e x t " : " t e s t " ,
" c o m p l e t e " : f a l s e
}
}
}
}
}
{
" i n p u t " : {
" t e x t " : " t e s t " ,
" c l i e n t M u t a t i o n I d " : " 0 "
}
}
Type System
t y p e To d o i m p l e m e n t s N o d e {
# T h e I D o f a n o b j e c t
i d : I D !
t e x t : S t r i n g
c o m p l e t e : B o o l e a n
}
t y p e U s e r i m p l e m e n t s N o d e {
# T h e I D o f a n o b j e c t
i d : I D !
t o d o s ( a f t e r : S t r i n g , f i r s t : I n t , b e f o r e : S t r i n g , l a s t : I n t ) :
To d o C o n n e c t i o n
t o t a l C o u n t : I n t
c o m p l e t e d C o u n t : I n t
}
Schema
schema {
query: Root
mutation: Mutation
}
Relay
Advantages
- Declarative Data Fetching
- Co-locate your Data Requirement
- Caching
- Data Consistency
- Optimistice Updates
- …
GraphQL Relay
Specification
Relay 對
GraphQL Server
有三個要求
1. A mechanism for refetching an object
2. A description of how to page through
connections
3. Structure around mutations to make them
predictable
參閱
https://chentsulin.github.io/relay/docs/graphql-
relay-specification.html#content
這些會藉由
graphql-relay
來達成
Object Identification
import {
nodeDefinitions,
} from 'graphql-relay';
const {
nodeInterface,
nodeField,
} = nodeDefinitions(
globalIdToResource,
objToGraphQLType,
);
Node Definitions
c o n s t G r a p h Q LTo d o = n e w G r a p h Q L O b j e c t Ty p e ( {
n a m e : ' To d o ' ,
f i e l d s : {
i d : g l o b a l I d F i e l d ( ' To d o ' ) ,
t e x t : {
t y p e : G r a p h Q L S t r i n g ,
r e s o l ve : o b j = > o b j . t e x t ,
} ,
c o m p l e t e : {
t y p e : G r a p h Q L B o o l e a n ,
r e s o l ve : o b j = > o b j . c o m p l e t e ,
} ,
} ,
i n t e r f a c e s : [ n o d e I n t e r f a c e ] ,
} ) ;
Implement Node Interface
const Root = new GraphQLObjectType({
name: 'Root',
fields: {
node: nodeField,
},
});
Export to Root Query
Connection
import {
connectionDefinitions,
} from 'graphql-relay’;
const {
connectionType: TodosConnection,
edgeType: GraphQLTodoEdge,
} = connectionDefinitions({
name: 'Todo',
nodeType: GraphQLTodo,
});
Connection Definitions
Mutation
const GraphQLA ddTodoMutation =
mut at ionWithC lient MutationId ({
name: ' A ddTodo ' ,
input Fields : {
t ext : { t ype: new
GraphQLN onN ull ( GraphQLSt ring ) },
},
mut at eA ndGet Payload : ( { t e x t }) = > {
const localTodoId = addTodo ( t ext) ;
ret urn { localTodoId };
},
// …
}) ;
Using mutationWithClientMutationId
/ / …
o u t p u t F i e l d s : {
t o d o E d g e : {
t y p e : G r a p h Q LTo d o E d g e ,
r e s o l v e : ( { l o c a l To d o I d } ) = > {
c o n s t t o d o = g e t To d o ( l o c a l To d o I d ) ;
r e t u r n {
c u r s o r : c u r s o r F o r O b j e c t I n C o n n e c t i o n ( g e t To d o s ( ) ,
t o d o ) ,
n o d e : t o d o ,
} ;
} ,
} ,
v i e w e r : {
t y p e : G r a p h Q L U s e r,
r e s o l v e : ( ) = > g e t V i e w e r ( ) ,
} ,
} ,
Using mutationWithClientMutationId
Walk Through
Simplified Todo
1. Setup (packages, babel-plugin)
2. GraphQL server
3. GraphQL schema & database
4. Frontend entry
5. Routes
6. Components
7. Fragement Composition
8. Mutations
打個廣告…
最近開了
GraphQL Taiwan
fb 社團
https://www.facebook.com/groups/1110869155655
533/
The En
Thanks for Listening

Contenu connexe

Tendances

PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet
PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet
PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet Pôle Systematic Paris-Region
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil Witecki
 
为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?勇浩 赖
 
PyLadies Talk: Learn to love the command line!
PyLadies Talk: Learn to love the command line!PyLadies Talk: Learn to love the command line!
PyLadies Talk: Learn to love the command line!Blanca Mancilla
 
Lisp как универсальная обертка
Lisp как универсальная оберткаLisp как универсальная обертка
Lisp как универсальная оберткаVsevolod Dyomkin
 
CBSE Computer Project for Class 12 ( C++)
CBSE Computer Project for Class 12 ( C++)CBSE Computer Project for Class 12 ( C++)
CBSE Computer Project for Class 12 ( C++)Karan Bora
 
C++の話(本当にあった怖い話)
C++の話(本当にあった怖い話)C++の話(本当にあった怖い話)
C++の話(本当にあった怖い話)Yuki Tamura
 
Playing 44CON CTF for fun and profit
Playing 44CON CTF for fun and profitPlaying 44CON CTF for fun and profit
Playing 44CON CTF for fun and profit44CON
 
VTU Network lab programs
VTU Network lab   programsVTU Network lab   programs
VTU Network lab programsAnanda Kumar HN
 
Network lap pgms 7th semester
Network lap pgms 7th semesterNetwork lap pgms 7th semester
Network lap pgms 7th semesterDOSONKA Group
 
การเข ยนคำส _งควบค_มข__นพ__นฐาน (1)
การเข ยนคำส _งควบค_มข__นพ__นฐาน (1)การเข ยนคำส _งควบค_มข__นพ__นฐาน (1)
การเข ยนคำส _งควบค_มข__นพ__นฐาน (1)HamHam' Kc
 

Tendances (20)

Function Call Stack
Function Call StackFunction Call Stack
Function Call Stack
 
week-12x
week-12xweek-12x
week-12x
 
C code
C codeC code
C code
 
PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet
PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet
PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet
 
Lab loop
Lab loopLab loop
Lab loop
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, code
 
为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?
 
Problemas de Arreglos en c++
Problemas de Arreglos en c++Problemas de Arreglos en c++
Problemas de Arreglos en c++
 
PyLadies Talk: Learn to love the command line!
PyLadies Talk: Learn to love the command line!PyLadies Talk: Learn to love the command line!
PyLadies Talk: Learn to love the command line!
 
How to master C++
How to master C++How to master C++
How to master C++
 
Graph Algebra
Graph AlgebraGraph Algebra
Graph Algebra
 
Lisp как универсальная обертка
Lisp как универсальная оберткаLisp как универсальная обертка
Lisp как универсальная обертка
 
CBSE Computer Project for Class 12 ( C++)
CBSE Computer Project for Class 12 ( C++)CBSE Computer Project for Class 12 ( C++)
CBSE Computer Project for Class 12 ( C++)
 
C++の話(本当にあった怖い話)
C++の話(本当にあった怖い話)C++の話(本当にあった怖い話)
C++の話(本当にあった怖い話)
 
Playing 44CON CTF for fun and profit
Playing 44CON CTF for fun and profitPlaying 44CON CTF for fun and profit
Playing 44CON CTF for fun and profit
 
VTU Network lab programs
VTU Network lab   programsVTU Network lab   programs
VTU Network lab programs
 
Network lap pgms 7th semester
Network lap pgms 7th semesterNetwork lap pgms 7th semester
Network lap pgms 7th semester
 
งานนำเสนอ อาจารย์ลาวัลย์
งานนำเสนอ อาจารย์ลาวัลย์งานนำเสนอ อาจารย์ลาวัลย์
งานนำเสนอ อาจารย์ลาวัลย์
 
Implementing string
Implementing stringImplementing string
Implementing string
 
การเข ยนคำส _งควบค_มข__นพ__นฐาน (1)
การเข ยนคำส _งควบค_มข__นพ__นฐาน (1)การเข ยนคำส _งควบค_มข__นพ__นฐาน (1)
การเข ยนคำส _งควบค_มข__นพ__นฐาน (1)
 

En vedette

From MEAN to the MERN Stack
From MEAN to the MERN StackFrom MEAN to the MERN Stack
From MEAN to the MERN StackTroy Miles
 
GraphQL IndyJS April 2016
GraphQL IndyJS April 2016GraphQL IndyJS April 2016
GraphQL IndyJS April 2016Brad Pillow
 
Zensations Drupal 8 GraphQL Presentation 2015
Zensations Drupal 8 GraphQL Presentation 2015Zensations Drupal 8 GraphQL Presentation 2015
Zensations Drupal 8 GraphQL Presentation 2015Zensations GmbH
 
MERN Presentation, January 2015
MERN Presentation, January 2015MERN Presentation, January 2015
MERN Presentation, January 2015Barry Dyck
 
Relay: Seamless Syncing for React (VanJS)
Relay: Seamless Syncing for React (VanJS)Relay: Seamless Syncing for React (VanJS)
Relay: Seamless Syncing for React (VanJS)Brooklyn Zelenka
 
Work with V8 memory leaks
Work with V8 memory leaksWork with V8 memory leaks
Work with V8 memory leaksRoman Krivtsov
 
React, GraphQL и Relay - вполне себе нормальный компонентный подход (nodkz)
React, GraphQL и Relay - вполне себе нормальный компонентный подход (nodkz)React, GraphQL и Relay - вполне себе нормальный компонентный подход (nodkz)
React, GraphQL и Relay - вполне себе нормальный компонентный подход (nodkz)Pavel Chertorogov
 
GraphQL With Relay Part Deux
GraphQL With Relay Part DeuxGraphQL With Relay Part Deux
GraphQL With Relay Part DeuxBrad Pillow
 
Introduction to GraphQL at API days
Introduction to GraphQL at API daysIntroduction to GraphQL at API days
Introduction to GraphQL at API daysyann_s
 
Migration microservices to GraphQL
Migration microservices to GraphQLMigration microservices to GraphQL
Migration microservices to GraphQLRoman Krivtsov
 
GraphQL vs REST
GraphQL vs RESTGraphQL vs REST
GraphQL vs RESTGreeceJS
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBChun-Kai Wang
 
GraphQL Story: Intro To GraphQL
GraphQL Story: Intro To GraphQLGraphQL Story: Intro To GraphQL
GraphQL Story: Intro To GraphQLRiza Fahmi
 
London React August - GraphQL at The Financial Times - Viktor Charypar
London React August - GraphQL at The Financial Times - Viktor CharyparLondon React August - GraphQL at The Financial Times - Viktor Charypar
London React August - GraphQL at The Financial Times - Viktor CharyparReact London Community
 
Graphql Intro (Tutorial and Example)
Graphql Intro (Tutorial and Example)Graphql Intro (Tutorial and Example)
Graphql Intro (Tutorial and Example)Rafael Wilber Kerr
 

En vedette (20)

From MEAN to the MERN Stack
From MEAN to the MERN StackFrom MEAN to the MERN Stack
From MEAN to the MERN Stack
 
GraphQL IndyJS April 2016
GraphQL IndyJS April 2016GraphQL IndyJS April 2016
GraphQL IndyJS April 2016
 
GraphQL + relay
GraphQL + relayGraphQL + relay
GraphQL + relay
 
Graphql
GraphqlGraphql
Graphql
 
Zensations Drupal 8 GraphQL Presentation 2015
Zensations Drupal 8 GraphQL Presentation 2015Zensations Drupal 8 GraphQL Presentation 2015
Zensations Drupal 8 GraphQL Presentation 2015
 
MERN Presentation, January 2015
MERN Presentation, January 2015MERN Presentation, January 2015
MERN Presentation, January 2015
 
Relay: Seamless Syncing for React (VanJS)
Relay: Seamless Syncing for React (VanJS)Relay: Seamless Syncing for React (VanJS)
Relay: Seamless Syncing for React (VanJS)
 
Work with V8 memory leaks
Work with V8 memory leaksWork with V8 memory leaks
Work with V8 memory leaks
 
React, GraphQL и Relay - вполне себе нормальный компонентный подход (nodkz)
React, GraphQL и Relay - вполне себе нормальный компонентный подход (nodkz)React, GraphQL и Relay - вполне себе нормальный компонентный подход (nodkz)
React, GraphQL и Relay - вполне себе нормальный компонентный подход (nodkz)
 
GraphQL With Relay Part Deux
GraphQL With Relay Part DeuxGraphQL With Relay Part Deux
GraphQL With Relay Part Deux
 
GraphQL & Relay
GraphQL & RelayGraphQL & Relay
GraphQL & Relay
 
Introduction to GraphQL at API days
Introduction to GraphQL at API daysIntroduction to GraphQL at API days
Introduction to GraphQL at API days
 
Migration microservices to GraphQL
Migration microservices to GraphQLMigration microservices to GraphQL
Migration microservices to GraphQL
 
GraphQL vs REST
GraphQL vs RESTGraphQL vs REST
GraphQL vs REST
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Swift + GraphQL
Swift + GraphQLSwift + GraphQL
Swift + GraphQL
 
GraphQL
GraphQLGraphQL
GraphQL
 
GraphQL Story: Intro To GraphQL
GraphQL Story: Intro To GraphQLGraphQL Story: Intro To GraphQL
GraphQL Story: Intro To GraphQL
 
London React August - GraphQL at The Financial Times - Viktor Charypar
London React August - GraphQL at The Financial Times - Viktor CharyparLondon React August - GraphQL at The Financial Times - Viktor Charypar
London React August - GraphQL at The Financial Times - Viktor Charypar
 
Graphql Intro (Tutorial and Example)
Graphql Intro (Tutorial and Example)Graphql Intro (Tutorial and Example)
Graphql Intro (Tutorial and Example)
 

Similaire à GraphQL Relay Introduction

PostgreSQL Day italy 2016 Unit Test
PostgreSQL Day italy 2016 Unit TestPostgreSQL Day italy 2016 Unit Test
PostgreSQL Day italy 2016 Unit TestAndrea Adami
 
Spring Roo 2.0 Preview at Spring I/O 2016
Spring Roo 2.0 Preview at Spring I/O 2016 Spring Roo 2.0 Preview at Spring I/O 2016
Spring Roo 2.0 Preview at Spring I/O 2016 DISID
 
Breathe life into your designer!
Breathe life into your designer!Breathe life into your designer!
Breathe life into your designer!Cédric Brun
 
Hardware Description Languages .pptx
Hardware Description Languages .pptxHardware Description Languages .pptx
Hardware Description Languages .pptxwafawafa52
 
Ember js meetup treviso liquid-fire
Ember js meetup treviso liquid-fireEmber js meetup treviso liquid-fire
Ember js meetup treviso liquid-fireWilliam Bergamo
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler DevelopmentLogan Chien
 
Code GPU with CUDA - Identifying performance limiters
Code GPU with CUDA - Identifying performance limitersCode GPU with CUDA - Identifying performance limiters
Code GPU with CUDA - Identifying performance limitersMarina Kolpakova
 
Piotr Szotkowski about "Ruby smells"
Piotr Szotkowski about "Ruby smells"Piotr Szotkowski about "Ruby smells"
Piotr Szotkowski about "Ruby smells"Pivorak MeetUp
 
Modeling avengers – open source technology mix for saving the world
Modeling avengers – open source technology mix for saving the worldModeling avengers – open source technology mix for saving the world
Modeling avengers – open source technology mix for saving the worldCédric Brun
 
Modeling avengers – open source technology mix for saving the world econ fr
Modeling avengers – open source technology mix for saving the world econ frModeling avengers – open source technology mix for saving the world econ fr
Modeling avengers – open source technology mix for saving the world econ frCédric Brun
 
Android & PostgreSQL
Android & PostgreSQLAndroid & PostgreSQL
Android & PostgreSQLMark Wong
 
No Flex Zone: Empathy Driven Development
No Flex Zone: Empathy Driven DevelopmentNo Flex Zone: Empathy Driven Development
No Flex Zone: Empathy Driven DevelopmentDuretti H.
 
Testing Fuse Fabric with Pax Exam
Testing Fuse Fabric with Pax ExamTesting Fuse Fabric with Pax Exam
Testing Fuse Fabric with Pax ExamHenryk Konsek
 
Advanced QUnit - Front-End JavaScript Unit Testing
Advanced QUnit - Front-End JavaScript Unit TestingAdvanced QUnit - Front-End JavaScript Unit Testing
Advanced QUnit - Front-End JavaScript Unit TestingLars Thorup
 
Using TypeScript at Dashlane
Using TypeScript at DashlaneUsing TypeScript at Dashlane
Using TypeScript at DashlaneDashlane
 
Advanced Use of Properties and Scripts in TIBCO Spotfire
Advanced Use of Properties and Scripts in TIBCO SpotfireAdvanced Use of Properties and Scripts in TIBCO Spotfire
Advanced Use of Properties and Scripts in TIBCO SpotfireHerwig Van Marck
 

Similaire à GraphQL Relay Introduction (20)

PostgreSQL Day italy 2016 Unit Test
PostgreSQL Day italy 2016 Unit TestPostgreSQL Day italy 2016 Unit Test
PostgreSQL Day italy 2016 Unit Test
 
Spring Roo 2.0 Preview at Spring I/O 2016
Spring Roo 2.0 Preview at Spring I/O 2016 Spring Roo 2.0 Preview at Spring I/O 2016
Spring Roo 2.0 Preview at Spring I/O 2016
 
Breathe life into your designer!
Breathe life into your designer!Breathe life into your designer!
Breathe life into your designer!
 
Hardware Description Languages .pptx
Hardware Description Languages .pptxHardware Description Languages .pptx
Hardware Description Languages .pptx
 
Ember js meetup treviso liquid-fire
Ember js meetup treviso liquid-fireEmber js meetup treviso liquid-fire
Ember js meetup treviso liquid-fire
 
GraphQL, l'avenir du REST ?
GraphQL, l'avenir du REST ?GraphQL, l'avenir du REST ?
GraphQL, l'avenir du REST ?
 
JavaFX, because you're worth it
JavaFX, because you're worth itJavaFX, because you're worth it
JavaFX, because you're worth it
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler Development
 
Code GPU with CUDA - Identifying performance limiters
Code GPU with CUDA - Identifying performance limitersCode GPU with CUDA - Identifying performance limiters
Code GPU with CUDA - Identifying performance limiters
 
Geb for browser automation
Geb for browser automationGeb for browser automation
Geb for browser automation
 
Piotr Szotkowski about "Ruby smells"
Piotr Szotkowski about "Ruby smells"Piotr Szotkowski about "Ruby smells"
Piotr Szotkowski about "Ruby smells"
 
Modeling avengers – open source technology mix for saving the world
Modeling avengers – open source technology mix for saving the worldModeling avengers – open source technology mix for saving the world
Modeling avengers – open source technology mix for saving the world
 
Modeling avengers – open source technology mix for saving the world econ fr
Modeling avengers – open source technology mix for saving the world econ frModeling avengers – open source technology mix for saving the world econ fr
Modeling avengers – open source technology mix for saving the world econ fr
 
Android & PostgreSQL
Android & PostgreSQLAndroid & PostgreSQL
Android & PostgreSQL
 
No Flex Zone: Empathy Driven Development
No Flex Zone: Empathy Driven DevelopmentNo Flex Zone: Empathy Driven Development
No Flex Zone: Empathy Driven Development
 
Testing Fuse Fabric with Pax Exam
Testing Fuse Fabric with Pax ExamTesting Fuse Fabric with Pax Exam
Testing Fuse Fabric with Pax Exam
 
Advanced QUnit - Front-End JavaScript Unit Testing
Advanced QUnit - Front-End JavaScript Unit TestingAdvanced QUnit - Front-End JavaScript Unit Testing
Advanced QUnit - Front-End JavaScript Unit Testing
 
Using TypeScript at Dashlane
Using TypeScript at DashlaneUsing TypeScript at Dashlane
Using TypeScript at Dashlane
 
javapravticalfile.doc
javapravticalfile.docjavapravticalfile.doc
javapravticalfile.doc
 
Advanced Use of Properties and Scripts in TIBCO Spotfire
Advanced Use of Properties and Scripts in TIBCO SpotfireAdvanced Use of Properties and Scripts in TIBCO Spotfire
Advanced Use of Properties and Scripts in TIBCO Spotfire
 

Dernier

Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadhamedmustafa094
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxNadaHaitham1
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARKOUSTAV SARKAR
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwaitjaanualu31
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilVinayVitekari
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 

Dernier (20)

Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 

GraphQL Relay Introduction

  • 2. chentsulin chentsulin C. T. Lin Active JavaScript Open Source Developer System Engineer Lead@Yoctol
  • 3. Repos - electron-react-boilerplate - awesome-graphql - koa-graphql - sweetalert-react Translations - redux - graphql - relay PR Welcome !
  • 5. 今天的範例放在 k o a - g r a p h q l - r e l a y - e x a m p l e ( h t t p s : / / g o o . g l / g t 3 i A h )
  • 8. Problems - A lot of Endpoints - Breakings cause client/server Mismatch - Overfetching and Underfetching - Nested Resources - Lack of Type System
  • 10. { view er { id t ot alC ount } } ResponseRequest { "dat a ": { "view er": { "id": " VXN lc jpt ZQ = = ", "t ot alC ount ” : 2 } } }
  • 13. { v i e w e r { i d t o t a l C o u n t . . . t o d o I n f o } } f r a g m e n t t o d o I n f o o n U s e r { t o d o s { e d g e s { n o d e { i d } } } } ResponseRequest { " d a t a " : { " v i e w e r " : { " i d " : " V X N l c j p t Z Q = = " , " t o t a l C o u n t " : 2 , " t o d o s " : { " e d g e s " : [ { " n o d e " : { " i d " : " V G 9 k b z o w " } } , { " n o d e " : { " i d " : " V G 9 k b z o x " } } ] } } } }
  • 15. m u t a t i o n A d d To d o ( $ i n p u t : A d d To d o I n p u t ! ) { a d d To d o ( i n p u t : $ i n p u t ) { t o d o E d g e { n o d e { i d t e x t c o m p l e t e } } } } ResponseRequest { " d a t a " : { " a d d To d o " : { " t o d o E d g e " : { " n o d e " : { " i d " : " V G 9 k b z o z " , " t e x t " : " t e s t " , " c o m p l e t e " : f a l s e } } } } } { " i n p u t " : { " t e x t " : " t e s t " , " c l i e n t M u t a t i o n I d " : " 0 " } }
  • 17. t y p e To d o i m p l e m e n t s N o d e { # T h e I D o f a n o b j e c t i d : I D ! t e x t : S t r i n g c o m p l e t e : B o o l e a n } t y p e U s e r i m p l e m e n t s N o d e { # T h e I D o f a n o b j e c t i d : I D ! t o d o s ( a f t e r : S t r i n g , f i r s t : I n t , b e f o r e : S t r i n g , l a s t : I n t ) : To d o C o n n e c t i o n t o t a l C o u n t : I n t c o m p l e t e d C o u n t : I n t }
  • 20. Relay
  • 21. Advantages - Declarative Data Fetching - Co-locate your Data Requirement - Caching - Data Consistency - Optimistice Updates - …
  • 24. 1. A mechanism for refetching an object 2. A description of how to page through connections 3. Structure around mutations to make them predictable 參閱 https://chentsulin.github.io/relay/docs/graphql- relay-specification.html#content
  • 27. import { nodeDefinitions, } from 'graphql-relay'; const { nodeInterface, nodeField, } = nodeDefinitions( globalIdToResource, objToGraphQLType, ); Node Definitions
  • 28. c o n s t G r a p h Q LTo d o = n e w G r a p h Q L O b j e c t Ty p e ( { n a m e : ' To d o ' , f i e l d s : { i d : g l o b a l I d F i e l d ( ' To d o ' ) , t e x t : { t y p e : G r a p h Q L S t r i n g , r e s o l ve : o b j = > o b j . t e x t , } , c o m p l e t e : { t y p e : G r a p h Q L B o o l e a n , r e s o l ve : o b j = > o b j . c o m p l e t e , } , } , i n t e r f a c e s : [ n o d e I n t e r f a c e ] , } ) ; Implement Node Interface
  • 29. const Root = new GraphQLObjectType({ name: 'Root', fields: { node: nodeField, }, }); Export to Root Query
  • 31. import { connectionDefinitions, } from 'graphql-relay’; const { connectionType: TodosConnection, edgeType: GraphQLTodoEdge, } = connectionDefinitions({ name: 'Todo', nodeType: GraphQLTodo, }); Connection Definitions
  • 33. const GraphQLA ddTodoMutation = mut at ionWithC lient MutationId ({ name: ' A ddTodo ' , input Fields : { t ext : { t ype: new GraphQLN onN ull ( GraphQLSt ring ) }, }, mut at eA ndGet Payload : ( { t e x t }) = > { const localTodoId = addTodo ( t ext) ; ret urn { localTodoId }; }, // … }) ; Using mutationWithClientMutationId
  • 34. / / … o u t p u t F i e l d s : { t o d o E d g e : { t y p e : G r a p h Q LTo d o E d g e , r e s o l v e : ( { l o c a l To d o I d } ) = > { c o n s t t o d o = g e t To d o ( l o c a l To d o I d ) ; r e t u r n { c u r s o r : c u r s o r F o r O b j e c t I n C o n n e c t i o n ( g e t To d o s ( ) , t o d o ) , n o d e : t o d o , } ; } , } , v i e w e r : { t y p e : G r a p h Q L U s e r, r e s o l v e : ( ) = > g e t V i e w e r ( ) , } , } , Using mutationWithClientMutationId
  • 36.
  • 37. 1. Setup (packages, babel-plugin) 2. GraphQL server 3. GraphQL schema & database 4. Frontend entry 5. Routes 6. Components 7. Fragement Composition 8. Mutations
  • 40. The En Thanks for Listening

Notes de l'éditeur

  1. https://github.com/chentsulin