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

GraphQl - One endpoint to rule your application...

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

GraphQl - One endpoint to rule your application - Open-Source Kerala 2017

Avatar for Shaiju Edakulangara

Shaiju Edakulangara

November 25, 2017

More Decks by Shaiju Edakulangara

Other Decks in Technology

Transcript

  1. { name: 'Shaiju E', email: '[email protected]', tech_blog: 'tech.eshaiju.in', github: 'eshaiju',

    twitter: 'eshaiju', facebook: 'eshaiju', linkedin: 'eshaiju' }
  2. Agenda Modern API technologies & challenges What it is, what

    it’s not and what GraphQL aims to solves? Supported languages Who’s using GraphQL?
  3. REST Architectural style of the web Resources a single resource

    e.g. /api/posts Verbs GET, PUT, DELETE & POST
  4. GET /comments/post_id=1 GET /comments/post_id=2 GET /comments/post_id=5 GET /posts GET /users/1

    GET /users/2 GET /users/5 Multiple round trips! (Under-fetching) Client Server
  5. GET /comments/post_id=1 GET /comments/post_id=2 GET /comments/post_id=5 GET /posts GET /users/1

    GET /users/2 GET /users/5 Client Server Includes /api/posts?include=users,comments
  6. "post": { "id": 1, "title": "A GraphQL Server Implementation", "url":

    "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-ruby-on-rails/" } What I get What I care about Over-fetching of data
  7. What GraphQL is NOT It's not about graph databases and

    graph search It's not about data storage it's not language specific it's not a library
  8. What GraphQL IS GraphQL is a query language for APIs

    is a new API standard that provides an alternative to REST 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 GraphQL
  9. query { post(id: 1){ title url published_at } } {

    "data": { "post": { "title": "A GraphQL Server Implementa "url": "http://tech.eshaiju.in/blog/2 "published_at": "2017-07-11 00:00:00 } } }
  10. query { users{ id name } post(id: 1){ title url

    published_at author{ name } comments{ body author{ name } } } } { "data": { "users": [ { "id": 1, "name": "Shaiju E" }, { "id": 2, "name": "Awin" } ], "post": { "title": "A GraphQL Server Implementation", "url": "http://tech.eshaiju.in/blog/2017/05/06/a-graphql-se "published_at": "11 Jul 2017 00 00 AM", "author": { "name": "Shaiju E" }, "comments": [ { "body": "Good post", "author": { "name": "Awin" } } ] } } }
  11. POST /api curl -XPOST -d 'query { post(id: 1) {

    title url } }' http://localhost:3000/api
  12. 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 Mutations are the equivalent to the POST, PUT, PATCH and DELETE in HTTP/REST. Mutation
  13. PostType = GraphQL::ObjectType.define do name "Post" description "Post and details"

    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') } deprecation_reason 'This field is deprecated' end field :comments, types[CommentType] field :author, UserType end Implementation - Type System
  14. query { post(id: 1){ title url published_at } } Efficiency

    { "data": { "post": { "title": "A GraphQL Server Implementa "url": "http://tech.eshaiju.in/blog/2 "published_at": "2017-07-11 00:00:00 } } }
  15. Efficiency query { users{ id name } post(id: 1){ title

    url published_at author{ name } comments{ body author{ name } } } } { "data": { "users": [ { "id": 1, "name": "Shaiju E" }, { "id": 2, "name": "Awin" } ], "post": { "title": "A GraphQL Server Implementation", "url": "http://tech.eshaiju.in/blog/2017/05/06/a-graphql-server-impl "published_at": "11 Jul 2017 00 00 AM", "author": { "name": "Shaiju E" }, "comments": [ { "body": "Good post", "author": { "name": "Awin" } } ] } } }
  16. Versioning Evolve your API without versions GraphQL only returns the

    data that's explicitly requested New capabilities can be added via new types and new fields on those types without creating a breaking change. Deprecate older fields
  17. Server Libraries Ruby C# / .NET Elixir Erlang Go Groovy

    Java JavaScript PHP Python Scala Clojure