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

GraphQL on Rails

GraphQL on Rails

Introduction to GraphQl and how to use it in Rails.

Marc-Andre Giroux

March 15, 2016
Tweet

More Decks by Marc-Andre Giroux

Other Decks in Programming

Transcript

  1. Server Client product view v1 product model v1 update endpoints

    create new endpoints product view v2 product view v3 product model v2 product model v3
  2. { “myShop” { “name”: “GitHub” } } { myShop {

    name } } Lexed Parsed Validated Executed
  3. { myShop { name location { city address } products(orderby:

    POPULARITY) { name price } } } type QueryRoot { myShop: Shop shop(id: Int): Shop }
  4. { myShop { name location { city address } products(orderby:

    POPULARITY) { name price } } } type Shop { name: String location: Address products(orderby: OrderEnum): [Product] } enum ProductOrderEnum { PRICE, POPULARITY, ALPHABETICAL }
  5. { myShop { name location { city address } products(orderby:

    POPULARITY) { name price } } } type Address { city: String address: String }
  6. { myShop { name location { city address } products(orderby:

    POPULARITY) { name price } } } type Product { name: String price: Int }
  7. Server Client H ER E I S P RO DU

    C T W I TH I D = 1 G ET P R OD U C T W IT H I D= 1
  8. Server Client EX P O SES S C HE M

    A O F AL L PO S SI B I L IT I ES ( T Y P E SYS T EM ) DATA R EQ UI R EM EN T S (G R AP H Q L LA N GUAG E )
  9. Server Client O K H ER E’S EX AC T

    LY T HE DATA YO U NEE D G I VE ME DATA O F T HAT PAR T IC U LA R S H AP E
  10. Server Client product view v1 Product model v1 product view

    v3 Product model v3 product view v2 Product model v2 Doesn’t care
  11. rails generate model Blog title:string content:string author_id:integer rails generate model

    Author name:string class Blog < ActiveRecord::Base has_one :author end
  12. { "data": { "blogs": [ { "title": "Intro to GraphQL",

    "content": "Something something something. Blah blah blah. Etc etc etc.", "author": { "name": "Marc-Andre Giroux" } }, { "title": "Hello, it's me", "content": "I've been wondering", "author": { "name": "Marc-Andre Giroux" } }, { "title": "Hello, it's me", "content": "I've been wondering", "author": { "name": "Marc-Andre Giroux" } } ] } }
  13. MG I ROUX.M E G RAPH QL-SLACK .HE R OK

    UA P P.COM/ Slack channel My blog graphql-ruby G IT HU B.COM /R MO SO LG O/ G R A P H Q L-R U BY Twitter @__ XUORI G__