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

GraphQL: How and Why

GraphQL: How and Why

Introduction to GraphQL concepts and advantages.

Afd676ef11a648704b71df2243bf34bc?s=128

Timi Ajiboye

July 28, 2017
Tweet

Transcript

  1. GraphQL is a query language for APIs and a runtime

    for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools. — graphql.org
  2. GraphQL is simply a specification for how clients get data

    from APIs This sounds exactly like HTTP REST , doesn’t it?
  3. Very Long Documentation REST Client Server

  4. REST Server Client Yo, if you want the information on

    users, just shout ‘/USERS’, and I’ll give you a list of them. But if you want to see the products the user has bought, you gotta shout ‘/USER/PRODUCTS’ along with the user’s ID and if you want…etc. Got it? *in Morty’s voice* Man, that was a lot of things, could you write that down for me? No way I could remember all that. Sure, the devs have written really nice documentation for you.
  5. GraphQL Server Client Auto Docs

  6. GraphQL Server Client Just ask for whatever, how you want

    it, in whatever combination you want. As long as you’re speaking GraphQL queries, I’m your man! Awesome!
  7. GraphQL is actually almost like defining functions/methods that your client

    can call. Just a bit more powerful, because the client can determine exactly what it wants returned and can combine some of these functions. If you’ve ever used SQL, this should have started becoming clear by now. In SQL you write queries to get the data you want from the database, how you want it. The difference here is that: • You’re not querying from within the same application, you’re using the client application to query the server application. • You get to define what these queries are/can be.
  8. This is an example of a GraphQL query a client

    would make if it wanted to get a list of Heroes and their names.
  9. This is an example of a GraphQL query a client

    would make if it wanted to get a list of Heroes and their names, powers & a list of Villains, their names, powers and crimes they committed between ‘82 and 2017
  10. Example response

  11. Client Server Query Needs to parse and respond to query

  12. Client Server

  13. Other Tools In browser GraphQL IDE/explorer

  14. Client chrome extension Other Tools

  15. Build a Node.js API in your browser Other Tools

  16. Other Tools GraphQL analytics

  17. It does documentation automatically. Because everything is done with Types,

    tools like GraphiQL can generate docs from your server code easily. The client no longer needs to make multiple queries to display data of different models on one view. You can adopt it incrementally. One can just add GraphQL to their server without changing how it works. It’s not tied to any specific language or framework, you can use any DB you like! It improves organisation, communication between developers and stability of code.
  18. Schema Language Basic concepts Types are the most basic components

    of a schema An Object Type is a type with some fields Built in scalar type. Like primitive types. This means non-nullable An array of Episode types This is a field. Define what can appear in a GraphQL query
  19. Schema Language Argument and it’s type. Enum Type Remember, GraphQL

    is language agnostic, and we cannot rely on specific syntax to describe our data. That’s why the schema language is necessary. Each field is backed by an arbitrary function in the server’s code. ‼‼ More Pros Because GraphQL is strongly typed. It can provide descriptive error messages before queries are executed. Both on the server and client. GraphQL is introspective. Clients can query servers for the types it supports. IDEs and libraries leverage this for hints and code generation.
  20. None
  21. Learn More graphql.org/learn howtographql.com learnapollo.com learnrelay.org graph.cool dev-blog.apollodata.com

  22. None