Give REST a rest, use GraphQL for your next Ruby API - Deccan RubyConf 2017
GraphQL is a query language for APIs. While typical REST APIs require loading from multiple URLs, GraphQL APIs get all the data your app needs in a single request which makes Apps using GraphQL can be quick and stable.
"http://tech.eshaiju.in/blog/2017/05/ "body": null, "votes": 100, "published_at": "11 Jul 2017 00 00 AM", "auther_name": "Shaiju E", "created_at": "2017-07-11 12:57:20 UTC", "updated_at": "2017-07-11 13:15:50 UTC" } "post": { "id": 1, "title": "A GraphQL Server Implementation", "url": "http://tech.eshaiju.in/blog/2017/05/06/a-graphql-server-implementation-r } What I get What I care about Over-fetching of data
is a new API standard that provides an alternative to REST GraphQL Declarative data fetching - The client specifies what data shapes, and the server returns that data in exact same shape. GraphQL server only exposes a single endpoint and responds with precisely the data a client asked for
DELETE in HTTP/REST. mutation addComment{ addComment(input: { comment: "New comment", post_id: 1, author_id: 1}) { post{ id title comments{ body user{ name } } } } } Mutations are used to change your data Mutation
of this schema" field :post do type PostType description "Find a Post by ID" argument :id, !types.ID resolve ->(obj, args, ctx) { Post.find_by_id(args[:id]) } end field :posts, types[PostType] do description "Return all posts" resolve ->(obj, args, ctx) { Post.all } end field :users, types[UserType] do description "Fetch all users" resolve ->(obj, args, ctx) { User.all } end end
field :id, types.Int field :title, types.String do resolve -> (post, args, ctx) { post.title } end field :body, types.String field :url, types.String field :votes, types.Int field :published_at, types.String do resolve -> (post, args, ctx) { post.published_at.strftime('%d %b %Y %H %M %p') } end field :comments, types[CommentType] field :author, UserType end
:body, types.String field :author, UserType field :post, PostType end UserType = GraphQL::ObjectType.define do name "User" field :id, types.Int field :name, types.String field :joined_at end