Rest vs. GraphQl

Rest vs. GraphQl
Techiio-author
Written by Sagar RabidasNovember 22, 2021
12 min read
GraphQL
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

Today we will discuss between Rest and GraphQl.

About Rest:-

Rest is an acronym for representational country transfer and an architectural fashion for allotted hypermedia structures. Roy fielding first presented it in 2000 in his well-known dissertation.

Like different architectural patterns, the Rest has its guiding standards and constraints. Those concepts need to be happy if a provider interface wishes to be referred to as restful.

blogpost

About GraphQl:-

Graphql is a question language (that’s what the “Ql” stands for) for APIs and a runtime for pleasurable the ones queries together with your current records. It's far used to load information from a server to a customer -- it’s a manner to get statistics from an API into your utility. And as you’ll see, it does this in a much more efficient way than traditional strategies and offerings.

blogpost

Rest vs. GraphQl

Endpoints

With REST API, we typically look at an endpoint for each request. This would be for every query, whether that is a Select, Insert, Update, or Delete. Each endpoint represents a different call to the Database. This is not the case for GraphQL. Instead, GraphQL implements only a single endpoint. All calls are made through a single endpoint.

This is not to say that GraphQL is unable to Select, Insert, Update, or Delete. All of these types of calls may still be made. However, it is all done through a single endpoint. There are then two separate “Types”, which are query and mutation. These allow you to do exactly as they say. Either you can query, or you can mutate, which could be an Insert, an Update, or a Delete.

Structure

With a REST API, we know that data is organized in JSON format. With GraphQL, we use a graph structure instead. These nodes are more schema-defined than an object. This graph format is more custom to GraphQL, as it creates its custom structure. This allows for greater flexibility, as there isn’t a de-facto format like there is with REST.

With REST, the size of resources is determined by the server. In GraphQL, by contrast, a server allows clients to request data at more specific times, which ultimately determines what resources are available and when they are available.

Because GraphQL uses only a single endpoint, relationships are assigned based on the schema your graph is set up in. This means that a relationship between tables is established instead of using different endpoints, because it may only use the single endpoint.

Solving Querying Issues

As previously mentioned, GraphQL is made to be flexible. It can therefore return a more customized subset of data to better reflect what information is required. What this means is to first take a database and fetch it with a REST API. If we’re needing several tables worth of information, we would need different requests. Further, if we needed a specific column from a table, we would need a call to get that information as well. While we develop, we see that we have to put that data into an object, so there may be unnecessary data that is saved into that object. With GraphQL, fetching data is more custom and has more freedom. It could limit the number of calls required, or even keep the calls at only one. If certain columns are desired, those columns could be gathered without needing to save others in an object. In theory, this could then speed up your API calls.

There are a few API issues that REST may pose. One is Over-fetching. The example is like what was given above, where only certain data is needed. GraphQL avoids over-fetching data by allowing the information to be specifically requested, therefore only getting what is needed.

The next is Under-fetching. Again, like mentioned above, this would be where you need multiple requests to gather the information you need because making a single request would not return everything needed. The flexibility of GraphQL ensures you can get the data you need with fewer calls, potentially only a single call, because of how customized it is.

An issue that could potentially be resolved is the issue of speed. Theoretically, if you are requiring fewer calls, and if you are pulling less data than entire tables, your queries should run faster.

Another slowdown could come from development. If a new table is needed, with a REST API you would write your function to gather the data, build the object, but you would also have to then create an endpoint to handle that “GET”. With GraphQL, you don’t need to create that extra endpoint, because we only work with one. Even if only a little, it does speed up the development process.

Scalability

Although maybe not a large factor, being flexible allows for much easier scaling. Being able to make alterations to schemas or queries without harming the existing schemas/queries allows for a much easier implementation when making data larger. This means that changes leave a minimal impact on the current architecture, which is more encouraging when making large changes. Keeping in mind that resourcing is also better dealt with by the server, it is also very helpful that GraphQL does typically speed up requests. Being so flexible allows for further growth. It is also faster to write, as there would not be as much code to change. With the graph structure, it is readable even to non-technical users, making it easier for others to review your code and understand it.

With GraphQL growing in popularity, documentation and support are also growing. This makes finding answers or even helping to resolve issues easier. Although REST allows for the same, it is still promising that the same support can be found for GraphQL.

Conclusion

All being said, there are a few drawbacks to GraphQL. One major drawback is the learning curve. While with REST you can typically jump in even as a beginner, GraphQL takes more research to understand. I read several articles, and still, I’m thinking I need to understand just a little better before I give it a go.

GraphQL is still growing, and so hasn’t perfected all of its flaws. But so far, it offers more flexibility than REST, and also fixes some of the drawbacks, such as under-fetching or over-fetching. GraphQL also uses a strong “Type” system in its format and uses a graph instead of JSON.

With REST, each request uses a different endpoint. However, with GraphQL, you use only a single endpoint. Further architecture differences occur, such as how the server will determine resources.

In the end, I think whether to use, GraphQL or REST is more up to you and your ambition. GraphQL seems very interested, granted there is quite a learning curve. I think GraphQL has wonderful potential to be used in some of my projects to optimize the requests. That being said, this may not be the case for everyone. As I’ve said in other posts, you know your project than anyone else does. But I would encourage you to do a little research on what GraphQL can be used for to see if this challenge is for you. For now, I’m very interested in learning more about GraphQL, so be on the lookout for another more in-depth post about it. Until then, cheers!

rest
graphql
rest vs graphql
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Sagar Rabidas
Software Developer
Techiio-followerTechiio-followerTechiio-followerTechiio-followerTechiio-follower
+8 more
300 Blog Posts
14 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance