Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From REST to GraphQL

From REST to GraphQL

Marc-Andre Giroux

September 06, 2016
Tweet

More Decks by Marc-Andre Giroux

Other Decks in Programming

Transcript

  1. Server Client Updates a view Creates a new view Product

    view v2 Product model changes Update endpoints Create new endpoint
  2. { myShop { name } } Lexed Parsed Validated Executed

    { “myShop” { “name”: “GitHub” } }
  3. { “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 }] } }
  4. type Shop { name: String location: Address products(orderby: OrderEnum): [Product]

    } enum ProductOrderEnum { PRICE, POPULARITY, ALPHABETICAL }
  5. field :price do type types.Int resolve -> (obj, args, ctx)

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

    do obj.subtotal + obj.taxes + obj.shipping_price end end
  7. 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
  8. 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" = …
  9. field :image do type ImageType resolve -> (product, args, ctx)

    do RecordLoader.for(Image).load(product.image_id) end end
  10. query { shop { products { price } } }

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