Pros and Cons of GraphQL for Developers
In this blog, I explain the reasons for using GraphQL as well as the challenges developers may face when using it. GraphQL is a query language for APIs. It’s rapidly becoming an alternative to REST, but it also comes with certain challenges, such as those which developers may face in the area of observability.
GraphQL is not necessarily a replacement for REST but rather a different development approach. It was developed primarily because numerous mobile clients were not easily supported on the front-end by REST. The many advantages of GraphQL make it a tool of choice not only for front-end developers but also for back-end developers. GraphQL brings improved interaction between the client and the application front end. The same improved interaction benefits back-end developers when dealing with microservices communication.
The adoption of GraphQL started at large companies with strong development teams who were able to deal with GraphQL challenges successfully. On the other hand, the simplicity of REST compared to GraphQL could be an advantage for developers in smaller companies.
The Reasons for Using GraphQL
Below are the main GraphQL benefits:
Better Efficiency. A single GraphQL query can be equivalent to multiple REST calls. Such capability increases the efficiency of the application. Fewer calls mean faster response times and better user experience. Also, the decrease in the number of calls results in less network traffic.
Less overhead. GraphQL allows clients to ask only for the fields they need, which results in less work for back-end code. Security increases too because there is no unintentional leak of data. That’s because the client gets only requested data.
More Client Types. GraphQL provides a layer of abstraction for the web and mobile clients when communicating with microservices. Think of it as an API gateway between different types of clients and microservices. The capability of GraphQL to serve various clients such as web, mobile, IoT was one of the first goals the creators of GraphQL wanted to achieve. REST does not easily support numerous types of clients.
Easier Error Detection. The beauty of the GraphQL query is that it reveals unexpected API changes. Try the query interactively in manual mode, and you would know if APIs behave the way you expect. You might not even need to read API documentation in some cases. You would be safe, nonetheless, because you can check GraphQL queries on your own before implementing it in the code. The capability to verify queries before deployment into production saves valuable development time and avoids costly failures.
Increased Productivity. The simplicity of GraphQL’s syntax makes it easier to read and develop the code. The syntax improves the developer’s experience and results in increased productivity. Sometimes GraphQL development involves creating code which is not the same but quite similar. Schema duplication is one of the examples of code replication. As a result of schema duplication, developer productivity may decrease, but luckily some tools can help. For more information, how to avoid schema duplication look at GraphQL schema generation tools such as PostGraphile and Prisma.
More Troubleshooting Insights. GraphQL can retrieve the properties of APIs, which can accelerate or improve troubleshooting. For example, APIs may be developed or maintained to include the name of the developer or the owner of each API. Once monitoring tools start reporting failures of the specific API, the developer or the owner can be quickly put on call. The ability to reach the developer or the owner of the API is of the utmost priority for the organizations with a large number of developers and demanding SLAs. Each minute lost for such organizations may result in penalties.
Better Database Calls’ Performance. Use Dataloader with GraphQL to avoid unnecessary calls to your database. Dataloader can batch and cache database calls. In reality, it’s hard to achieve perfect caching and batching, but using Dataloader can significantly improve performance.
The Challenges of Using GraphQL
Complex Observability. Replacing multiple REST calls with the single GraphQL query increases the efficiency of API calls but makes monitoring much harder. It’s not always straightforward to pinpoint the reason for the failure of the GraphQL query. That’s because the GraphQL query may be an equivalent of multiple REST calls. It’s often hard to pinpoint which element of the GraphQL query failed. One solution may be to compare the same queries over time. But that’s not always possible because each query may be unique. Figuring out the right way to monitor GraphQL is the area where the creativity of the developer is crucial. And it’s not only about the capability of a developer to understand the issue. It’s also important for the developer to choose the appropriate observability tools with the ability to analyze metrics, logging, and traces.
HTTP Caching. GraphQL is HTTP agnostic and doesn’t take the full advantage of HTTP caching. GraphQL is more suitable for dealing with data that frequently changes than data that can be cached. REST effectively uses the caching concept and can be the right choice if HTTP caching is important for your application.
Asynchronous Loading. GraphQL is not the best choice when resources requested by the query are intended to be loaded asynchronously to improve the performance of the application. In that case, by aggregating interaction with these resources, GraphQL can’t take advantage of the asynchronous nature of the resources.
Because GraphQL is a query language for APIs, the number of GraphQL use cases is limited only by the developers’ imagination. Also, keep in mind that different GraphQL implementations may bring even more functionalities than outlined here. Choosing wisely different GraphQL implementations can help you with the way you develop your projects and how your customers perceive the quality of your product.
GraphQL comes with many advantages, such as increased performance and capability to serve different types of clients. Also, it increases development productivity and allows easier troubleshooting of ever-changing APIs. GraphQL is not necessarily a replacement for REST but rather an alternative to it. Observability and HTTP caching can be challenging, and developers should be prepared to look for ways to deal with these difficulties or choose REST instead.