graphql relay introduction

40
GraphQL & Relay 入入

Upload: chen-tsu-lin

Post on 07-Jan-2017

110 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: GraphQL Relay Introduction

GraphQL & Relay 入 門

Page 2: GraphQL Relay Introduction

chentsulin

chentsulin

C. T. LinActive JavaScript Open Source Developer

System Engineer Lead@Yoctol

Page 3: GraphQL Relay Introduction

Repos- electron-react-boilerplate- awesome-graphql- koa-graphql- sweetalert-react

Translations- redux- graphql- relay

PR Welcome !

Page 4: GraphQL Relay Introduction

進 入 正 題

Page 5: GraphQL Relay Introduction

今 天 的 範 例 放 在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 )

Page 6: GraphQL Relay Introduction

GraphQL

Page 7: GraphQL Relay Introduction

Why?RESTfu l d idn’ t sca le

Page 8: GraphQL Relay Introduction

Problems- A lot of Endpoints- Breakings cause client/server Mismatch- Overfetching and Underfetching- Nested Resources- Lack of Type System

Page 9: GraphQL Relay Introduction

叫作 QL最強大的當然是它的 Query

Page 10: GraphQL Relay Introduction

{ v iewer { id tota lCount } }

ResponseRequest

{ "data" : { "v iewer" : { " id" : "VXNlc jptZQ==", " tota lCount”: 2 } } }

Page 11: GraphQL Relay Introduction

順便介紹一下重要的 GraphiQL

Page 12: GraphQL Relay Introduction

還有重要的 Fragment

Page 13: GraphQL Relay Introduction

{ 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 " } } ] } } } }

Page 14: GraphQL Relay Introduction

再來就是用來處理 side effects 的

Mutation

Page 15: GraphQL Relay Introduction

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{ "data" : { "addTodo" : { " todoEdge" : { "node" : { " i d " : "VG9kbzoz" , " t ex t " : " tes t " , " comple te" : fa l se } } } }}

{ " 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 " }}

Page 16: GraphQL Relay Introduction

Type System

Page 17: GraphQL Relay Introduction

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 , fi 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}

Page 18: GraphQL Relay Introduction

Schema

Page 19: GraphQL Relay Introduction

schema { query: Root mutation: Mutation}

Page 20: GraphQL Relay Introduction

Relay

Page 21: GraphQL Relay Introduction

Advantages- Declarative Data Fetching- Co-locate your Data Requirement- Caching- Data Consistency- Optimistice Updates- …

Page 22: GraphQL Relay Introduction

GraphQL Relay Specificat ion

Page 23: GraphQL Relay Introduction

Relay 對 GraphQL Server有三個要求

Page 24: GraphQL Relay Introduction

1. A mechanism for refetching an object2. A description of how to page through

connections3. Structure around mutations to make them

predictable

參閱 https://chentsulin.github.io/relay/docs/graphql-relay-specification.html#content

Page 25: GraphQL Relay Introduction

這些會藉由 graphql-relay來達成

Page 26: GraphQL Relay Introduction

Object Identification

Page 27: GraphQL Relay Introduction

import { nodeDefinitions,} from 'graphql-relay' ;

const { nodeInterface, nodeField, } = nodeDefinitions( globalIdToResource, objToGraphQLType,);

Node Definitions

Page 28: GraphQL Relay Introduction

const GraphQLTodo = new GraphQLObjec tType ({ name: ' Todo ' , fie lds : { i d : g loba l IdF ie ld ( ' Todo ' ) , t ex t : { t ype : GraphQLSt r ing , reso lve : ob j => ob j . tex t , } , comp le te : { t ype : GraphQLBoo lean , reso lve : ob j => ob j . comple te , } , } , i n te r faces : [ node In ter fac e ] ,} ) ;

Implement Node Interface

Page 29: GraphQL Relay Introduction

const Root = new GraphQLObjectType({ name: 'Root', fields: { node: nodeField, },});

Export to Root Query

Page 30: GraphQL Relay Introduction

Connection

Page 31: GraphQL Relay Introduction

import { connectionDefinit ions,} from 'graphql-relay’;

const { connectionType: TodosConnection, edgeType: GraphQLTodoEdge,} = connectionDefinit ions({ name: 'Todo', nodeType: GraphQLTodo,});

Connection Definitions

Page 32: GraphQL Relay Introduction

Mutation

Page 33: GraphQL Relay Introduction

const GraphQLAddTodoMutat ion = mutat ionWithCl ientMutat ion Id ({ name: 'AddTodo ' , inputF ie lds : { text : { type: new GraphQLNonNul l (GraphQLStr ing) } , } , mutateAndGetPay load: ({ text }) => { const loca lTodoId = addTodo(text) ; return { loca lTodoId } ; } , / / …}) ;

Using mutationWithClientMutationId

Page 34: GraphQL Relay Introduction

/ / … 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 Fo 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 LU s e r , r e s o l v e : ( ) = > g e t V i e w e r ( ) , } , } ,

Using mutationWithClientMutationId

Page 35: GraphQL Relay Introduction

Walk Through Simplified Todo

Page 36: GraphQL Relay Introduction
Page 37: GraphQL Relay Introduction

1. Setup (packages, babel-plugin)2. GraphQL server3. GraphQL schema & database4. Frontend entry5. Routes6. Components7. Fragement Composition8. Mutations

Page 38: GraphQL Relay Introduction

打個廣告…

Page 40: GraphQL Relay Introduction

The EndThanks for L isten ing