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

let’s talk about GraphQL & Relay

Slide 5

Slide 5 text

in Ruby

Slide 6

Slide 6 text

but first

Slide 7

Slide 7 text

let’s start with APIs

Slide 8

Slide 8 text

REST APIs

Slide 9

Slide 9 text

are endpoint driven

Slide 10

Slide 10 text

are verb driven

Slide 11

Slide 11 text

!"""# verb !""""""""""""""""""""""""""""""""""""""""""""# endpoint https://api.reddit.com/r/catsstandingup.json GET

Slide 12

Slide 12 text

{ "kind": "Listing", "data": { "modhash": "", "children": [ { "kind": "t3", "data": { "id": "4lyzb1", "created": 1464776494, "title": "Cat.", "author": "AcezennJames", "score": 1, "num_comments": 0, "subreddit": "CatsStandingUp", "url": "https://i.reddituploads.com/da4620368fdd4c08a17e3b2f56571889? fit=max&h=1536&w=1536&s=a7a58b1ca15529fa6dda138d12d6fb51", "permalink": "/r/CatsStandingUp/comments/4lyzb1/cat/", "thumbnail": "http://b.thumbs.redditmedia.com/uT4-uPd2Z7Jt-gVdJSswq8W5bTa9Jchfnb3GHRlhHLE.jpg", "locked": false } } ] } }

Slide 13

Slide 13 text

but, we’re Rubyists…

Slide 14

Slide 14 text

require 'net/http' require 'json' uri = URI('https://api.reddit.com/r/catsstandingup.json') response = Net::HTTP.get(uri) parsed_response = JSON.parse(response) posts = parsed_response['data']['children'] html = posts.each_with_object('') do |post, string| thumbnail = post['data']['thumbnail'] string << "" end File.write('cats_standing_up.html', html)

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

REST APIs

Slide 17

Slide 17 text

…for backend engineers

Slide 18

Slide 18 text

are difficult to change

Slide 19

Slide 19 text

…for implementors

Slide 20

Slide 20 text

aren’t optimized for consumption

Slide 21

Slide 21 text

{ "kind": "Listing", "data": { "modhash": "", "children": [ { "kind": "t3", "data": { "id": "4lyzb1", "created": 1464776494, "title": "Cat.", "author": "AcezennJames", "score": 1, "num_comments": 0, "subreddit": "CatsStandingUp", "url": "https://i.reddituploads.com/da4620368fdd4c08a17e3b2f56571889? fit=max&h=1536&w=1536&s=a7a58b1ca15529fa6dda138d12d6fb51", "permalink": "/r/CatsStandingUp/comments/4lyzb1/cat/", "thumbnail": "http://b.thumbs.redditmedia.com/uT4-uPd2Z7Jt-gVdJSswq8W5bTa9Jchfnb3GHRlhHLE.jpg", "locked": false } } ] } }

Slide 22

Slide 22 text

enter, GraphQL

Slide 23

Slide 23 text

What is it?

Slide 24

Slide 24 text

a Data Query Language

Slide 25

Slide 25 text

think SQL

Slide 26

Slide 26 text

not Neo4j

Slide 27

Slide 27 text

GraphQL is a specification

Slide 28

Slide 28 text

created by Facebook

Slide 29

Slide 29 text

powering all of their mobile apps

Slide 30

Slide 30 text

what does it look like?

Slide 31

Slide 31 text

{ me { firstName lastName email } }

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

{ restaurant(name: "Cafe Ghia") { name buildingNumber street zipcode borough grade } } { "data": { "restaurant": { "name": "Cafe Ghia", "buildingNumber": "24", "street": "Irving Avenue", "zipcode": "11237", "borough": "BROOKLYN", "grade": "A" } } } https://nyc-restaurant-grades.com/graphql POST

Slide 35

Slide 35 text

how do you know what you can query?

Slide 36

Slide 36 text

GraphQL is introspectable

Slide 37

Slide 37 text

GraphQL is typed

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

documentation and client generation, are free

Slide 40

Slide 40 text

THIS IS THE HOLY GRAIL

Slide 41

Slide 41 text

Building a GraphQL server

Slide 42

Slide 42 text

like SQL, there are many implementations

Slide 43

Slide 43 text

let’s talk about the Ruby implementation

Slide 44

Slide 44 text

ActiveRecord::Schema.define(version: 20160605211006) do create_table "inspections"do |t| t.integer "restaurant_id" t.text "type" t.datetime "inspected_at" t.datetime "graded_at" t.integer "score" t.text "violation_description" t.text "violation_code" t.text "grade" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "restaurants"do |t| t.text "name" t.text "camis" t.text "building_number" t.text "street" t.text "zipcode" t.integer "borough" t.text "cuisine" t.datetime "created_at", null: false t.datetime "updated_at", null: false end end db/schema.rb

Slide 45

Slide 45 text

class Inspection < ActiveRecord::Base belongs_to :restaurant end app/models/inspection.rb

Slide 46

Slide 46 text

class Restaurant < ActiveRecord::Base enum borough: ['BRONX', 'BROOKLYN', 'MANHATTAN', 'STATEN_ISLAND', 'QUEENS'] has_many :inspections def grade return unless last_inspection last_inspection.grade end private def last_inspection inspections.where.not(grade: nil).order(inspected_at: :desc).last end end app/models/restaurant.rb

Slide 47

Slide 47 text

graphql-ruby

Slide 48

Slide 48 text

app/graph/graph/types/restaurant.rb module Graph module Types Restaurant = GraphQL::ObjectType.define do name 'Restaurant' description 'A place of business serving food in New York City' global_id_field :id field :name, !types.String, 'The doing-business-as value.' field :camis, !types.String, 'The unique identifier.' field :buildingNumber, types.String, 'The street number.', property: :building_number field :street, types.String, 'The street name.' field :zipcode, types.String, 'The zip code.' field :cuisine, types.String, 'The cuisine.' field :grade, types.String, 'The latest grade of an inspection.' end end end

Slide 49

Slide 49 text

app/graph/graph/types/inspection.rb module Graph module Types Inspection = GraphQL::ObjectType.define do name 'Inspection' description 'A NYC health inspection.' global_id_field :id field :type, types.String, 'The type of inspection.' field :violationDescription, types.String, 'The violation description.', property: :violation_description field :violationCode, types.String, 'The violation code cited.', property: :violation_code field :grade, types.String, 'The grade received.' field :score, types.Int, 'The numeric score received.' field :inspectedAt, !types.String, 'The inspection timestamp.', property: :inspected_at field :gradedAt, types.String, 'The issued grade timestamp.', property: :graded_at end end end

Slide 50

Slide 50 text

app/graph/graph/types/root_query.rb module Graph module Types RootQuery = GraphQL::ObjectType.define do name "RootQuery" description "The query root." field :restaurant do type -> { Types::Restaurant } description "Perform a search for one restaurant." argument :name, types.String resolve -> (object, arguments, context) do ::Restaurant.find_by(name: arguments['name']) end end end end end

Slide 51

Slide 51 text

{ restaurant(name: "Cafe Ghia") { name buildingNumber street zipcode borough grade } } { "data": { "restaurant": { "name": "Cafe Ghia", "buildingNumber": "24", "street": "Irving Avenue", "zipcode": "11237", "borough": "BROOKLYN", "grade": "A" } } } https://nyc-restaurant-grades.com/graphql POST

Slide 52

Slide 52 text

class Restaurant < ActiveRecord::Base enum borough: ['BRONX', 'BROOKLYN', 'MANHATTAN', 'STATEN_ISLAND', 'QUEENS'] has_many :inspections def grade return unless last_inspection last_inspection.grade end private def last_inspection inspections.where.not(grade: nil).order(inspected_at: :desc).last end end app/models/restaurant.rb

Slide 53

Slide 53 text

Enter, Relay

Slide 54

Slide 54 text

but first

Slide 55

Slide 55 text

an introduction to Graph Theory

Slide 56

Slide 56 text

Graphs contain:

Slide 57

Slide 57 text

nodes

Slide 58

Slide 58 text

edges

Slide 59

Slide 59 text

{ "name": "Cafe Ghia", "buildingNumber": "24", "street": "Irving Avenue", "zipcode": "11237", "borough": "BROOKLYN" }

Slide 60

Slide 60 text

{ "name": "Cafe Ghia", "buildingNumber": "24", "street": "Irving Avenue", "zipcode": "11237", "borough": "BROOKLYN" } { "grade": "A", "score": 13, "type": "Cycle Inspection", "violationCode": "10B", "violationDescription": "Plumbing not properly installed.", "inspectedAt": "2016-02-18 00:00:00 UTC", "gradedAt": "2016-02-18 00:00:00 UTC" }

Slide 61

Slide 61 text

{ "name": "Cafe Ghia", "buildingNumber": "24", "street": "Irving Avenue", "zipcode": "11237", "borough": "BROOKLYN" } { "grade": "A", "score": 13, "type": "Cycle Inspection", "violationCode": "02G", "violationDescription": "Cold food item held above 41ºF", "inspectedAt": "2016-02-18 00:00:00 UTC", "gradedAt": "2016-02-18 00:00:00 UTC" } { "grade": "A", "score": 13, "type": "Cycle Inspection", "violationCode": "10B", "violationDescription": "Plumbing not properly installed.", "inspectedAt": "2016-02-18 00:00:00 UTC", "gradedAt": "2016-02-18 00:00:00 UTC" }

Slide 62

Slide 62 text

{ restaurant(name:"Cafe Ghia") { inspections(first:2) { edges { node { violationCode violationDescription } } } } } https://nyc-restaurant-grades.com/graphql POST

Slide 63

Slide 63 text

{ restaurant(name:"Cafe Ghia") { inspections(first:2) { edges { node { violationCode violationDescription } } } } } { "data": { "restaurant": { "inspections": { "edges": [ { "node": { "violationCode": "10F", "violationDescription": "Non-food contact surface improperly constructed. Unacceptable material used." } }, { "node": { "violationCode": "04L", "violationDescription": "Evidence of mice or live mice present in facility's food and/or non-food areas." } } ] } } } } https://nyc-restaurant-grades.com/graphql POST

Slide 64

Slide 64 text

graphql-relay-ruby

Slide 65

Slide 65 text

app/graph/graph/types/restaurant.rb module Graph module Types Restaurant = GraphQL::ObjectType.define do name 'Restaurant' description 'A place of business serving food in New York City' connection :inspections, -> { Types::Inspection.connection_type } do description 'List the inspections.' resolve -> (object, arguments, context) do object.inspections end end end end end

Slide 66

Slide 66 text

Mutations

Slide 67

Slide 67 text

Versions

Slide 68

Slide 68 text

More information

Slide 69

Slide 69 text

https://facebook.github.io/graphql/

Slide 70

Slide 70 text

http://nyc-restaurant-grades.com

Slide 71

Slide 71 text

https://github.com/bswinnerton/nyc-restaurant-grades

Slide 72

Slide 72 text

Thanks Follow me on Twitter & GitHub: @bswinnerton