What is GraphQL?

Plan: Developer Foundations

Lesson 2 of 28 · 15 min

GraphQL

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data.

Developed by Facebook in 2012 and open-sourced in 2015, GraphQL allows clients to define the structure of the data presented in a query and returned from the server, thus preventing excessively large amounts of data from being returned.

GraphQL provides a complete 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.

Benefits of GraphQL

  • Get Predictable Results - Send a GraphQL query to your API and get exactly what you need, nothing more and nothing less. GraphQL queries always return predictable results. Apps using GraphQL are fast and stable because they control the data they get, not the server.
  • Get Many Resources in a Single Request- GraphQL queries access not just the properties of one resource but also smoothly follow references between them. While completing a task with typical REST APIs often requires making calls to multiple endpoints, GraphQL APIs get all the data your app needs in a single request. Apps using GraphQL can be quick even on slow mobile network connections.
  • Type System - GraphQL APIs are organized in terms of types and fields, not endpoints. Access the full capabilities of your data from a single endpoint. GraphQL uses types to ensure Apps only ask for what’s possible and provide clear and helpful errors. Apps can use types to avoid writing manual parsing code.

GraphQL vs REST API

It’s important to understand the differences between GraphQL and REST API and how they work together. Click the tabs below to explore some of the major differences between the two.

REST APIs put different information on different endpoints or paths/URLs. There is an endpoint for products and there is an endpoint for cart. They are different URLs. For this reason, if you are interested in both cart and product information at the same time, you have to run more than one HTTP request because that is how the API is structured.

With GraphQL there is only one single endpoint: /graphql

With GraphQL, you can ask more complex questions with a single API request.

I want to know about product 1234 and I also want to know about the category tree

Resources