When to use GraphQL? The benefits over REST

When to use GraphQL? The benefits over REST

Representative State Transfer commonly referred to as REST has been central to the development of RESTful web services.  These services are more or less the backbone of interoperability between various internet-based systems and platforms.  The simplicity of these types of query structures has been central to its success over the years.  However, the main shortfall of rest queries is that it does not provide granular control over queries. This drawback results in fetching of unnecessary data and unavoidable repeated API calls. This article delves into details about GraphQL, its history, and how it evolved into a vital tool for building APIs. We shall also analyze the main components of GaphQL and review the key components of the GraphQL ecosphere.

What Is GraphQL?

In a layman’s language, GraphQL is an open-source language that is used to query a database from a client program. Perhaps, the best person to define GraphQL is Dan Schafer, the co-founder of Graph QL.  This is how Dan describes GraphQL

 “GraphQL is a query language for your API that shifts the contract between clients and servers that allows the server to say ‘these are the capabilities that I exposed’ and allows the clients to describe their requirements in a way that ultimately empowers product developers to build the products they want to create.”

Besides asking for precise data, GraphQL also instructs the API to present the data retrieved in a specific manner.  All these features work together so that developers can make precise data request and receive exactly what they asked for.

Developers can benefit from GraphQL in the following ways:

  • Make a precise request and receive the exact data in a predictable manner.
  • Retrieve data from multiple sources through a single request.
  • The API allows the user to know the available data and the state of the data (strongly typed).

 In practical terms, the GraphQL is just a simple string that returns as a JSON when it is sent to the server. The interesting part is that the queries replicate the server response. Therefore, it is easy to predict the shape of the data that is returned after running a GraphQL query. This feature also allows developers to write queries easily as long as they know the data that the app needs.

Another advantage of GraphQL is that it uses a hierarchical method for querying data sources.  This simple approach uses the existing relationship within the object, which is perfect for the graph-structured data typical to hierarchical users. It is also easy to relate with compared to RESTful services and complicated SQL join statements.

GraphQL does not dictate or provide data stores, it simply uses a method that relies on existing codes. In essence, the developer can simply request the type of data that the GaphQL supports. This approach makes it easy for tools to use the data retrieved with greater efficiency.

 Another important fact is that the shape of the data returned is more or less controlled by the query the client sends.  This feature makes room for the addition of extra fields to the server without complications. For instance, you can decide to add new or deprecate existing project features while the existing clients remain intact.  The practical advantage is that when you are deprecating older features, the corresponding server fields will continue to function normally. As a result, GraphQL enhances backward compatibility, so you no longer need an incremental version numbering. 

Since you now have a better idea about GraphQL and what you can achieve with it, let’s look back at history. What led to the creation of GraphQL?

When to use GraphQL?

The application of GraphQL is pretty broad and there are several situations that you can benefit from using this language. Here are 3 common use cases of why you should use GraphQL over REST.

Reduce Overfetching

This is the most common situation that developers go for GraphQL. Using this language will allow fetching the exact amount of requests a query needs. No more, no less. Very simple and effective. This benefit will derive into two other use cases. Mobile social network applications can particularly benefit from reducing over-fetching.

Reduce Data Transfer Costs

Reducing data transfer on client and server-side is a secondary benefit of using GraphQL. The data transfer is a function of the quantity of API calls an app does. The more requests, the higher the quantity of data that is transferred. Reducing over fetching will make your application process a smaller quantity of requests and consequently decrease the total data transfer cost. This means a lower cloud service bill on the server-side. On the client-side, this means less network usage (3G/4G or 5GB).

Improve App Performance

This is another secondary implication of reducing over-fetching. The lower the number of requests, the less processing power on the server and client-side will be required. On the server-side, a large number of requests can be compensated by more or larger servers. On the client-side, the developer has no control over the user’s device and over fetching will make the mobile app slower or not operational at all. So, reducing over-fetching will lead you to better performance on the client-side (mobile phones) and to lower server-side costs (smaller servers or fewer number of instances)

How GraphQL began?

What are developers using before the creation of GaphQL? And what are the challenges they faced that led them to seek an alternative? We shall delve into these facts in a short while.

  • The Mobile Shift.

The GraphQL antecedence dates many years before it was created. Facebook decided to embrace mobile technology and created an HTML-5-based mobile app. However, inefficient network usage made the mobile strategy ineffective, so the company decided to rebuild the app. This time around, Facebook opted for native technology.

The problem with Facebook Feed

The procedure of implementing feed on Facebook Mobile app is complicated. It required more than just fetching a story, the identity of the author, comments, and people that liked the post. In essence, the content of the feeds is interrelated, nested, and recursive. Unfortunately, the existing APIs are not designed for handling the news feed experience on mobile devices. These APIs do not use a hierarchical structure which gives the developer the freedom to fetch any content within it. Neither can they display a random list of feed stories.

To overcome this challenge, Facebook decided to develop a News Feed API, specifically for the Facebook Mobile app. This was back in 2012 when the Facebook in-house team started developing what is now known as GraphQl.  In August of the same year, The Facebook App for iOS5 was released, and powered by the new GraphQL technology for displaying News Feeds.

With the new data-fetching technology, developers are now empowered with enhanced data-fetching capabilities that reduced the load on the network.  Over the next 18 months, the function of GraphQL was scaled to take care of most features of the Facebook iOS app. The first GraphQl spec along with the reference for implementation in JavaScript was published in 2015,

Drawbacks of the conventional REST APIs

The primary challenge that REST API posed to developers is its slowness and hard coding. Although a possible solution to enhance speed is to create multiple endpoints, but this is not practical. An attempt to use REST to handle all the numerous data sources that an API client needs to expose the limitations of REST APIs

The following hypothetical scenario explains the limitations of REST APIs.

If you are using a vending machine, you are required to press one item to vent a product. In a scenario where a customer wants to purchase several items, you will be compelled to press several buttons to vend the items. This approach is painfully slow.

There should be a faster way of sending multiple items.  What about having a special button that can be used to sell multiple items? Perhaps a combination of four items at a time?

A third approach would be a system that allows you to press a precise key combination and vend multiple items in a single run. This is the approach of GraphQL.

To solve the inherent problem, GraphQL decided to implement a single intelligent endpoint as opposed to multiple dumb endpoints used by REST. This simple change is vital to GraphQL performance because smart endpoints can organize complex queries and deliver the data that client app requests.

The GraphQL layer sits between the client application and the data source. It listens and receives the requests made by clients and fetches the required data based on the requirements of the query. The approach that GraphQL uses to handle queries has solved the pressing problems of large-scale apps and scalability challenges of fast-growing ones.

What Made GraphQL Famous?

The challenge that GraphQL solves for the mobile industry has been pressing without a solution.  That is why it was adopted very fast by developers around the world. Within just six months of its release, GraphQL was implemented in multiple programming languages such as JavaScript, PHP, Python, Scala, and Ruby.

As more hobbyist programmers and companies tested the technology and experienced the advantages, they quickly adopted it. And the good news of GraphQL started spreading like wildfire.  It did not take long before big-time companies started using GraphQL technology. First, it was GitHub in 2016, then Twitter, New York Times, Airbnb, Yelp, and many more.

GraphQL Key Components

In practical terms, there are three significant components in GraphQL, which are Queries, Resolvers, and Schemas.

The Query is the API request that a client application makes. The query supports augments and can also point to arrays.

The Resolver tells the GraphQL Server what to do with an API query. In simple terms, it shows the GraphQL Server the procedure and location from which a data corresponding to a specific field will be fetched. The resolver also decouples both the API schema and the database schema. Consequently, the decoupled information is used as a mutation resolver which modifies the content fetched from the database.

The GraphQL Schema describes the functions that the client app can access after connecting to the GraphQL server. She schema consists of several building blocks known as types.

Next, we shall review the major players in the GraphQL Ecosphere.

GraphQL Servers

Because the GraphQL is just an ordinary specification, it will require a GraphQL implementation to work.

  • GraphQL-JS: This is the original implementation reference for GaphQL that works with Express.
  • GraphQL-Server:  This is a comprehensive Apollo GraphQL server solution that is becoming increasingly popular.  Any GaphQL client can query it without hassles.
  • GraphQL Serverless: This is back4pp’s database -integrated GraphQL API solution with cloud functions.  In essence, it gives developers access to Backend as a Service which offers GraphQL APIs and MongoDB database.
  • GraphQL-Yoga: A Prisma’s server implementation of GraphQL that makes use of Express and Apollo Servers.

GraphQL Clients

It is possible to query a GraphQL API directly, but a better approach is to leverage the power of a dedicated client library.

  • Relay: This is a JavaScript library developed by Facebook for building React applications with GraphQL.
  • Apollo Client: This program caches requests and normalizes data to save network traffic. Other features include prefetching data, support for pagination, and also creates a connection between the data and view layers.

GraphQL Gateways

Apollo Engine is arguably the most popular GraphQL gateway. It provides query execution tracing, error tracking, query caching, as well as API performance trend analysis.

Open Source Apps and Tools

Some open-source applications and tools that use GraphQL include the following:

  • Gatsby: is a GraphQL-powered application that can use data derived from multiple GraphQL API sources to create a static, client-based React application.
  • VulcanJS: This is a powerful react and  GraphQL framework for building CRUD applications quickly.
  • GraphQL Playground:  This is a powerful IDE that has an in-built editor for handling GraphQl queries, mutations, subscriptions, validation, and many more.  With this IDE, the developer can visualize the structure of a schema.
  • GraphiQL: This is an ID that integrates with the web browser and interacts with GraphQL APIs. Some of the functions that it enables include data querying, mutations, and autocompletes queries.


GraphQL was developed by Facebook to resolve challenges with News Feed API in the Facebook iOS app. The program became useful for solving many other problems on Facebook. After Facebook turned GraphQL into an open-source program, it quickly became a community tool used by developers and global companies. What is your opinion about GraphQL? Do you think it will eventually become an industry standard? Let us know your view by dropping a comment.

Leave a reply

Your email address will not be published.