If you’re new to backend development, you may have encountered REST.
While the name might sound relaxing, it’s an acronym for Representational State Transfer.
Exaggeration (sort of) aside, REST stands for Representational State Transfer and this is not at all wordy even though you’ll probably agree with me that we stick to its acronym.
To understand what REST and GraphQL are, we would have to dive into the world of APIs which stands for Application Programming Interfaces.
So what are APIs?
APIs define a set of protocols that determine how different software applications communicate. The data formats, means of exchange, and how requests and responses are handled are greatly determined by the choice of API used.
They act as some sort of middlemen that determine how information is handled and how data is integrated from software to software. Think of APIs in the context of web development for instance, as waiters that take data from the backend (which could be the kitchen whipping up some spaghetti code) to the frontend (this could be customers impatiently waiting for the data or they don’t pay).
Where do REST and GraphQL fit in and why do they matter?
Since APIs are so important considering they handle sensitive information and have to make software interact, it makes sense that priority is placed on how we choose to design them.
Developers are a creative bunch but to ensure that creativity doesn’t get out of hand certain standards govern how APIs are handled. These are called API architectures and some examples of them are REST and graphQL (Graph Query Language).
The choice of architecture is so important when designing APIs and the differences are considered depending on the software use case. If your application has real-time usage and is rather complex, you may opt for graphQL instead of REST but if your aim is simplicity, you may prefer REST.
These have been extensively researched and they are wordy academic papers about them but lucky for you I don’t have a CS degree, just painstaking experience so we’ll get straight to the point.
An Overview of REST
REST was born in the pages of a doctoral dissertation written by Roy Fielding in 2000. He presented his new findings as standards for building distributed systems over the web. It greatly aligns with the HTTP (hypertext transfer protocol) and can be said to be quite straightforward and not complex.
A lot of web developers utilize REST in building APIs due to this relative simplicity, adding to the fact that it focuses on resources using the well-known HTTP methods which you may recognize as GET, POST, PUT, and DELETE. With GET we retrieve data, with POST we create data, PUT allows us to update data and DELETE lets us delete data.
Features of REST
REST exhibits several features. A couple of these are discussed below:
Stateless Communication
Each request sent by a client is treated as an independent transaction. What this means is that if a client requests something, say questionable browser data, the server does not keep the memory of past requests and treats the request as completely separate.
This makes it scalable. Using our restaurant analogy, the waiter takes your order and provides what you need. What you ordered last week is not important in that particular transaction.
Multiple Data Representations
In REST, data representations like JSON and XML can be used. You are therefore not ‘fixed’ to a particular data format. That makes this architecture quite flexible.
HTTP Methods
Its alignment with HTTP has been mentioned, so we tend to use methods like GET (for retrieving data), POST (for creating data/resources), PUT (for updating data), and DELETE (for removing data).
A Layered Architecture
Here, REST APIs are primarily concerned with a separation of concerns. This just means that to keep things organized, certain layers in the API are responsible for certain things and just those things.
Just like how a waiter handles just serving customers, the kitchen staff prepares bad spaghetti code and the pantry stores food products, keeping things separate helps to make your code more scalable and easy to maintain.
Your presentation layer which is concerned with user interfaces would be kept separate from your application layer which handles the logic of your application and would also be separated from your data layer which handles data manipulation. It is important to utilize this principle so you can save yourself and other developers working on your code some headaches and REST APIs make this a breeze.
In what areas could we use REST?
I should mention that while several architectures can be utilized in the same use cases, some are better suited to some cases. Deciding what to use when is therefore up to an experienced Engineer. That being said, REST tends to be widely used in the following areas:
Web Services
Communication between the backend, frontend, and mobile services is generally facilitated by RESTful API’s. You could look around codebases for websites and generally find that to be true as popular platforms like Facebook extensively use REST.
This popularity with web services encourages a good deal of learning resources, hence a lot of beginners learn to use REST before learning about other architectures.
IoT (Internet of Things)
I used to think that IoT was some hacky internet fraud scheme but you can tell I was pretty ignorant. REST is used for interactions between IoT applications, devices, and cloud services. You may find its application in smart home devices like smart thermostats.
Microservices
For microservice architectures, REST is used for communication with well-defined APIs. If you don’t know what microservices are, they are a style of building APIs that organizes them into a sort of collection of small but loosely coupled services. Each of these services can be deployed independently.
The above are just a few use cases for REST. I encourage you to surf the web for more information on the application of REST APIs.
What is GraphQL?
GraphQL, similar to REST, is an API architecture. A major difference between them however is that while in REST you may be forced to retrieve more data at a time than you need, with graphQL you get exactly what you require.
It was developed on Facebook in 2012 and was intended to be a project for internal use to improve the efficiency of their apps. However, it was such a good idea that it became a standalone product. It was later open-sourced in 2015 for the public to use.
GraphQL uses components such as types, mutations, queries, resolvers, and subscriptions. Each of these has a part to play. For instance, queries are concerned with requesting data from servers, mutations for modifying data, types to indicate the sort of data, and resolvers to handle the logic behind fetching the data.
Features of GraphQL
GraphQL possesses features of its own, some of which you can find below;
A Single Endpoint
GraphQL is precise. It possesses a single endpoint for all its interactions. It is minimalism embodied in an API design. You do not need to make multiple requests to obtain specific data.
Flexibility
With GraphQL, you only get exactly what you request, making it highly efficient and flexible. Clients can tailor their requests to include particular fields they want to see instead of a fixed whole set of data.
Real-Time Usage
It is well known for its real-time operations. This helps make applications more interactive.
Strong Typing
Graphql supports the use of strong types which essentially means that the type of data is explicitly defined or specified. With this, clients know exactly what data to expect, reducing the likelihood of errors. It also allows for better tooling—IDEs can provide auto-completion and real-time validation, making development faster and reducing debugging time.
Use Cases of GraphQL?
Complex Systems
If you are working on programs where the data changes rather frequently and can get complex quickly, you could opt for graphQL instead of REST.
Microservices
Like with REST, graphQL is used in microservices as well. It is particularly interesting because it can combine data from multiple services as is typical of microservices into one response.
Mobile Applications
A lot of mobile applications deal with real-time processes, making graphQL highly relevant. It helps in the optimization of the transfer of data over networks.
The above are only a few applications of GraphQL. On browsing the web you’re sure to find more use cases.
Difference Between GraphQL and REST
It has been established that REST and GraphQL being API architectures are two peas in a pod. As stated, however, they differ in several areas but to keep things concise we will refer to three distinct areas.
Error handling
When handling errors the two architectures differ in their status code presentation and partial success. While REST uses HTTP status codes, graphQL uses a different response structure. When there is a request failure, REST returns an error. GraphQL on the other hand can return partial data accompanied by any error messages present. We refer to this as partial success.
Data Fetching
REST has multiple endpoints based on the nature of the action to be taken. For instance to retrieve data you use GET, to create data you expose POST. This implies that to get specific data like a particular user ID, you would need to make multiple requests.
A client may need just a name and get the entire object with each request. These instances can lead to over-fetching or under-fetching data. Picture a waiter serving you a whole tray of food when you request just one plate of spaghetti-code or serving you 3 grains of rice when you need a medium-sized plate. REST fetches data this way.
On the other hand, GraphQL uses a single endpoint and retrieves specific data. The waiter serves you the single meatball you ordered and doesn’t question if you’re anorexic or not. It reduces the need for multiple requests.
Efficiency
With the above, we can speculate how the architectures fare in matters of efficiency. We see that the problem of over-fetching and under-fetching can greatly affect the efficiency of REST APIs. GraphQL however, does not have this problem.
Since REST maintains multiple endpoints, there is also the matter of increased space and time to expose those endpoints unlike with graphQL. In addition, there is increased latency with requests when using REST, a problem not encountered with graphQL’s single endpoint.
At this point, one may wonder why developers bother to use REST anymore if efficiency is a major concern. That may be a haughty conclusion however and the table below would help to put the relevance of both into perspective.
Comparison of REST and GraphQL
S/N | Aspect | REST | GRAPHQL |
Best Use Cases |
|
| |
Differences |
|
| |
Disadvantages |
|
|
Based on our discourse, we can see that both API architectures have their advantages and disadvantages as well as their best use cases. We observe that REST is useful for simple applications while graphQL shows promise for more complex ones.
We also note differences in how they handle data. Using the above, you may be more guided in your choice of API architecture.
Be sure to consider the scale, complexity, and requirements of your application when deciding between REST and graphQL. The right choice can significantly affect your application’s performance and development speed.
Choosing the Right API Architecture
Here’s a step-by-step approach to help you decide:
Define Your Application's Needs:
Does your application require real-time updates? Opt for GraphQL.
Is simplicity and standardization a priority? REST is a better fit.
Evaluate Data Complexity:
For applications with complex and evolving data models, GraphQL excels.
For straightforward data models, REST suffices.
Consider Scalability:
Both architectures support scalability, but REST's stateless nature simplifies scaling horizontally.
Developer Experience:
REST is beginner-friendly with extensive documentation and community support.
GraphQL offers robust tooling and strong typing, reducing runtime errors but requiring a steeper learning curve.
Conclusion
Both REST and GraphQL are powerful tools for backend development, each suited to different scenarios. While REST remains a go-to solution for simplicity and widespread adoption, GraphQL shines in dynamic, data-intensive environments.
By weighing the strengths and limitations of each, you can choose the architecture that best meets your application's needs. Whether you prioritize flexibility, real-time capabilities, or simplicity, REST and GraphQL provide the foundation for building robust and scalable APIs.