Slide 1

Slide 1 text

From REST to GraphQL Marc-Andre Giroux @__xuorig__

Slide 2

Slide 2 text

About me

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

A simple UI component

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

What kind of data is needed?

Slide 7

Slide 7 text

Cart Product ProductImage

Slide 8

Slide 8 text

Reusable endpoints (REST)

Slide 9

Slide 9 text

/carts/1 /products/1 /products/2 /products/3 /product_images/1 /product_images/2 /product_images/3

Slide 10

Slide 10 text

Too many round trips!

Slide 11

Slide 11 text

/carts/1?expand=products

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Custom Endpoints

Slide 15

Slide 15 text

/cart_with_all_the_stuff_i_need

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

/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 18

Slide 18 text

No content

Slide 19

Slide 19 text

Server Client Updates a view Creates a new view Product view v2 Product model changes Update endpoints Create new endpoint

Slide 20

Slide 20 text

GraphQL

Slide 21

Slide 21 text

What GraphQL is NOT

Slide 22

Slide 22 text

What GraphQL IS

Slide 23

Slide 23 text

{ myShop { name } } Field Selection Set

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

{ myShop { name } }

Slide 26

Slide 26 text

{ myShop { name } }

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

{ shop(id: 1) { name } }

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

{ “myShop”: { “name”: “Full Stack Fest Shop” “location” { “city”: “Barcelona” “address”: “Av. Diagonal 547” } “products”: [{ “name”: “Conference Ticket” “price”: 500000 }, { “name”: “Cool T-Shirt” “price”: 20000 }] } }

Slide 31

Slide 31 text

Type System

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

type Address { city: String address: String }

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

type Product { name: String price: Int }

Slide 41

Slide 41 text

Fragments

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

fragment productFields on Product { id name price }

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

query Fragment Fragment Fragment Fragment

Slide 48

Slide 48 text

Introspection

Slide 49

Slide 49 text

query { __schema { … } }

Slide 50

Slide 50 text

Static Validation Code Generation IDE Integration Auto Documentation

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

Resolving fields

Slide 54

Slide 54 text

type Product { name: String price: Int }

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

POST /graphql

Slide 60

Slide 60 text

Mutations

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

Drawbacks and solutions

Slide 63

Slide 63 text

N+1 Queries

Slide 64

Slide 64 text

field :image do type ImageType resolve -> (product, args, ctx) do product.image end end field :products do type [ProductType] resolve -> (shop, args, ctx) do shop.products end end

Slide 65

Slide 65 text

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 66

Slide 66 text

Solution: Batching + Caching

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

HTTP Caching

Slide 69

Slide 69 text

Solution: Client Side Cache

Slide 70

Slide 70 text

Normalized Cache

Slide 71

Slide 71 text

Normalized Cache

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

Security

Slide 76

Slide 76 text

Query Depth Timeouts Query Complexity

Slide 77

Slide 77 text

Future

Slide 78

Slide 78 text

Subscriptions

Slide 79

Slide 79 text

subscription { productInventorySubscribe { products { inventory } } }

Slide 80

Slide 80 text

Deferred Queries

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

Thank you Marc-Andre Giroux @__xuorig__