Slide 1

Slide 1 text

Facebook’s GraphQL Stack Dan Schafer

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

{ me { name } }

Slide 4

Slide 4 text

{ "me": { "name": "Daniel Schafer" } } { me { name } }

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

{ "me": { "name": "Daniel Schafer", "profilePicture": { "width": 50, "height": 50, "url": "https://cdn/50.jpg" } } } { me { name profilePicture { width height url } } }

Slide 7

Slide 7 text

query Q { me { name birthday teammates { name birthday } } }

Slide 8

Slide 8 text

query Q { me { name birthday teammates { name birthday } } } { "me": { "name": "Daniel Schafer", "birthday": "Jan 17", "teammates": [ { "name": "Lee Byron", "birthday": "Dec 18" }, { "name": "Laney Kuenzel", "birthday": "Jan 25" } ] } }

Slide 9

Slide 9 text

query Q { me { ...BasicInfo teammates { ...BasicInfo } } } fragment BasicInfo on User { name birthday } { "me": { "name": "Daniel Schafer", "birthday": "Jan 17", "teammates": [ { "name": "Lee Byron", "birthday": "Dec 18" }, { "name": "Laney Kuenzel", "birthday": "Jan 25" } ] } }

Slide 10

Slide 10 text

graphql.org

Slide 11

Slide 11 text

GraphQL: How?

Slide 12

Slide 12 text

How do I implement authorization? How do I make GraphQL efficient? How do I cache my results?

Slide 13

Slide 13 text

How do I implement authorization? How do I make GraphQL efficient? How do I cache my results?

Slide 14

Slide 14 text

How do I implement authorization? How do I make GraphQL efficient? How do I cache my results?

Slide 15

Slide 15 text

Facebook’s GraphQL Stack

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Think Graphs, not Endpoints

Slide 18

Slide 18 text

Single Source of Truth

Slide 19

Slide 19 text

Thin API layer

Slide 20

Slide 20 text

Authorization

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

“A Todo Item can only be seen by its creator.”

Slide 23

Slide 23 text

What is a Todo Item?

Slide 24

Slide 24 text

http://api.todoapp.com/todo/4

Slide 25

Slide 25 text

http://api.todoapp.com/todo/4 Too interface-specific

Slide 26

Slide 26 text

SELECT * FROM todoitems WHERE id = 4

Slide 27

Slide 27 text

SELECT * FROM todoitems WHERE id = 4 GET todoitem:4

Slide 28

Slide 28 text

SELECT * FROM todoitems WHERE id = 4 Storage Detail GET todoitem:4

Slide 29

Slide 29 text

External Interfaces Storage Layer

Slide 30

Slide 30 text

External Interfaces Business Logic Storage Layer

Slide 31

Slide 31 text

External Interfaces Business Logic Storage Layer

Slide 32

Slide 32 text

GraphQL Business Logic Storage Layer

Slide 33

Slide 33 text

What is a Todo Item?

Slide 34

Slide 34 text

class TodoItem { constructor(id: number, title: string, isDone: boolean) { this.id = id; this.title = title; this.isDone = isDone; } };

Slide 35

Slide 35 text

http://sujaytrivedi.blogspot.com/2015/03/object-oriented-programming-oop-in-c.html

Slide 36

Slide 36 text

https://upload.wikimedia.org/wikipedia/commons/b/b8/Policy_Admin_Component_Diagram.PNG

Slide 37

Slide 37 text

class TodoItem { constructor(id: number, title: string, isDone: boolean) { this.id = id; this.title = title; this.isDone = isDone; } };

Slide 38

Slide 38 text

Single Source of Truth

Slide 39

Slide 39 text

class TodoItem { constructor(data: TodoItemData) { this.id = data.id; this.title = data.title; this.isDone = data.isDone; } // Single source of truth for fetching static async gen(id: number): Promise { const data = await Redis.get("ti:" + id); // Nullable return data ? new TodoItem(data) : null; } }

Slide 40

Slide 40 text

Implementing Authorization

Slide 41

Slide 41 text

class TodoItem { // Single source of truth for fetching static async gen(id: number): Promise { const data = await Redis.get("ti:" + id); // Nullable return data ? new TodoItem(data) : null; } }

Slide 42

Slide 42 text

class TodoItem { // Single source of truth for fetching static async gen(id: number): Promise { const data = await Redis.get("ti:" + id); // Nullable // Single source of truth for authorization return data ? new TodoItem(data) : null; } }

Slide 43

Slide 43 text

class TodoItem { // Single source of truth for fetching static async gen(id: number): Promise { const data = await Redis.get("ti:" + id); // Nullable if (data === null) return null; const canSee = checkCanSee(data); return canSee ? new TodoItem(data) : null; } }

Slide 44

Slide 44 text

class TodoItem { // Single source of truth for fetching static async gen(id: number): Promise { const data = await Redis.get("ti:" + id); // Nullable if (data === null) return null; const canSee = checkCanSee(data); return canSee ? new TodoItem(data) : null; } } function checkCanSee(data: Object) { // A Todo Item can only be seen by its creator }

Slide 45

Slide 45 text

class TodoItem { // Single source of truth for fetching static async gen(id: number): Promise { const data = await Redis.get("ti:" + id); // Nullable if (data === null) return null; const canSee = checkCanSee(data); return canSee ? new TodoItem(data) : null; } } function checkCanSee(data: Object) { return (data.creatorID === /* authenticated user ID */); }

Slide 46

Slide 46 text

class TodoItem { // Single source of truth for fetching static async gen(id: number): Promise { const data = await Redis.get("ti:" + id); // Nullable if (data === null) return null; const canSee = checkCanSee(data); return canSee ? new TodoItem(data) : null; } } function checkCanSee(data: Object) { return (data.creatorID === /* authenticated user ID */); }

Slide 47

Slide 47 text

class TodoItem { // Single source of truth for fetching static async gen( viewer: Viewer, id: number): Promise { const data = await Redis.get("ti:" + id); // Nullable if (data === null) return null; const canSee = checkCanSee(data); return canSee ? new TodoItem(data) : null; } } function checkCanSee(data: Object) { return (data.creatorID === /* authenticated user ID */); }

Slide 48

Slide 48 text

class TodoItem { // Single source of truth for fetching static async gen( viewer: Viewer, id: number): Promise { const data = await Redis.get("ti:" + id); // Nullable if (data === null) return null; const canSee = checkCanSee(viewer, data); return canSee ? new TodoItem(data) : null; } } function checkCanSee(viewer: Viewer, data: Object) { return (data.creatorID === viewer.userID); }

Slide 49

Slide 49 text

class TodoItem { static async gen( viewer: Viewer, id: number): Promise; // No other public constructors } class TodoList { static async gen( viewer: Viewer, id: number): Promise; // No other public constructors }

Slide 50

Slide 50 text

Authorization in GraphQL

Slide 51

Slide 51 text

todoItem: { type: TodoItemType, resolve: (obj) => /* ??? */ }

Slide 52

Slide 52 text

todoItem: { type: TodoItemType, resolve: (obj) => /* ??? */ } // ... graphql(schema, "{todoItem}");

Slide 53

Slide 53 text

todoItem: { type: TodoItemType, resolve: (obj) => TodoItem.gen(viewer, id) } // ... graphql(schema, "{todoItem}");

Slide 54

Slide 54 text

todoItem: { type: TodoItemType, args: { id: { type: GraphQLID } }, resolve: (obj, {id}) => TodoItem.gen(viewer, id) } // ... graphql(schema, "{todoItem(id: 4)}");

Slide 55

Slide 55 text

todoItem: { type: TodoItemType, args: { id: { type: GraphQLID } }, resolve: (obj, {id}, viewer) => TodoItem.gen(viewer, id) } // ... graphql(schema, "{todoItem(id: $id)}", viewer);

Slide 56

Slide 56 text

todoItem: { type: TodoItemType, args: { id: { type: GraphQLID } }, resolve: (obj, {id}, viewer) => TodoItem.gen(viewer, id) } // ... graphql(schema, "{todoItem(id: $id)}", viewer);

Slide 57

Slide 57 text

todoItem: { type: TodoItemType, args: { id: { type: GraphQLID } }, resolve: (obj, {id}, viewer) => TodoItem.gen(viewer, id) } // ... const viewer = Viewer.fromAuthToken(request.auth_token); graphql(schema, "{todoItem(id: $id)}", viewer);

Slide 58

Slide 58 text

Efficiency

Slide 59

Slide 59 text

{ me { name bestFriend { name } friends(first: 5) { name bestFriend { name } } } }

Slide 60

Slide 60 text

{ me { name bestFriend { name } friends(first: 5) { name bestFriend { name } } } } GET u:1 GET u:2 LRANGE friends:1 0 5 GET u:3 GET u:4 GET u:5 GET u:6 GET u:7 GET u:8 GET u:9 GET u:10 GET u:11 GET u:12 redis.log

Slide 61

Slide 61 text

{ me { name bestFriend { name } friends(first: 5) { name bestFriend { name } } } } GET u:1 GET u:2 LRANGE friends:1 0 5 GET u:3 GET u:4 GET u:5 GET u:6 GET u:7 GET u:8 GET u:9 GET u:10 GET u:11 GET u:12 redis.log

Slide 62

Slide 62 text

{ me { name bestFriend { name } friends(first: 5) { name bestFriend { name } } } } GET u:1 GET u:2 LRANGE friends:1 0 5 GET u:3 GET u:4 GET u:5 GET u:6 GET u:7 GET u:8 GET u:9 GET u:10 GET u:11 GET u:12 redis.log

Slide 63

Slide 63 text

{ me { name bestFriend { name } friends(first: 5) { name bestFriend { name } } } } GET u:1 GET u:2 LRANGE friends:1 0 5 GET u:3 GET u:4 GET u:5 GET u:6 GET u:7 GET u:8 GET u:9 GET u:10 GET u:11 GET u:12 redis.log

Slide 64

Slide 64 text

{ me { name bestFriend { name } friends(first: 5) { name bestFriend { name } } } } GET u:1 GET u:2 LRANGE friends:1 0 5 GET u:2 GET u:3 GET u:4 GET u:5 GET u:6 GET u:8 GET u:9 GET u:10 GET u:11 GET u:12 redis.log

Slide 65

Slide 65 text

{ me { name bestFriend { name } friends(first: 5) { name bestFriend { name } } } } GET u:1 GET u:2 LRANGE friends:1 0 5 GET u:2 GET u:3 GET u:4 GET u:5 GET u:6 GET u:1 GET u:5 GET u:7 GET u:8 GET u:9 redis.log

Slide 66

Slide 66 text

{ me { name bestFriend { name } friends(first: 5) { name bestFriend { name } } } } GET u:1 GET u:2 LRANGE friends:1 0 -1 GET u:3 GET u:4 GET u:5 GET u:6 GET u:7 GET u:8 GET u:9 GET u:10 GET u:11 GET u:12 redis.log

Slide 67

Slide 67 text

{ me { name bestFriend { name } friends(first: 5) { name bestFriend { name } } } } GET u:1 GET u:2 LRANGE friends:1 0 5 GET u:3 GET u:4 GET u:5 GET u:6 GET u:7 redis.log

Slide 68

Slide 68 text

{ me { name bestFriend { name } friends(first: 5) { name bestFriend { name } } } } GET u:1 GET u:2 LRANGE friends:1 0 5 MGET u:2 u:3 u:4 u:5 u:6 MGET u:8 u:9 u:10 u:11 u:12 redis.log

Slide 69

Slide 69 text

{ me { name bestFriend { name } friends(first: 5) { name bestFriend { name } } } } GET u:1 GET u:2 LRANGE friends:1 0 5 MGET u:2 u:3 u:4 u:5 u:6 MGET u:1 u:5 u:7 u:8 u:9 redis.log

Slide 70

Slide 70 text

Implementing Batching in GraphQL

Slide 71

Slide 71 text

Implementing Batching in GraphQL

Slide 72

Slide 72 text

DataLoader

Slide 73

Slide 73 text

https://github.com/facebook/dataloader

Slide 74

Slide 74 text

const userDataLoader = new DataLoader( ids => Redis.mget(ids.map(id => "m:" + id)) ); class User { static async gen(v: Viewer, id: number): Promise { const rawData = await Redis.get("u:" + id); // Do authorization ... return rawData === null ? new TodoItem(rawData) : null; } }

Slide 75

Slide 75 text

const userDataLoader = new DataLoader( ids => Redis.mget(ids.map(id => "m:" + id)) ); class User { static async gen(v: Viewer, id: number): Promise { const rawData = await Redis.get("u:" + id); // Do authorization ... return rawData === null ? new TodoItem(rawData) : null; } }

Slide 76

Slide 76 text

const userDataLoader = new DataLoader( ids => Redis.mget(ids.map(id => "u:" + id)) ); class User { static async gen(v: Viewer, id: number): Promise { const rawData = await Redis.get("u:" + id); // Do authorization ... return rawData === null ? new TodoItem(rawData) : null; } }

Slide 77

Slide 77 text

const userDataLoader = new DataLoader( ids => Redis.mget(ids.map(id => "u:" + id)) ); class User { static async gen(v: Viewer, id: number): Promise { const rawData = await userDataLoader.load(id); // Do authorization ... return rawData === null ? new TodoItem(rawData) : null; } }

Slide 78

Slide 78 text

{ me { name bestFriend { name } friends(first: 5) { name bestFriend { name } } } } GET u:1 GET u:2 LRANGE friends:1 0 5 MGET u:2 u:3 u:4 u:5 u:6 MGET u:1 u:5 u:7 u:8 u:9 redis.log

Slide 79

Slide 79 text

{ me { name bestFriend { name } friends(first: 5) { name bestFriend { name } } } } GET u:1 GET u:2 LRANGE friends:1 0 5 MGET u:2 u:3 u:4 u:5 u:6 MGET u:1 u:5 u:7 u:8 u:9 redis.log

Slide 80

Slide 80 text

Caching

Slide 81

Slide 81 text

const userDataLoader = new DataLoader( ids => Redis.mget(ids.map(id => "u:" + id)) ); class User { static async gen(v: Viewer, id: number): Promise { const rawData = await userDataLoader.load(id); // Do authorization ... return rawData === null ? new TodoItem(rawData) : null; } }

Slide 82

Slide 82 text

const userDataLoader = new DataLoader( ids => Redis.mget(ids.map(id => "u:" + id)) ); class User { static async gen(v: Viewer, id: number): Promise { const rawData = await userDataLoader.load(id); // Do authorization ... return rawData === null ? new TodoItem(rawData) : null; } }

Slide 83

Slide 83 text

{ me { name bestFriend { name } friends(first: 5) { name bestFriend { name } } } } GET u:1 GET u:2 LRANGE friends:1 0 5 MGET u:2 u:3 u:4 u:5 u:6 MGET u:1 u:5 u:7 u:8 u:9 redis.log

Slide 84

Slide 84 text

{ me { name bestFriend { name } friends(first: 5) { name bestFriend { name } } } } GET u:1 GET u:2 LRANGE friends:1 0 5 MGET u:2 u:3 u:4 u:5 u:6 MGET u:1 u:5 u:7 u:8 u:9 redis.log

Slide 85

Slide 85 text

Clients?

Slide 86

Slide 86 text

const userDataLoader = new DataLoader( ids => Redis.mget(ids.map(id => "m:" + id)) ); class User { static async gen(v: Viewer, id: number): Promise { const rawData = await userDataLoader.load(id); // Do authorization ... return rawData === null ? new TodoItem(rawData) : null; } getID(): number { return this.id; } }

Slide 87

Slide 87 text

const userDataLoader = new DataLoader( ids => Redis.mget(ids.map(id => "m:" + id)) ); class User { static async gen(v: Viewer, id: number): Promise { const rawData = await userDataLoader.load(id); // Do authorization ... return rawData === null ? new TodoItem(rawData) : null; } getID(): number { return this.id; } }

Slide 88

Slide 88 text

const userType = new GraphQLObjectType({ name: 'User', fields: () => ({ id: { type: GraphQLID, resolve: (user) => user.getID() } }) });

Slide 89

Slide 89 text

{ me { id profilePhoto { id } } }

Slide 90

Slide 90 text

{ me { id profilePhoto { id } } } { me: { id: "1", profilePhoto: { id: "1" } } }

Slide 91

Slide 91 text

What do we want in IDs?

Slide 92

Slide 92 text

Unique Cache Key

Slide 93

Slide 93 text

Globally Unique Cache Key

Slide 94

Slide 94 text

Globally Unique Cache Key Refetch Identifier

Slide 95

Slide 95 text

Globally Unique Cache Key Refetch Identifier Client Comprehension?

Slide 96

Slide 96 text

Globally Unique Cache Key Refetch Identifier Client Comprehension?

Slide 97

Slide 97 text

Globally Unique Cache Key Refetch Identifier Opaque to Clients

Slide 98

Slide 98 text

const userType = new GraphQLObjectType({ name: 'User', fields: () => ({ id: { type: GraphQLID, resolve: (user) => user.getID() } }) });

Slide 99

Slide 99 text

const userType = new GraphQLObjectType({ name: 'User', fields: () => ({ id: { type: GraphQLID, resolve: (user) => base64("user:" + user.getID()) } }) });

Slide 100

Slide 100 text

{ me { id profilePhoto { id } } }

Slide 101

Slide 101 text

{ me { id profilePhoto { id } } } { me: { id: "dXNlcjoxCg==", profilePhoto: { id: "cGhvdG86MQo=" } } }

Slide 102

Slide 102 text

Facebook’s GraphQL Stack

Slide 103

Slide 103 text

Think Graphs, not Endpoints

Slide 104

Slide 104 text

Single Source of Truth

Slide 105

Slide 105 text

Thin API layer

Slide 106

Slide 106 text

graphql.org