01 Nov GraphQL, solving REST’s problems
GraphQL is a specification created by Facebook on 2012 and open sourced in 2015. It defines a way of implementing APIs to enable more powerful and flexible usage.
What problems is it trying to solve?
REST provides an easy way to expose business logic to clients. By publishing concepts as entities with GET, POST, PUT and DELETE operations, clients can interact with the server following these commands. However, several problems appear under this approach:
– The rigid structure of endpoints commonly ends in over or under fetching data.
– Multiple calls to endpoints may be needed to perform operations on several entities.
– REST principles are broken as a result, deriving in a fragile and monolithic code base.
How does it solve these problems?
A GraphQL sever exposes a single endpoint, to which the clients can send queries for the data they need. This means that the client has the flexibility and the power to request the specific information they need of any number of entities with only one request. The backend has the power to resolve each query in standard or custom ways.
When and where can we use GraphQL?
GraphQL is meant to be a thin layer between clients and business logic. This means that it can be used in new projects or in existing projects. Incremental adoption in an existing stack is easy and can support interacting with legacy systems.
What about GraphQL clients?
A GraphQL API can be consumed from virtually anywhere, all major languages have a stable client implementation, each one of them leveraging the best of each language.
One of the biggest and most popular clients is Apollo, which can be used in React, React Native, Vue, Ember, Angular, Meteor, Swift and Android. Developers can add features to their apps that were previously very hard to implement. Optimistic UI, prefetching and caching are just some of the features that Apollo enables out-of-the-box.
GraphQL is a powerful tool that can benefit both client and server. Solving major pain points in client-server communication and greatly accelerating development times, it’s a technology that should be analyzed in all projects when defining architecture.