graphql + relay

46
GraphQL + Relay @telligcirdec + @catelandaxel

Upload: cedric-gillet

Post on 16-Apr-2017

651 views

Category:

Software


1 download

TRANSCRIPT

Page 1: GraphQL + relay

GraphQL + Relay

@telligcirdec + @catelandaxel

Page 2: GraphQL + relay

Cédric GILLET

Développeur JAVA / Architect

Axel CATELAND

Développeur Frontend / Leadtech Front

Bientôt =>

Page 3: GraphQL + relay

GraphQL Specification

The boring part

Page 4: GraphQL + relay

What is GraphQL ?

● An open sourced specification edited by Facebook still in draft

● Define a query language to query, fetch and mutate data from any datasources in a declarative way

Page 5: GraphQL + relay

What is NOT GraphQL ?

● NOT a framework

● NOT specific to a language

● NOT as easy as you think

Page 6: GraphQL + relay

Keys concepts● Hierarchical : a GraphQL query is a hierarchical set of fields

● Product-centric : the consumer decide what and how

● Strong-typing : queries are syntactically checked and provided data are type safe

● Introspective : clients and tools can query the type system using the GraphQL syntax

● Application-layer protocol : use it via FTP, you fools !!

Page 7: GraphQL + relay

(hidden) Keys concepts

● You gonna need some cache

● You gonna need some papers and a good knowledge of your data

● You gonna need some red bull

Page 8: GraphQL + relay

GraphQL ImplementationExploitation

May the force be with you !

Page 9: GraphQL + relay

Meet SWAPI

A P I

SWAPI

Page 10: GraphQL + relay

Meet SWAPI

● The Star Wars API, or "swapi" (Swah-pee) is the world's first quantified and programmatically-accessible data source for all the data from the Star Wars canon universe!

● All the data is accessible through a HTTP web API.

SWAPI

Page 11: GraphQL + relay

SWAPI Examples

GO ! GO ! GO !

SWAPI

Page 12: GraphQL + relay

GraphQL : MappingConsist of creating links between the backend data and graphql fields

SWAPI

Page 13: GraphQL + relay

GraphQL : TypingSWAPI

Page 14: GraphQL + relay

GraphQL : Typing● Consist of adding a strong type to GraphQL fields● Exhaustive list of types

○ Scalar■ Int / Float / String / Boolean / ID

○ Object○ Interface○ Union○ Enum○ List○ Input Object○ NonNull

SWAPI

Page 15: GraphQL + relay

GraphQL : IntrospectionA GraphQL server supports introspection over its schema by using GraphQL itself

SWAPI

Page 16: GraphQL + relay

GraphQL : Introspection

{ __type(name: “User”) { name, fields { name, type { name } } }}

{ “data” : { “__type”: { “name” : “User”, “fields” : [ { “name” : “id”, “type” : { “name” : “String” } }, { “name” : “name”, “type” : { “name” : “String” } }, { “name” : “gender”, “type” : { “name” : “Boolean” } }, ] }}

SWAPI

Page 17: GraphQL + relay

GraphQL : Arguments ● Arguments allow to pass arguments to your GraphQL

query

{ posts(category:METEOR){ title, category, content }}

SWAPI

Page 18: GraphQL + relay

GraphQL : Adding cacheAdding cache not on the GraphQL schema but on data fetcher.

SWAPI

Page 19: GraphQL + relay

GraphQL : Several data sourcesSWAPI

Page 20: GraphQL + relay

GraphQL : MutationsCreating a specific endpoint on the GraphQL Schema for a client to ask for data mutation on the backend data

SWAPI

Page 21: GraphQL + relay

GraphQL : Directives and Variables

SWAPI

● Variable ability allows to add variable to your queries

● Directive ability provides a way to describe alternate runtime execution and type validation directly in your queries

query myQuery($someTest: Boolean) {

experimentalField @skip(if: $someTest)

}

Page 22: GraphQL + relay

GraphQL usefull links

● http://graphql.org/● http://facebook.github.io/graphql/● https://sandbox.learngraphql.com/● https://learngraphql.com/● https://code.facebook.com

Page 23: GraphQL + relay

Relay

Best use of GraphQL for moderns webapps ? Why ? How ?

Page 24: GraphQL + relay

How relay help us ?

● To create true independents/reusables components.● To further optimize data fetching.● Static query check at build time (babel plugin)● Effortless data mutation (Nope)

https://facebook.github.io/relay/

https://github.com/facebook/relay/

Page 25: GraphQL + relay

From graph data to components !

Expliquer le mapping de données sur

un composant

Relay container

Relay route

Page 26: GraphQL + relay

Relay.route

A query targeting an entry point of our GraphQL endpoint.

Page 27: GraphQL + relay

Relay.RootContainer

Compose a Relay Route and a Relay enhanced component.

Page 28: GraphQL + relay

Relay.container

Wrap a React component.

Declare needed data inside a GraphQL Fragment

Page 29: GraphQL + relay

Lets start to compose

Page 30: GraphQL + relay

Fragment and reusability

Page 31: GraphQL + relay

Relay compose and solve !

Under data fetching

Over data fetching

Use the best of GraphQL

tl/dr kill the need of ad’hoc endpoints

Page 32: GraphQL + relay

Under data fetching

Page 33: GraphQL + relay

Under data fetching

Page 34: GraphQL + relay

Over data fetching

Page 35: GraphQL + relay

Solved !

Page 36: GraphQL + relay

Store ?

Relay graph local cache

Reducing graph request sent to GraphQL endpoint

Reducing graph response

Pushing mutations

Page 37: GraphQL + relay

Graph Local Cache

Page 38: GraphQL + relay

Graph Local Cache

Page 39: GraphQL + relay

GraphQL Relay specification

● Unique ID for every element● Endpoint to refetch object one by one● Pagination through connections

○ Unique ID for a connection

○ Unique ID for each cursor in a connection

● Input and output of mutation Unique ID

Page 40: GraphQL + relay

GraphQL Relay to save the day

GraphQL Relay provide api to help fulfilling these request.

GraphQL Relay live in your GraphQL Endpoint

https://github.com/graphql/graphql-relay-js

Page 41: GraphQL + relay

Mutation - Welcome to hell

(seriously hardest part of relay)

Page 42: GraphQL + relay

Mutation

Relay push mutation to server

And ask back as a payload an intersection between what it need and what changed on the server.

Can mutate local cache optimistically.

Page 43: GraphQL + relay

Mutation - payload intersection

Page 44: GraphQL + relay

App State

Binding with app routing

No app state stored only app data

Page 46: GraphQL + relay

QUESTION(S) ?