GraphQL Vs RestAPI

Abhishek Rathore
3 min readOct 31, 2021

GraphQL is a JSON-like query language for APIs as well as a server-side runtime for executing your queries. Instead of working with predefined server-defined endpoints, you can send queries to server to get exact data you’re looking for in single request. In GraphQL, the client has complete control on what data the API need to return.

GraphQl vs REST -Structure
If you are working on RESTful architecture, the endpoints might grow over time and maintaining every version is a very difficult task. But with GraphQL, you just has a single endpoint which client queries eg: Https://graph.MovieDB.com

In using GraphQL instead of calling multiple REST endpoints to fetch related data you can make single query to API and get the response in required format. To better understand lets look at key components of GraphQL

Schema

Schema describes the data which could be returned by GraphQL endpoint. Schema lets the API consumer know the functionality available for the clients to consume i.e. what data they can expect and the actions they can perform.

Type
Notice below the type which is exposed will be exposed by API but this description doesn’t tell you anything at all about how to fetch this object from the Graph API. That’s one core difference between REST and GraphQL — the description of a particular resource is not coupled to the way you retrieve it.

type Project
{
name: String
tagline: String
contributors: [User]
}

Query
GraphQL queries always return predictable results. Query allows client to control the data they get in response from server.

Notice below sample query which is requesting server to return tagline for the project with the name “GraphQL”

{
project(name: “GraphQL”) {
tagline
}
}

So for above query server returns a predictable and exact response which client need eg:

{
“project”: {
“tagline”: “A query language for APIs”
}
}

GraphQL vs REST -Performace
GraphQL is faster than REST because as you can pick the fields you want to query, so the response from server will always be the smallest possible. Additionally, with GraphQL, you can enquire multiple entities in one request, and because less bits will be transferred over the wire so response from API will be faster than using REST.

GraphQL vs REST -Caching

Since REST implements different url for every resource implementing caching is easy but GraphQL uses single endpoint so developers needs to inspect the request body and needs to be extra cautious while implementing cache logic.

GraphQL vs REST -Security
In terms of GraphQL vs. REST security, REST provides several inherent ways to enforce the security of your APIs.

For example, you can ensure REST API security by implementing different API authentication methods, such as via HTTP authentication, where sensitive data is sent in HTTP headers, via JSON Web Tokens (JWT), where sensitive data is sent as JSON data structures, or via standard OAUth 2.0 mechanisms.

While GraphQL also provides some measures to ensure your APIs’ security, they are not as mature as those of REST. For example, although GraphQL assists in integrating data validation, users are left on their own to apply authentication and authorization measures on top. This often leads to unpredictable authorization checks, which may jeopardize the security of GraphQL-based apps.

GraphQL vs REST -API Versioning
GraphQL does not support API versioning since it is a single endpoint while with REST versioning could be easily implemented

Conclusion

Both REST and GraphQL APIs are just different styles of exposing service/function over network. If you’re familiar with building a REST API, implementing a GraphQL API won’t feel too different. But GraphQL has a big leg up because it lets you call several related functions without multiple roundtrips.

--

--