As a part of our internal tech talks, the team gave a presentation named “GraphQL, solving REST’s problems”. GraphQL is a mature open-source specification meant to improve client-server communication. It adds powerful features to the stack, allowing fast development of previously hard-to-develop functionalities.
The talk included an explanation on how to integrate this technology into new and existing applications, highlighting advantages and analyzing possible pitfalls. With this knowledge on hand, now we can use GraphQL to enhance and give more power to our projects.
What is GraphQL?
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.A GraphQL API can be consumed and served from all kinds of platforms: Javascript, Node.js, .NET, Ruby, Scala, Go and much more. The GraphQL ecosystem is strong, so it’s easy to find a mature and stable implementation for almost all tech stacks.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 back end 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.
Conclusion
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.