What is GraphQL?
Explore the fundamentals of GraphQL, its creation, and how it differs from REST APIs by enabling single-request data fetching. Understand how GraphQL queries offer precise data control, reduce over-fetching, and simplify backend communication, helping you build efficient React applications that consume data from Hygraph effectively.
We'll cover the following...
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}.
At some points, we are now beginning to have several similar requests on a single page, which could slow down our application and lots more. This is a significant downside of the REST approach.
GraphQL queries help us get all the data we need with just a single request, rather than multiple requests, which could become cumbersome to maintain and will slow down our application. Applications and web-powered platforms using GraphQL to fetch data can communicate with the database quickly, even on slow mobile network connections.
Fetch what you need
Traditional approaches like REST either end up over-fetching or under-fetching data.
By over-fetching, we mean supplying more data than we need. When the client makes an API request with the REST approach, we have access to all the actual data contained in that endpoint. Let’s say we request a particular product created by a user. We only want to output the name and price of the product, but when we access the endpoint, we already have access to all the data, such as the product image, the rating number, description, and lots more.
By under-fetching, we mean a specific endpoint doesn't provide enough of the required information. This means the client will have to make additional requests to fetch everything it needs. Let’s assume a client needs to get the last three people that purchased a product, and then has to get all the people that purchased before making another request per element to fetch the required data.
With GraphQL, we are totally in control of what we get. We get what we request for and nothing more or less or different. We get exactly what we want, just as we queried.
GraphQL query
GraphQL query is what we use to interact with our APIs. It is used to fetch data from our content repository. This is similar to the GET request we perform using REST APIs. Here is what its syntax looks like:
GraphQL returns our data exactly as we requested it and in JSON format, making it easy for us to consume this data.
Note: The API_URL added to the code below was generated from Hygraph.