Slide 1

Slide 1 text

GraphQL on Rails Marc-Andre Giroux @__xuorig__

Slide 2

Slide 2 text

@__xuorig__

Slide 3

Slide 3 text

@__xuorig__ therakiasite.blogspot.com

Slide 4

Slide 4 text

@__xuorig__ Montreal, Canada

Slide 5

Slide 5 text

@__xuorig__

Slide 6

Slide 6 text

@__xuorig__

Slide 7

Slide 7 text

@__xuorig__ A simple UI component

Slide 8

Slide 8 text

@__xuorig__

Slide 9

Slide 9 text

@__xuorig__ What kind of data is needed?

Slide 10

Slide 10 text

@__xuorig__ Cart Product ProductImage

Slide 11

Slide 11 text

@__xuorig__ Reusable endpoints (REST)

Slide 12

Slide 12 text

@__xuorig__ /carts/1 /products/1 /products/2 /products/3 /product_images/1 /product_images/2 /product_images/3

Slide 13

Slide 13 text

@__xuorig__ Too many round trips!

Slide 14

Slide 14 text

@__xuorig__ /carts/1?expand=products

Slide 15

Slide 15 text

@__xuorig__ /carts/1?fields=products(name, description, price)

Slide 16

Slide 16 text

@__xuorig__ /carts/1?fields=products/name,description,price

Slide 17

Slide 17 text

@__xuorig__ Custom Endpoints

Slide 18

Slide 18 text

@__xuorig__ /cart_with_all_the_stuff_i_need

Slide 19

Slide 19 text

@__xuorig__

Slide 20

Slide 20 text

@__xuorig__ /cart_with_all_the_stuff_i_need /cart_version_2_with_all_the_things /cart_with_products_and_images /cart_with_products_and_images_with_price_and_taxes my_tightly_coupled_custom_endpoint_including_only_the_things_i_need_bla_bla_bla_bla /cart_with_products_and_images_with_price_and_taxes_but_no_description /cart_with_products_and_images_with_price_and_taxes_but_no_description_v2

Slide 21

Slide 21 text

@__xuorig__

Slide 22

Slide 22 text

@__xuorig__ Server Client Updates a view Creates a new view New view version Model changes Update endpoints Create new endpoint

Slide 23

Slide 23 text

@__xuorig__ yo, give me the resource with id = abc OK, here’s the resource with id = abc Client Server

Slide 24

Slide 24 text

@__xuorig__ yo, give me the resource v2 with id = abc OK, here’s the resource v2 with id = abc Client Server

Slide 25

Slide 25 text

@__xuorig__ yo, give me the resource v3 with id = abc OK, here’s the resource v3 with id = abc Client Server

Slide 26

Slide 26 text

@__xuorig__ GraphQL

Slide 27

Slide 27 text

@__xuorig__ What GraphQL is NOT

Slide 28

Slide 28 text

@__xuorig__ What GraphQL IS

Slide 29

Slide 29 text

@__xuorig__ { myShop { name } } Field Selection Set

Slide 30

Slide 30 text

@__xuorig__ { myShop { name } } Lexed Parsed Validated Executed { “myShop” { “name”: “GitHub” } }

Slide 31

Slide 31 text

@__xuorig__ { myShop { name } }

Slide 32

Slide 32 text

@__xuorig__ { myShop { name } }

Slide 33

Slide 33 text

@__xuorig__

Slide 34

Slide 34 text

@__xuorig__ { shop(id: 1) { name } }

Slide 35

Slide 35 text

@__xuorig__ { myShop { name location { city address } products(orderby: POPULARITY) { name price } } }

Slide 36

Slide 36 text

@__xuorig__ { “myShop”: { “name”: “Euruko 2016” “location” { “city”: “Sofia” “address”: “Av. Diagonal 547” } “products”: [{ “name”: “Conference Ticket” “price”: 500000 }, { “name”: “Cool T-Shirt” “price”: 20000 }] } }

Slide 37

Slide 37 text

@__xuorig__ Type System

Slide 38

Slide 38 text

@__xuorig__ { myShop { name location { city address } products(orderby: POPULARITY) { name price } } }

Slide 39

Slide 39 text

@__xuorig__ { myShop { name location { city address } products(orderby: POPULARITY) { name price } } }

Slide 40

Slide 40 text

@__xuorig__ type QueryRoot { myShop: Shop shop(id: Int): Shop }

Slide 41

Slide 41 text

@__xuorig__ { myShop { name location { city address } products(orderby: POPULARITY) { name price } } }

Slide 42

Slide 42 text

@__xuorig__ type Shop { name: String location: Address products(orderby: OrderEnum): [Product] } enum ProductOrderEnum { PRICE, POPULARITY, ALPHABETICAL }

Slide 43

Slide 43 text

@__xuorig__ { myShop { name location { city address } products(orderby: POPULARITY) { name price } } }

Slide 44

Slide 44 text

@__xuorig__ type Address { city: String address: String }

Slide 45

Slide 45 text

@__xuorig__ { myShop { name location { city address } products(orderby: POPULARITY) { name price } } }

Slide 46

Slide 46 text

@__xuorig__ type Product { name: String price: Int }

Slide 47

Slide 47 text

@__xuorig__ { myShop { name yolo { swag } } }

Slide 48

Slide 48 text

@__xuorig__ { myShop { name yolo { swag } } } type Shop { name: String location: Address }

Slide 49

Slide 49 text

@__xuorig__ Introspection

Slide 50

Slide 50 text

@__xuorig__ query { __schema { … } }

Slide 51

Slide 51 text

@__xuorig__ Static Validation Code Generation IDE Integration Auto Documentation

Slide 52

Slide 52 text

@__xuorig__

Slide 53

Slide 53 text

@__xuorig__

Slide 54

Slide 54 text

@__xuorig__ { myShop { name location { city address } products(orderby: POPULARITY) { id name price } } }

Slide 55

Slide 55 text

@__xuorig__

Slide 56

Slide 56 text

@__xuorig__ { myShop { name location { city address } products(orderby: POPULARITY) { id name price } } }

Slide 57

Slide 57 text

@__xuorig__ Fragments

Slide 58

Slide 58 text

@__xuorig__ fragment productFields on Product { id name price }

Slide 59

Slide 59 text

@__xuorig__ { myShop { name location { city address } products(orderby: POPULARITY) { ...productFields } } }

Slide 60

Slide 60 text

@__xuorig__ query Fragment Fragment Fragment Fragment

Slide 61

Slide 61 text

@__xuorig__ Mutations

Slide 62

Slide 62 text

@__xuorig__ mutation { createProduct(name: “Nice Mug”, price: 10000) { id name } }

Slide 63

Slide 63 text

@__xuorig__ Resolving fields

Slide 64

Slide 64 text

@__xuorig__ github.com/rmosolgo/graphql-ruby

Slide 65

Slide 65 text

@__xuorig__ ProductType = GraphQL::ObjectType.define do name "Product" description “A product sold at a shop” # … end

Slide 66

Slide 66 text

@__xuorig__ field :name do type types.String resolve -> (obj, args, ctx) { obj.name } end

Slide 67

Slide 67 text

@__xuorig__ field :price do type types.Int resolve -> (obj, args, ctx) do obj.subtotal + obj.taxes + obj.shipping_price end end

Slide 68

Slide 68 text

@__xuorig__ ShopType = ObjectType.define do name “Shop" description "A Shop" field :products, [ProductType] end

Slide 69

Slide 69 text

@__xuorig__ QueryType = ObjectType.define do name “Query" description “The Root of my Schema" field :myShop, ShopType end

Slide 70

Slide 70 text

@__xuorig__ MySchema = GraphQL::Schema.new( query: QueryRoot, mutation: MutationRoot )

Slide 71

Slide 71 text

@__xuorig__ POST /graphql

Slide 72

Slide 72 text

@__xuorig__ module Api class GraphqlController < ApiController def query query_string = params[:query] variables = params[:variables] context = { current_user: current_user } # ... end end end

Slide 73

Slide 73 text

@__xuorig__ module Api class GraphqlController < ApiController def query # ... result = MySchema.query( query_string, variables: variables, context: context ) render json: result end end end

Slide 74

Slide 74 text

@__xuorig__ Drawbacks and solutions

Slide 75

Slide 75 text

@__xuorig__ N+1 Queries

Slide 76

Slide 76 text

@__xuorig__ # Product Type field :image do type ImageType resolve -> (product, args, ctx) do product.image end end # Shop Type field :products do type [ProductType] resolve -> (shop, args, ctx) do shop.products end end

Slide 77

Slide 77 text

@__xuorig__ Product Load (1.0ms) SELECT "products".* FROM "products" WHERE "products"."shop_id" = … Image Load (0.9ms) SELECT "images".* FROM "images" WHERE "images"."product_id" = … Image Load (0.2ms) SELECT "images".* FROM "images" WHERE "images"."product_id" = … Image Load (0.1ms) SELECT "images".* FROM "images" WHERE "images"."product_id" = …

Slide 78

Slide 78 text

@__xuorig__ Solution: Batching + Caching

Slide 79

Slide 79 text

@__xuorig__ field :image do type ImageType resolve -> (product, args, ctx) do RecordLoader.for(Image).load(product.image_id) end end

Slide 80

Slide 80 text

@__xuorig__ HTTP Caching

Slide 81

Slide 81 text

@__xuorig__ Solution: Client Side Cache

Slide 82

Slide 82 text

@__xuorig__ Normalized Cache

Slide 83

Slide 83 text

@__xuorig__ query { shop { products { price } } } query { shop { product(id: 1) { price } } }

Slide 84

Slide 84 text

@__xuorig__ { root: { shop: { products: [ Link.new(1) ] } }, 1: { price: 1000 } }

Slide 85

Slide 85 text

@__xuorig__ https://github.com/facebook/relay http://www.apollostack.com/

Slide 86

Slide 86 text

@__xuorig__ Security

Slide 87

Slide 87 text

@__xuorig__ Query Depth Timeouts Query Complexity

Slide 88

Slide 88 text

@__xuorig__ Future

Slide 89

Slide 89 text

@__xuorig__ Subscriptions

Slide 90

Slide 90 text

@__xuorig__ subscription { productInventorySubscribe { products { inventory } } }

Slide 91

Slide 91 text

@__xuorig__ Deferred Queries

Slide 92

Slide 92 text

@__xuorig__ query { shop { name description products { name price } } }

Slide 93

Slide 93 text

@__xuorig__ query { shop { name description products { name price } } }

Slide 94

Slide 94 text

@__xuorig__ query { shop { name description products @defer { name price } } }

Slide 95

Slide 95 text

@__xuorig__

Slide 96

Slide 96 text

@__xuorig__ yo, give me the resource with id = abc OK, here’s the resource with id = abc Client Server

Slide 97

Slide 97 text

@__xuorig__ Client Server yo, give me the resource with id = 1 yo, give me the resource with id = 2 yo, give me the resource with id = 3 yo, give me the resource with id = 4

Slide 98

Slide 98 text

@__xuorig__ oh boy :( Client Server

Slide 99

Slide 99 text

@__xuorig__ I need that exact data shape (requirements) Here is everything you can do! (capabilities) Client Server

Slide 100

Slide 100 text

@__xuorig__ Efficiency

Slide 101

Slide 101 text

@__xuorig__ Predictability

Slide 102

Slide 102 text

@__xuorig__ Thank you! Marc-Andre Giroux @__xuorig__