What is GraphQL?

Learn about how GraphQL works and how to use it to fetch data.

In 2012, Facebook created GraphQL because they needed a better data-fetching approach that they could use across the entire company's products and services. They needed a new approach to fix a specific set of problems and shortcomings they had encountered with the REST approach. They also wanted something that was not too technical and could be understood by developers, designers, and non-technical folk.

GraphQL was launched and used internally by the Facebook team between 2012 and 2015. In 2015, Facebook open-sourced GraphQL, making it available for the public to benefit from and use. The GraphQL Foundation now maintains it.

People relied on the REST technique for communicating with APIs before the advent of GraphQL—and some rely on it even now. REST or Representational State Transfer, is an architectural style that conforms to a set of constraints when developing web services. It replaced SOAP APIs, making it possible to return multiple data formats (JSON, XML, and YAML) depending on what is needed, unlike having to return only XML format, which was the case with SOAP.

A REST API request is made up of the following:

  • The endpoint, also known as the URL, is used to help identify the resource online.

  • The type of request sent to the server is described by an HTTP method. This could be GET, PUT, POST, DELETE, or PATCH, all aimed toward performing CRUD (Create, Read, Update and Delete) operations.

  • The header provides information to clients and servers for purposes like authentication, caching, AB Testing, and more.

  • The body contains data that a client wishes to send to a server, such as the payload of a request.

The modern approach to client-server communication is GraphQL. It's a query language that's used to communicate (reading and mutating) with application programming interfaces (APIs) and returns exactly the data that is requested and nothing else.

GraphQL has many features that solve the shortcomings of the REST approach to API consumption. GraphQL is designed to make APIs fast, developer friendly, and very flexible. Since we only get the requested data with single requests within our application or web page, our page loads faster.

Why use GraphQL?

When we compare GraphQL with the REST approach, we arrive at some key reasons why we should use GraphQL. These are as follows:

Making a single request

Previously, developers have had to consume data using REST, where data entities live on different URLs/endpoints on a server, forcing developers to make numerous requests for similar data. For example, if we want to get all the products made by a particular user, we would send a GET request to the URL /users/{userid}/products. This is still okay, but when we want to get more details about the user, such as a hobby, birthday, and so on, we would need to make separate GET requests to the URL /users/{userid}.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy