exploring relay land

36
Exploring Relay-land Stefano Masini @stefanomasini

Upload: stefano-masini

Post on 15-Apr-2017

543 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Exploring Relay land

Exploring Relay-land

Stefano Masini @stefanomasini

Page 2: Exploring Relay land

Relay

• Automatic fetching of data, no more Ajax

• Colocation of queries and code

• Caching

• Good for API mashup

• Why not?

Page 3: Exploring Relay land

A new beginning

• How to fetch data from the server?

• How to write data to the server?

• How to handle URL routing?

• How to manage application (UI) state?

• How to propagate server push notifications?

Page 4: Exploring Relay land

Size of libraries

LOC - *.js in github project as of Feb 2016

0 K

20 K

40 K

60 K

80 K

100 K

React Relay GraphQL ReactRouter

Redux

Page 5: Exploring Relay land

Popularity of libraries

# stars in github project as of Feb 2016

0 K

8 K

16 K

24 K

32 K

40 K

React Relay GraphQL ReactRouter

Redux

Page 6: Exploring Relay land

Stars per Line Of Code

AKA what's the smartest idea out there? :-)

React Relay GraphQL ReactRouter

Redux

Page 7: Exploring Relay land

Topics

• How to fetch data from the server?

• How to write data to the server?

• How to handle URL routing?

• How to manage application (UI) state?

• How to propagate server push notifications?

Page 8: Exploring Relay land

GraphQL

• GOALS:

• Space efficient (on the wire)

• Computationally efficient (server side)

Page 9: Exploring Relay land

GraphQL

DEMO

Page 10: Exploring Relay land

GraphQL is a type systemUser

Site Site

Project Project Project

User Type

Site Type

Project Type

SitesOfUser Type

ProjectsOfSite Type

Page 11: Exploring Relay land

Edges

Edges

Node

Node

Node

Relay enhanced Type System

User

Site Site

Project Project Project

User Type

Site Type

SitesOfUser Type

Project Type

ProjectsOfSite Type

Page 12: Exploring Relay land

Relay caching (UI)

User X Site A Project 1 Project 2

Relay cache

React view

Server

Page 13: Exploring Relay land

Schemalet schema = new GraphQLSchema({ query: new GraphQLObjectType({ name: 'RootQueryType', fields: () => ({ node: nodeField, loggedUser: { type: UserType, description: 'The logged user', resolve: (_, _, {rootValue: {loggedUser}}) => loggedUser, }, }) }), });

Page 14: Exploring Relay land

The User Typelet UserType = new GraphQLObjectType({ name: 'User', interfaces: [nodeInterface], fields: () => ({ id: globalIdField('User', user => user.id), node: nodeField, sites: { type: UsersInSiteConnection, description: 'All sites the logged user has access to', resolve: (user, args, {rootValue: {db}}) => connectionFromPromisedArray( db.getSitesForAUser(user), args ), }, }) });

Page 15: Exploring Relay land

Querying with RESTUser

Site Site

Project Project Project

/user/14/all_my_sites

SELECT * FROM users LEFT JOIN sites LEFT JOIN projects WHERE ...

Page 16: Exploring Relay land

GraphQL is like an ORMUser

Site Site

so, beware!

query { user(userId: "14") { sites { ... } }}

Page 17: Exploring Relay land

GraphQL is like an ORMUser

Site Site

Project Project Project

so, beware!

query { user(userId: "14") { sites { projects { ... } } }}

Page 18: Exploring Relay land

GraphQL is like an ORMUser

Site Site

Project Project Project

so, beware!

resolve(...)

resolve(...)

SELECT * FROM sites WHERE ...

SELECT * FROM projects WHERE ...

n x

Page 19: Exploring Relay land

Topics

• How to fetch data from the server?

• How to write data to the server?

• How to handle URL routing?

• How to manage application (UI) state?

• How to propagate server push notifications?

Page 20: Exploring Relay land

MutationsServer

Client

"Fat query"

Page 21: Exploring Relay land

Topics

• How to fetch data from the server?

• How to write data to the server?

• How to handle URL routing?

• How to manage application (UI) state?

• How to propagate server push notifications?

Page 22: Exploring Relay land

@relayContainer({ fragments: { user: () => Relay.QL` fragment on User { firstName, lastName, } `, }, }) class UserFullName extends React.Component { render() { return <p> {this.props.user.firstName} {this.props.user.lastName} </p>; } }

Rendering in Relay

Page 23: Exploring Relay land

Query ConfigurationRelay Container

Rendering in Relay

Component

Page 24: Exploring Relay land

Rendering in React-Router/user/14/settings

/ <Application>

user/14 <User>

settings <Settings>

Page 25: Exploring Relay land

/ Q RC <Application>

user/14 Q RC <User>

Relay + Router NAIVE/user/14/settings

settings Q RC <Settings>

Page 26: Exploring Relay land

Smart Root Container

/ RC <Application>

user/14 RC <User>

react-router-relay/user/14/settings

settings RC <Settings>

Page 27: Exploring Relay land

Topics

• How to fetch data from the server?

• How to write data to the server?

• How to handle URL routing?

• How to manage application (UI) state?

• How to propagate server push notifications?

Page 28: Exploring Relay land

Relay + ReduxRelay

data storeRedux

data store

• Site Name • Projects in a Site

• Dialog (open/close) • New project form

Edit existing project form???

Page 29: Exploring Relay land

Topics

• How to fetch data from the server?

• How to write data to the server?

• How to handle URL routing?

• How to manage application (UI) state?

• How to propagate server push notifications?

Page 30: Exploring Relay land

Keeping clients in sync

Server

Client

Cache

Client

Cache

Client

Cache

AKA: Live Queries

Page 31: Exploring Relay land

Subscriptions*

• GraphQL "subscription" operator

• Modeled after "events"

• Client must register/deregister

• Server must fire the right events in the right places

* yet to come, as of Feb 2016

Page 32: Exploring Relay land

I like Relay and GraphQL

• Modeling this way is fun

• It Just Makes Sense®

• It scales well

Page 33: Exploring Relay land

RELAY IS

AWESOMEbut...

Page 34: Exploring Relay land

Limitations*

• Poor error handling

• ORM-like, hard to optimize DB access

• Root queries

• Subscriptions

* as of Feb 2016

Page 35: Exploring Relay land

Relay is COMPLEX

Is you app COMPLEX too?

YES

Give it a try!

NO

Wanna learn?

Not today, thanks

Stay with REST and Redux

YES

Page 36: Exploring Relay land

Thanks!

@stefanomasini

video and slides