A woman sitting at a table with her laptop and coffee.

Key Use Cases for GraphQL APIs

Developers who have had to retrieve data from external sources on the web (that is, nearly all of them) have probably encountered REST APIs. REST is popular for good reasons, but that doesn’t make it the right tool for every job. GraphQL APIs are a highly customizable alternative, and you should recognize situations where they might do the job more efficiently.

GraphQL motivation

Facebook created GraphQL in 2012 as a more efficient and customizable alternative to REST APIs. GraphQL has become a preferred option when flexibility and efficiency are top priorities. GraphQL is also easy to integrate with existing applications, since implementations are available in JavaScript, Python, PHP, and several other languages. Here are three use cases where GraphQL shines.

Saving bandwidth

Nobody wants to waste bandwidth, but data efficiency and faster page loading are especially important for users on mobile devices. Mobile device users often pay for the data they consume, and they can be disinclined to use applications that they perceive as data hogs. 

REST APIs are at a disadvantage in this situation because of their limited customizability. REST APIs retrieve all of the available data from a given server endpoint, even when that’s substantially more than the requestor needs. To complicate matters further, REST APIs typically return JSON objects, which often require extra handling. 

By contrast, GraphQL allows users to specify in their query exactly which fields they want. This results in a smaller payload that is already in the structure the requestor needs. This flexibility in GraphQL allows requestors to structure their queries in exactly the manner that is useful to them.

Retrieving nested data

REST APIs often require multiple queries to retrieve nested data. A website, for example, might contain several blogs, each of which contains posts, each of which contains comments. In a REST API setup, the server has different endpoints for posts and for comments, so separate queries are necessary to retrieve both. 

A GraphQL query, however, can be structured to retrieve a set of blog posts, and inside each post, retrieve that post’s comments. GraphQL also offers additional customizability, allowing you, for example, to select comments only by certain authors. This can result in significant time savings, especially as the number of queries increases.

Retrieving data from multiple sources

Many modern applications make use of dashboards, which may require data to be fetched from multiple sources. As the preferred approach to application architectures has moved away from large storehouses of information, applications increasingly rely on networks of highly specialized microservices. For example, an ecommerce store might use separate microservices to manage inventory and to handle the checkout process. 

Using REST APIs to update the application would require separate queries for each microservice, but GraphQL has the capability to retrieve all the relevant data in a single query. Such an approach offers additional advantages of agility and scalability, both with respect to queries and the applications themselves.

The above three use cases show common situations that benefit from the power and efficiency of GraphQL. These are compelling reasons to choose GraphQL over its competitors.