DEV Community

loading...
Cover image for Why Rest api's are Good and Graphql api's Better

Why Rest api's are Good and Graphql api's Better

ayeolakenny
Software Engineer • Full stack Developer • Reactjs, Typescript and Graphql Enthusiast
・2 min read

GraphQL was internally developed by Facebook in 2012 and was later open sourced in 2015. The idea came up when facebook needed to rebuild its mobile native application, Facebook needed a data-fetching API powerful enough to describe all of Facebook yet simple enough to be easy to learn by their developers, now graphql powers tech giants like Netflix, Twitter, Shopify etc.

Now wondering what it is about graphql that outweighs rest, well lets get on with it.

Data Fetching

Graphql and Rest data fetching
Data fetching is one of the obvious reasons we use api's, graphql makes data fetching easier by only exposing one endpoint to access data from the server, unlike rest where we have to make multiple requests to multiple endpoints.

Network Requsts

Graphql and Rest network requests
As said earlier, Graphql exposes only one endpoint which reduces trips and network requests to the server to fetch data, while Rest exposes multiple endpoints and increases trips and network requests to the server.
As you can see in the image above, to grab a users profile, users posts and users followers with graphql we only make a request to the server while we make 3 requests to the server using rest apis to grab all the data we need.

Over/Under Fetching

  • Graphql is a declarative data fetching specification and query language. Graphql fetching we only fetch what we need from the server by constructing our query to only include what we need.
  • Rest apis gives you more or less data than is needed for the client at a point. Rest fetching This makes the server spit unecessary data that would not get used by the client, therefore making uneccessary requests.

Api Versioning

When the arise for addition of new features come in, you can easily add additional fields, queries, mutations etc to the server and other parts would not be affected, also it is easier to remove old features and make them backward compatible without any breaking changes.

Should I Abandon REST for GraphQL?

Graphql can be a life saver when when building scalable applications, but you do not need to rewrite or migrate applications that have been designed to use REST api. Rather you should plan to use GraphQL for future applications.

Conclusion

REST for sure is not going anywhere soon. For most small applications, there would not be significant increase in performance over REST, the real magic of GraphQL plays in as applications scale and the application becomes bigger.

Discussion (2)

Collapse
bolt04 profile image
David Pereira

In my opinion, GraphQL APIs aren't better or worst than RESTful APIs, they are a different design choice. We decide to choose one over the other depending on the problem and use cases we have 🙂.

Having that said, I think if your domain model is like a graph with tons of relationships, GraphQL is a good choice.

If you want, check out OData for RESTfull APIs and hypermedia (e.g. HAL-JSON) for ways to design your API that help the client fetch the data it requires, in a similar way GraphQL allows you 👍

Collapse
ayeolakenny profile image
ayeolakenny Author

Yh i agree with you, didn't know there was a way to fetch data that you only require in REST, ill check that out, cause thats my main pain point with rest, thanks.