Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Hi, I’m Brooks

Slide 3

Slide 3 text

I work at !

Slide 4

Slide 4 text

optimizing APIs for consumers…

Slide 5

Slide 5 text

with GraphQL

Slide 6

Slide 6 text

let’s start with APIs

Slide 7

Slide 7 text

REST APIs

Slide 8

Slide 8 text

http://nyc-restaurant-grades.com

Slide 9

Slide 9 text

http://nyc-restaurant-grades.com/api/v1/restaurants GET

Slide 10

Slide 10 text

!"""# verb http://nyc-restaurant-grades.com/api/v1/restaurants GET

Slide 11

Slide 11 text

!"""""""""""""""""""""""""""""""""""""""""""""""""""# endpoint !"""# verb http://nyc-restaurant-grades.com/api/v1/restaurants GET

Slide 12

Slide 12 text

http://nyc-restaurant-grades.com/api/v1/restaurants GET

Slide 13

Slide 13 text

[ { "id": 1, "name": "Garden Of Eat In", "grade": "A", "camis": "40394054", "address": "1416 Avenue J, Brooklyn, New York 11230", "cuisine": "Jewish/Kosher", "url": "http://nyc-restaurant-grades.com/api/v1/restaurants/1", "inspections_url": "http://nyc-restaurant-grades.com/api/v1/restaurants/1/inspections" }, { "id": 2, "name": "Hunan Delight", "grade": "A", "camis": "41188631", "address": "752 Union Street, Brooklyn, New York 11215", "cuisine": "Chinese", "url": "http://nyc-restaurant-grades.com/api/v1/restaurants/2", "inspections_url": "http://nyc-restaurant-grades.com/api/v1/restaurants/2/inspections" }, ... ] http://nyc-restaurant-grades.com/api/v1/restaurants GET

Slide 14

Slide 14 text

REST APIs return resources

Slide 15

Slide 15 text

enhance… !

Slide 16

Slide 16 text

[ { "id": 1, "name": "Garden Of Eat In", "grade": "A", "camis": "40394054", "address": "1416 Avenue J, Brooklyn, New York 11230", "cuisine": "Jewish/Kosher", "url": "http://nyc-restaurant-grades.com/api/v1/restaurants/1", "inspections_url": "http://nyc-restaurant-grades.com/api/v1/ restaurants/1/inspections" }, ... ] http://nyc-restaurant-grades.com/api/v1/restaurants GET

Slide 17

Slide 17 text

[ { "id": 1, "name": "Garden Of Eat In", "grade": "A", "camis": "40394054", "address": "1416 Avenue J, Brooklyn, New York 11230", "cuisine": "Jewish/Kosher", "url": "http://nyc-restaurant-grades.com/api/v1/restaurants/1", "inspections_url": "http://nyc-restaurant-grades.com/api/v1/ restaurants/1/inspections" }, ... ] http://nyc-restaurant-grades.com/api/v1/restaurants GET

Slide 18

Slide 18 text

[ { "id": 1, "name": "Garden Of Eat In", "grade": "A", "camis": "40394054", "address": "1416 Avenue J, Brooklyn, New York 11230", "cuisine": "Jewish/Kosher", "url": "http://nyc-restaurant-grades.com/api/v1/restaurants/1", "inspections_url": "http://nyc-restaurant-grades.com/api/v1/ restaurants/1/inspections" }, ... ] http://nyc-restaurant-grades.com/api/v1/restaurants GET

Slide 19

Slide 19 text

http://nyc-restaurant-grades.com/api/v1/restaurants/1/inspections GET

Slide 20

Slide 20 text

http://nyc-restaurant-grades.com/api/v1/restaurants/1/inspections GET

Slide 21

Slide 21 text

[ { "id": 1, "type": "Trans Fat / Initial Inspection", "score": 9, "grade": "A", "inspected_at": "2016-01-27T00:00:00.000Z", "graded_at": "2016-01-27T00:00:00.000Z", "url": "http://nyc-restaurant-grades.com/api/v1/restaurants/1/ inspections/1", "restaurant_url": "http://nyc-restaurant-grades.com/api/v1/ restaurants/1", "violations_url": "http://nyc-restaurant-grades.com/api/v1/ restaurants/1/inspections/1/violations" } ] http://nyc-restaurant-grades.com/api/v1/restaurants/1/inspections GET

Slide 22

Slide 22 text

[ { "id": 1, "type": "Trans Fat / Initial Inspection", "score": 9, "grade": "A", "inspected_at": "2016-01-27T00:00:00.000Z", "graded_at": "2016-01-27T00:00:00.000Z", "url": "http://nyc-restaurant-grades.com/api/v1/restaurants/1/ inspections/1", "restaurant_url": "http://nyc-restaurant-grades.com/api/v1/ restaurants/1", "violations_url": "http://nyc-restaurant-grades.com/api/v1/ restaurants/1/inspections/1/violations" } ] http://nyc-restaurant-grades.com/api/v1/restaurants/1/inspections GET

Slide 23

Slide 23 text

http://nyc-restaurant-grades.com/api/v1/restaurants/1/inspections/1/violations GET

Slide 24

Slide 24 text

http://nyc-restaurant-grades.com/api/v1/restaurants/1/inspections/1/violations GET

Slide 25

Slide 25 text

http://nyc-restaurant-grades.com/api/v1/restaurants/1/inspections/1/violations GET [ { "id": 62855, "description": "Food not protected from potential source of contamination during storage, preparation, transportation, display or service.", "code": "06C", "url": "http://nyc-restaurant-grades.com/api/v1/ restaurants/1/inspections/29572/violations/62855", "inspection_url": "http://nyc-restaurant-grades.com/api/v1/ restaurants/1/inspections/29572", "restaurant_url": "http://nyc-restaurant-grades.com/api/v1/ restaurants/1" } ]

Slide 26

Slide 26 text

API Server /restaurants /inspections /violations

Slide 27

Slide 27 text

API Server /restaurants /inspections /violations

Slide 28

Slide 28 text

API Server /restaurants /inspections /violations

Slide 29

Slide 29 text

API Server /restaurants /inspections /violations

Slide 30

Slide 30 text

API Server /restaurants /inspections /violations

Slide 31

Slide 31 text

"RESTful APIs are optimized for servers, not clients." - Mark Twain (I think)

Slide 32

Slide 32 text

what if we wanted to put clients first?

Slide 33

Slide 33 text

backends for frontends

Slide 34

Slide 34 text

API Server /desktop /mobile /fridge

Slide 35

Slide 35 text

API Server /desktop /mobile /fridge

Slide 36

Slide 36 text

but this only works if you’re in control of the client

Slide 37

Slide 37 text

what about a public API?

Slide 38

Slide 38 text

enter GraphQL

Slide 39

Slide 39 text

a data query language

Slide 40

Slide 40 text

think SQL

Slide 41

Slide 41 text

not Neo4j

Slide 42

Slide 42 text

GraphQL is a specification

Slide 43

Slide 43 text

{ me { firstName lastName email } }

Slide 44

Slide 44 text

{ me { firstName lastName email } }

Slide 45

Slide 45 text

{ me { firstName lastName email } }

Slide 46

Slide 46 text

{ me { firstName lastName email } } { "data":{ "me":{ "firstName":"Brooks", "lastName":"Swinnerton", "email":"[email protected]" } } }

Slide 47

Slide 47 text

https://nyc-restaurant-grades.com/graphql POST

Slide 48

Slide 48 text

!"""# verb https://nyc-restaurant-grades.com/graphql POST

Slide 49

Slide 49 text

!"""# verb !""""""""""""""""""""""""""""""""""""""""# endpoint https://nyc-restaurant-grades.com/graphql POST

Slide 50

Slide 50 text

https://nyc-restaurant-grades.com/graphql POST

Slide 51

Slide 51 text

{ restaurant(name: "Cafe Ghia") { name cuisine } } https://nyc-restaurant-grades.com/graphql POST

Slide 52

Slide 52 text

{ restaurant(name: "Cafe Ghia") { name cuisine } } https://nyc-restaurant-grades.com/graphql POST

Slide 53

Slide 53 text

{ restaurant(name: "Cafe Ghia") { name cuisine } } { "data": { "restaurant": { "name": "Cafe Ghia", "cuisine": "American" } } } https://nyc-restaurant-grades.com/graphql POST

Slide 54

Slide 54 text

features of the query language

Slide 55

Slide 55 text

GraphQL is typed

Slide 56

Slide 56 text

{ me { firstName lastName email } } type RootQuery { me: User } type User { firstName: String lastName: String email: String }

Slide 57

Slide 57 text

{ me { firstName lastName email } } type RootQuery { me: User } type User { firstName: String lastName: String email: String }

Slide 58

Slide 58 text

{ me { firstName lastName email } } type RootQuery { me: User } type User { firstName: String lastName: String email: String }

Slide 59

Slide 59 text

type RootQuery { restaurant(name: String): Restaurant } type Restaurant { name: String! cuisine: String } { restaurant(name: "Cafe Ghia") { name cuisine } }

Slide 60

Slide 60 text

type RootQuery { restaurant(name: String): Restaurant } type Restaurant { name: String! cuisine: String } { restaurant(name: "Cafe Ghia") { name cuisine } }

Slide 61

Slide 61 text

type RootQuery { restaurant(name: String): Restaurant } type Restaurant { name: String! cuisine: String } { restaurant(name: "Cafe Ghia") { name cuisine } }

Slide 62

Slide 62 text

type RootQuery { restaurant(name: String): Restaurant } type Restaurant { name: String! cuisine: String } { restaurant(name: "Cafe Ghia") { name cuisine } }

Slide 63

Slide 63 text

{ restaurants(borough: BROOKLYN) { name cuisine } }

Slide 64

Slide 64 text

{ restaurants(borough: BROOKLYN) { name cuisine } }

Slide 65

Slide 65 text

{ restaurants(borough: BROOKLYN) { name cuisine } }

Slide 66

Slide 66 text

type RootQuery { restaurants(borough: RestaurantBorough): [Restaurant]! } enum RestaurantBorough { BRONX BROOKLYN MANHATTAN STATEN_ISLAND QUEENS } type Restaurant { name: String! cuisine: String }

Slide 67

Slide 67 text

type RootQuery { restaurants(borough: RestaurantBorough): [Restaurant]! } enum RestaurantBorough { BRONX BROOKLYN MANHATTAN STATEN_ISLAND QUEENS } type Restaurant { name: String! cuisine: String }

Slide 68

Slide 68 text

type RootQuery { restaurants(borough: RestaurantBorough): [Restaurant]! } enum RestaurantBorough { BRONX BROOKLYN MANHATTAN STATEN_ISLAND QUEENS } type Restaurant { name: String! cuisine: String }

Slide 69

Slide 69 text

{ restaurant(name: "Mcdonalds") { name cuisine } restaurant(name: "Wendy's") { name cuisine } }

Slide 70

Slide 70 text

{ restaurant(name: "Mcdonalds") { name cuisine } restaurant(name: "Wendy's") { name cuisine } } { "data": { "restaurant": { "name": "Mcdonalds", "cuisine": "American" }, "restaurant": { "name": "Wendy's", "cuisine": "American" } } }

Slide 71

Slide 71 text

{ restaurant(name: "Mcdonalds") { name cuisine } restaurant(name: "Wendy's") { name cuisine } } { "data": { "restaurant": { "name": "Mcdonalds", "cuisine": "American" }, "restaurant": { "name": "Wendy's", "cuisine": "American" } } }

Slide 72

Slide 72 text

{ mcdonalds: restaurant(name: "McDonalds") { name cuisine } wendys: restaurant(name: "Wendy's") { name cuisine } }

Slide 73

Slide 73 text

{ "data": { "mcdonalds": { "name": "Mcdonalds", "cuisine": "American" }, "wendys": { "name": "Wendy's", "cuisine": "American" } } }

Slide 74

Slide 74 text

{ mcdonalds: restaurant(name: "McDonalds") { name cuisine } wendys: restaurant(name: "Wendy's") { name cuisine } }

Slide 75

Slide 75 text

{ mcdonalds: restaurant(name: "McDonalds") { ...RestaurantInfo } wendys: restaurant(name: "Wendy's") { ...RestaurantInfo } } fragment RestaurantInfo on Restaurant { name cuisine }

Slide 76

Slide 76 text

GraphQL is introspectable

Slide 77

Slide 77 text

documentation and client generation, are free

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

multiple resources in one round trip

Slide 80

Slide 80 text

API Server /restaurants /inspections /violations

Slide 81

Slide 81 text

API Server /restaurants /inspections /violations

Slide 82

Slide 82 text

API Server /restaurants /inspections /violations

Slide 83

Slide 83 text

API Server /restaurants /inspections /violations

Slide 84

Slide 84 text

API Server /desktop /mobile /fridge

Slide 85

Slide 85 text

API Server /desktop /mobile /fridge

Slide 86

Slide 86 text

API Server /graphql

Slide 87

Slide 87 text

API Server /graphql

Slide 88

Slide 88 text

Implementing a GraphQL server

Slide 89

Slide 89 text

https://nyc-restaurant-grades.com/graphql POST

Slide 90

Slide 90 text

class GraphqlController < ApplicationController def create result = Graph::Schema.execute(params[:query]) render json: result end end app/controllers/graphql_controller.rb

Slide 91

Slide 91 text

class GraphqlController < ApplicationController def create result = Graph::Schema.execute(params[:query]) render json: result end end app/controllers/graphql_controller.rb

Slide 92

Slide 92 text

class GraphqlController < ApplicationController def create result = Graph::Schema.execute(params[:query]) render json: result end end app/controllers/graphql_controller.rb

Slide 93

Slide 93 text

like SQL, there are many implementations

Slide 94

Slide 94 text

let’s talk about the Ruby implementation

Slide 95

Slide 95 text

rmosolgo/graphql-ruby

Slide 96

Slide 96 text

{ restaurant(name: "Cafe Ghia") { name cuisine } }

Slide 97

Slide 97 text

{ restaurant(name: "Cafe Ghia") { name cuisine } } type RootQuery { restaurant(name: String): Restaurant } type Restaurant { name: String! cuisine: String }

Slide 98

Slide 98 text

type RootQuery { restaurant(name: String): Restaurant } ?

Slide 99

Slide 99 text

? type RootQuery { restaurant(name: String): Restaurant }

Slide 100

Slide 100 text

type RootQuery { restaurant(name: String): Restaurant } module Graph::Types RootQuery = GraphQL::ObjectType.define do name 'RootQuery' end end

Slide 101

Slide 101 text

# The query root. type RootQuery { restaurant(name: String): Restaurant } module Graph::Types RootQuery = GraphQL::ObjectType.define do name 'RootQuery' description 'The query root.' end end

Slide 102

Slide 102 text

type RootQuery { restaurant(name: String): Restaurant } ?

Slide 103

Slide 103 text

module Graph::Types RootQuery = GraphQL::ObjectType.define do name 'RootQuery' field :restaurant do argument :name, types.String type Graph::Types::Restaurant resolve -> (object, arguments, context) do ::Restaurant.find_by(name: arguments['name']) end end end end type RootQuery { restaurant(name: String): Restaurant }

Slide 104

Slide 104 text

module Graph::Types RootQuery = GraphQL::ObjectType.define do name 'RootQuery' field :restaurant do argument :name, types.String type Graph::Types::Restaurant resolve -> (object, arguments, context) do ::Restaurant.find_by(name: arguments['name']) end end end end type RootQuery { restaurant(name: String): Restaurant }

Slide 105

Slide 105 text

module Graph::Types RootQuery = GraphQL::ObjectType.define do name 'RootQuery' field :restaurant do argument :name, types.String type Graph::Types::Restaurant resolve -> (object, arguments, context) do ::Restaurant.find_by(name: arguments['name']) end end end end type RootQuery { restaurant(name: String): Restaurant }

Slide 106

Slide 106 text

module Graph::Types RootQuery = GraphQL::ObjectType.define do name 'RootQuery' field :restaurant do argument :name, types.String type Graph::Types::Restaurant resolve -> (object, arguments, context) do ::Restaurant.find_by(name: arguments['name']) end end end end type RootQuery { restaurant(name: String): Restaurant }

Slide 107

Slide 107 text

type Restaurant { name: String! cuisine: String } ?

Slide 108

Slide 108 text

type Restaurant { name: String! cuisine: String } module Graph::Types Restaurant = GraphQL::ObjectType.define do name 'Restaurant' end end

Slide 109

Slide 109 text

# A place of business serving food. type Restaurant { name: String cuisine: String } module Graph::Types Restaurant = GraphQL::ObjectType.define do name 'Restaurant' description 'A place of business serving food.' end end

Slide 110

Slide 110 text

type Restaurant { name: String cuisine: String } ?

Slide 111

Slide 111 text

type Restaurant { name: String cuisine: String } module Graph::Types Restaurant = GraphQL::ObjectType.define do name 'Restaurant' field :cuisine do type types.String resolve -> (restaurant, arguments, context) do restaurant.cuisine end end end end

Slide 112

Slide 112 text

type Restaurant { name: String cuisine: String } module Graph::Types Restaurant = GraphQL::ObjectType.define do name 'Restaurant' field :cuisine do type types.String resolve -> (restaurant, arguments, context) do restaurant.cuisine end end end end

Slide 113

Slide 113 text

type Restaurant { name: String cuisine: String } module Graph::Types Restaurant = GraphQL::ObjectType.define do name 'Restaurant' field :cuisine do type types.String resolve -> (restaurant, arguments, context) do restaurant.cuisine end end end end

Slide 114

Slide 114 text

GraphQL schemas have resolvers

Slide 115

Slide 115 text

data can be resolved from ✨anywhere ✨

Slide 116

Slide 116 text

Which makes GraphQL servers…

Slide 117

Slide 117 text

a facade.

Slide 118

Slide 118 text

a facade in front of databases

Slide 119

Slide 119 text

a facade in front of caches

Slide 120

Slide 120 text

a facade in front of services

Slide 121

Slide 121 text

you use your existing application code

Slide 122

Slide 122 text

GraphQL at GitHub

Slide 123

Slide 123 text

the history of GitHub’s API

Slide 124

Slide 124 text

!"""# verb !"""""""""""""""""""""""# endpoint https://api.github.com GET

Slide 125

Slide 125 text

v4?

Slide 126

Slide 126 text

March 20th, 2016 proposal submitted

Slide 127

Slide 127 text

April 6th, 2016 proof of concept done

Slide 128

Slide 128 text

April 12th, 2016 New team created

Slide 129

Slide 129 text

September 14th, 2016 early access

Slide 130

Slide 130 text

Today >100 million queries/day

Slide 131

Slide 131 text

we use GraphQL both…

Slide 132

Slide 132 text

externally

Slide 133

Slide 133 text

!"""# verb !""""""""""""""""""""""""""""""# endpoint https://api.github.com/graphql POST

Slide 134

Slide 134 text

and internally

Slide 135

Slide 135 text

github/graphql-client

Slide 136

Slide 136 text

exchange a query for Ruby objects

Slide 137

Slide 137 text

collocate our queries in our views

Slide 138

Slide 138 text

tooling

Slide 139

Slide 139 text

gjtorikian/graphql-docs

Slide 140

Slide 140 text

https://github.com/gjtorikian/graphql-docs

Slide 141

Slide 141 text

schema driven development

Slide 142

Slide 142 text

with our REST API

Slide 143

Slide 143 text

new features were developed for the UI

Slide 144

Slide 144 text

then staff shipped

Slide 145

Slide 145 text

then released

Slide 146

Slide 146 text

REST API work started after the release

Slide 147

Slide 147 text

but today…

Slide 148

Slide 148 text

all new features are built with GraphQL

Slide 149

Slide 149 text

from the start

Slide 150

Slide 150 text

this allows us to build a true public API

Slide 151

Slide 151 text

this allows us to build a true public API

Slide 152

Slide 152 text

this allows us to build a true platform

Slide 153

Slide 153 text

shared between GitHubbers and integrators

Slide 154

Slide 154 text

what if we wanted to put clients first?

Slide 155

Slide 155 text

thank you @bswinnerton

Slide 156

Slide 156 text

More information

Slide 157

Slide 157 text

http://nyc-restaurant-grades.com @bswinnerton

Slide 158

Slide 158 text

http://graphql.org @bswinnerton