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

GraphQL

 GraphQL

Stefan Kanev

March 28, 2018
Tweet

More Decks by Stefan Kanev

Other Decks in Programming

Transcript

  1. Queries data on the server is represented as a graph

    
 the client specifies which part of the graph to fetch 
 server returns only those parts 
 the input is in a special language 
 the output is in JSON
  2. query { hero { name friends { name } }

    } Query { "data": { "hero": { "name": "R2-D2", "friends": [ { "name": "Luke Skywalker" }, { "name": "Han Solo" }, { "name": "Leia Organa" } ] } } } Response
  3. Query query { hero { name appearsIn friends { name

    appearsIn } } } Response { "data": { "hero": { "name": "R2-D2", "appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"], "friends": [ { "name": "Luke Skywalker", "appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"] }, { "name": "Han Solo", "appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"] }, { "name": "Leia Organa", "appearsIn": ["NEWHOPE", "EMPIRE", "JEDI"] } ] } } }
  4. Schema the graph is composed of objects 
 each object

    is of a specific type 
 each type defines a number of fields 
 each field has a type 
 there is a form of polymorphism
  5. type Character { name: String! friends: [Character]! appearsIn: [Episode]! }

    type Character { name: String! friends: [Character!]! appearsIn: [Episode!]! } By the way, this… …is better in a different way: Anybody see the difference?
  6. Mutations each mutation is a separate endpoint 
 it’s RPC-like

    (conceptually similar to REST mutations) 
 it still goes through the GraphQL language 
 we’ll see an example later
  7. Caveat emptor It might be a better fit for when

    you control the client, as opposed to letting anybody create a client 
 The complexity on the backend is a magnitude bigger
  8. Decouples clients from backend once the graph is large enough,

    clients won’t need to request (as many) endpoints for reading 
 it’s closer to “write once”
  9. Versionless evolution Versioning APIs is a bitch 
 You can

    get away with not doing it to a large extent 
 (example)
  10. Traffic optimisation You get only what you asked for 


    You can get multiple things with a single request
  11. Structure It provides a lot of structure to build 3rd

    party tools on
 (especially in comparison to REST) 
 GraphiQL is a good example 
 Apollo Engine is another one 
 Relay is the most interesting example
  12. ?