DEV Community

Shariq Ahmed
Shariq Ahmed

Posted on

GraphQL vs Rest: A Brief Overview

In order to help in the design and development of varying architecture, Dr Roy Fielding designed RESTful API of REST in 2000. But what actually is REST? For starters, REST stands for REpresentational State Transfer. Further, whenever RESTful API is called, all the representation of the resource will be given. Frankly speaking, it’s just another way of saying that a server will react to your requests.

For instance, imagine someone calls Facebook API of Ronaldo’s account. What would the API give in result? Well, it will return the state of the user as well as other information as well. Now, keep in mind that no matter what happens, you will see the state in JSON, XML and HTML format.

But why am I talking about this? Well, today I’ll talk about the difference between Rest API and GraphQL. Before that let’s tell you briefly what GraphQL actually is. It’s a query language as well as architecture that’s used to manipulate APIs.

REST is best when resources are well-defined. GraphQL, on the other hand, is best for large, complex as well as interrelated data sources. It also has only one URL endpoint. But REST has varying data points to define resources. Further, GraphQL returns data in a flexible structure. REST returns data in a fixed structure defined by the server.

REST is also weakly typed. But GraphQL is strongly typed. This means that the client can receive data in a format that’s mutually understood. In REST, clients must see if the returned data is valid by themselves. Fortunately, GraphQL takes care of this at the very first stage. Invalid requests are rejected by schema structure. When it comes to speed, GraphQL is faster. All thanks to efficient caching as well as reduced over-fetching and under-fetching.

Further, GraphQL is self-documenting. This means there are no comments, nothing. This is bad. Because again, what if someone doesn’t have any knowledge of these APIs but wants to learn them? Maybe that’s because REST is considered easy. Its learning curve is also moderate.

In case anyone wants to learn it, the best thing is that REST community is also large. Unfortunately, that’s not the case with GraphQL. It’s considered difficult. Further, the architecture of GraphQL is client-driven. REST, on the contrary, has server-driven architecture. GraphQL is organized in terms of schema and type system. But REST is organized in terms of endpoints. Fetching in GraphQL is done with a single API call. In REST, data fetching is done using multiple API calls.

All in all, you see, both GraphQL and REST looks the same. But they are actually not. GraphQL is best when one wants control of data fetching. But the thing is at times, GraphQL does complicate tasks. For instance, in an app that uses few fields, when one use GraphQL then complexity can be added because of types, queries, and other high-order components. REST can do more of what GraphQL is currently doing.

Top comments (0)