Slide 1

Slide 1 text

GraphQL: Client-Driven Development Dan Schafer Software Engineer, Facebook @dlschafer

Slide 2

Slide 2 text

https://facebook.github.io/react/blog/2015/05/01/graphql-introduction.html

Slide 3

Slide 3 text

Product-centric: GraphQL is unapologetically driven by the requirements of views and the front-end engineers that write them. We start with their way of thinking and requirements and build the language and runtime necessary to enable that. https://facebook.github.io/react/blog/2015/05/01/graphql-introduction.html

Slide 4

Slide 4 text

History of GraphQL July ’15 Aug ’12 Evolution Open Source Feb ’12 May ’17 Prototype

Slide 5

Slide 5 text

History of GraphQL Aug ’12 Prototype Feb ’12

Slide 6

Slide 6 text

Think Graphs, not Endpoints Single Source of Truth Thin API layer

Slide 7

Slide 7 text

Jan ’12 Prototype iOS Client {stories: {…}} SELECT * FROM stories Server

Slide 8

Slide 8 text

Jan ’12 Prototype iOS Client {stories: {…}} SELECT * FROM stories Server SELECT stories.id, stories.title, users.name, COUNT(likers.id) FROM stories LEFT JOIN users ON users.id = stories.author_id LEFT JOIN likers ON likers.post_id = stories.id LIMIT 10

Slide 9

Slide 9 text

Jan ’12 Prototype iOS Client {stories: {…}} SELECT * FROM stories Server

Slide 10

Slide 10 text

Jan ’12 Prototype iOS Client FQL Business Logic
 +
 Storage Layer {stories: {…}} SELECT * FROM stories

Slide 11

Slide 11 text

Feb ’12 Prototype iOS Client Business Logic
 +
 Storage Layer {stories: {…}} stories.fields(title)

Slide 12

Slide 12 text

Feb ’12 Prototype iOS Client GQL Business Logic
 +
 Storage Layer {stories: {…}} stories.fields(title)

Slide 13

Slide 13 text

Think Graphs, not Endpoints Single Source of Truth Thin API layer

Slide 14

Slide 14 text

History of GraphQL July ’15 Aug ’12 Evolution

Slide 15

Slide 15 text

1. SCALING MODELS 2. SCALING VIEWS 3. SCALING THE APP

Slide 16

Slide 16 text

query { me { name profilePicture { width height url } } }

Slide 17

Slide 17 text

nickname profilePicture { width height url } } } query { me { name

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

CoreData XML Files

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Generated Code

Slide 25

Slide 25 text

Codegen

Slide 26

Slide 26 text

query { me { name profilePicture { height width uri } } } Codegen

Slide 27

Slide 27 text

query { me { name profilePicture { height width uri } } } Codegen

Slide 28

Slide 28 text

Static Queries (a.k.a. .graphql files)

Slide 29

Slide 29 text

FBGraphQLNode *user = [self graphQLNode]; [user addField:FBUserFields.name expectedType:kFBGraphQLTypeString]; return user; Code Reuse

Slide 30

Slide 30 text

FBGraphQLNode *user = [self graphQLNode]; [user addField:FBUserFields.name expectedType:kFBGraphQLTypeString]; FBAttachProfilePictureGraphQLSubnode(user); return user; Code Reuse

Slide 31

Slide 31 text

query { me { name profilePicture { height width uri } } }

Slide 32

Slide 32 text

query { me { name ...ProfilePicture } }

Slide 33

Slide 33 text

Codegen → Fragments for Code Reuse

Slide 34

Slide 34 text

query { me { name ...ProfilePicture } } fragment ProfilePicture on User { profilePicture { height width uri } }

Slide 35

Slide 35 text

query { me { name profilePicture { height width uri } } }

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Client-Driven Development

Slide 39

Slide 39 text

1. SCALING MODELS 2. SCALING VIEWS 3. SCALING THE APP

Slide 40

Slide 40 text

CoreData XML Files

Slide 41

Slide 41 text

CoreData XML Files

Slide 42

Slide 42 text

React

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

Litho

Slide 45

Slide 45 text

Flux

Slide 46

Slide 46 text

Flux Dispatcher Action Store View Action

Slide 47

Slide 47 text

GraphQL Clients circa 2013 Dispatcher Action Store View Action GraphQL
 Queries

Slide 48

Slide 48 text

GraphQL Clients circa 2013 Dispatcher Action Store View Action GraphQL
 Queries GraphQL
 Engine

Slide 49

Slide 49 text

GraphQL Clients circa 2013 Dispatcher Action Store View Action GraphQL
 Queries GraphQL
 Engine Immutable
 Model
 Store

Slide 50

Slide 50 text

GraphQL Clients circa 2013 Dispatcher Action Store View Action GraphQL
 Queries GraphQL
 Engine Immutable
 Model
 Store Reactive
 Views

Slide 51

Slide 51 text

GraphQL Clients circa 2013 GraphQL
 Engine GraphQL
 Queries Immutable
 Model
 Store Reactive
 Views GraphQL
 Mutations

Slide 52

Slide 52 text

Flux → GraphQL Mutations

Slide 53

Slide 53 text

“Why top level mutations?”

Slide 54

Slide 54 text

“Why actions and not CRUD?”

Slide 55

Slide 55 text

GraphQL Clients circa 2013 GraphQL
 Engine GraphQL
 Queries Immutable
 Model
 Store Reactive
 Views GraphQL
 Mutations

Slide 56

Slide 56 text

Mutations

Slide 57

Slide 57 text

Mutations?

Slide 58

Slide 58 text

Client-Driven Development

Slide 59

Slide 59 text

true POST /1/like/ {page:{likers:{count:1}}} {page(id:1){likers{count}}}

Slide 60

Slide 60 text

true POST /1/like/ {page:{likers:{count:1}}} {page(id:1){likers{count}}} Batch API

Slide 61

Slide 61 text

REST Response REST POST Query Response GraphQL Query Batch API Client Shim GraphQL Mutation Mutation Response

Slide 62

Slide 62 text

Incrementally Add Value

Slide 63

Slide 63 text

{ "page_like": { "graph_endpoint": { "method": "POST", "url": { "format": "%@/likes", "variables": ["${page_id}"] }, "params": { "analytics": "JSON(${analytics})" } }, "input": { "page_id": "String!", "analytics": "[String!]" }, "payload": { "page": { "type": "Page", "query": "{node($id){...Contents}}", "query_variables": { "id": "${page_id}" } } } } } mutations.json

Slide 64

Slide 64 text

mutation PageLikeMutation( $page_id: String!, $analytics: [String!] ) { page_like(page_id: $page_id, analytics: $analytics) { page { id likers { count } } } }

Slide 65

Slide 65 text

+ (FBNetworkerRequest *)requestWithInput:(FBPageLikeInputData *)input withGraphQLQuery:(FBGraphQLRequest *)query { NSString *path = [NSString stringWithFormat:@"%@/likes", input.pageId]; FBGraphRequest *mutationRequest = [[FBGraphRequest alloc] initWithPath:path]; FBGraphBatchRequest *batchRequest = [[FBGraphBatchRequest alloc] initWithRequest:mutationRequest]; FBGraphQLRequest *queryRequest = [[FBGraphQLRequest alloc] initWithQuery:query]; [batchRequest addGraphRequest:queryRequest]; return batchRequest; } Auto-Generated Code Auto-Generated Code

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

1. SCALING MODELS 2. SCALING VIEWS 3. SCALING THE APP

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

Declarative!

Slide 71

Slide 71 text

Declarative…ish

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

https://wincent.com/blog/relay-modern

Slide 75

Slide 75 text

One of the big ideas was query colocation — the notion that you should be able to specify your data requirements for each view component inside the view itself and that the framework should transparently handle aggregation and efficient fetching. https://wincent.com/blog/relay-modern

Slide 76

Slide 76 text

Colocation!

Slide 77

Slide 77 text

Fragments for Code Reuse

Slide 78

Slide 78 text

Fragments as a Primitive

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

type User { name: String! profilePicture(size: Int = 50): ProfilePicture friends(first: Int): [User!]! events(first: Int): [Event!]! }

Slide 81

Slide 81 text

type User { name: String! profilePicture(size: Int = 50): ProfilePicture friends(first: Int): [User!]! events(first: Int): [Event!]! } type User = {| name: string, profilePicture: ?ProfilePicture, friends: Array, events: Array, |};

Slide 82

Slide 82 text

type User = {| name: string, profilePicture: ?ProfilePicture, friends: Array, events: Array, |}; type User { name: String! profilePicture(size: Int = 50): ProfilePicture friends(first: Int): [User!]! events(first: Int): [Event!]! }

Slide 83

Slide 83 text

query { me { profilePicture { uri } } }

Slide 84

Slide 84 text

type User = {| name: string, profilePicture: ?ProfilePicture, friends: Array, events: Array, |}; type User { name: String! profilePicture(size: Int = 50): ProfilePicture friends(first: Int): [User!]! events(first: Int): [Event!]! }

Slide 85

Slide 85 text

type User = {| name: ?string, profilePicture: ?ProfilePicture, friends: ?Array, events: ?Array, |}; type User { name: String! profilePicture(size: Int = 50): ProfilePicture friends(first: Int): [User!]! events(first: Int): [Event!]! }

Slide 86

Slide 86 text

query Timeline { me { displayName: name } } query Bookmarks { me { displayName: nickname } }

Slide 87

Slide 87 text

type User = {| displayName: ?string |}; query Timeline { me { displayName: name } } query Bookmarks { me { displayName: nickname } }

Slide 88

Slide 88 text

query ProfilePic { me { pic: profilePicUri } } query CoverPhoto { me { pic: coverPhoto { uri } } }

Slide 89

Slide 89 text

type User = {| pic: any |}; query ProfilePic { me { pic: profilePicUri } } query CoverPhoto { me { pic: coverPhoto { uri } } }

Slide 90

Slide 90 text

Type Models

Slide 91

Slide 91 text

Type Models

Slide 92

Slide 92 text

Fragments as a Primitive

Slide 93

Slide 93 text

fragment UserWithPic on User { profilePicture { uri } }

Slide 94

Slide 94 text

type UserWithPic = {| profilePicture: ?ProfilePicture |}; type ProfilePicture = {| uri: string |}; fragment UserWithPic on User { profilePicture { uri } }

Slide 95

Slide 95 text

Fragment Models

Slide 96

Slide 96 text

History of GraphQL July ’15 Open Source May ’17

Slide 97

Slide 97 text

Future of GraphQL Open Source

Slide 98

Slide 98 text

query { me { name nickname } }

Slide 99

Slide 99 text

query { me { name nickname # Only if in nickname test group } }

Slide 100

Slide 100 text

query { me { name nickname(if: $in_nickname_test) } }

Slide 101

Slide 101 text

query { me { name nickname @include(if: $in_nickname_test) } }

Slide 102

Slide 102 text

@future

Slide 103

Slide 103 text

Client-Driven Development

Slide 104

Slide 104 text

GraphQL: Client-Driven Development Dan Schafer Software Engineer, Facebook @dlschafer