DEV Community

Cover image for 5 Reasons to use GraphQL at Your Company
Kentaro Wakayama
Kentaro Wakayama

Posted on • Originally published at codersociety.com

5 Reasons to use GraphQL at Your Company

GraphQL is on the rise. Companies like Facebook, Netflix, Shopify or PayPal are using the data language and API technology to drive their products. Learn in this article, why you should be using it at your company.

This article was originally published at Coder Society.

The Rise of GraphQL

What is the best way to build an API today? REST probably comes to mind, but if you’re going to make the investment to build new software, it’s probably worth considering a few different options and choosing the best among them.

GraphQL stands out as an alternative to the REST API architecture mainly (but not only) because it provides a discoverable API by design. It also comes with its own query language and a runtime for fulfilling queries via functions called resolvers.

Originally developed in 2012 at Facebook as a better data-fetching solution for underpowered mobile devices, GraphQL was open-sourced in 2015. In 2018, it was moved under the care of the Linux Foundation, which maintains other important projects like Node.js, Kubernetes, and, of course, Linux itself.

The general movement around GraphQL is very encouraging for anyone looking to adopt it. Its popularity has been rapidly on the rise over the last few years, as seen on Stack Overflow Trends, for example. There are also several success stories at reputable companies such as PayPal, Netflix, and Coursera, where GraphQL was instrumental in building flexible and high-performant APIs in large, complex architectures.

However, given the dynamic technology landscape in which we operate today, you would be forgiven for being skeptical. Could GraphQL be another fad? If it works for these companies, does that necessarily mean it will work for you? Let’s discuss the benefits and challenges of GraphQL, so that you can make an informed decision.

Reasons to Use GraphQL

As an API technology designed for flexibility, GraphQL is a strong enabler for both developers and consumers of APIs, as well as the organizations behind them. In this section, we’ll explore some of the key areas where GraphQL shines. 

1. One Data Graph for All

GraphQL is an excellent choice for organizations with multiple teams and systems that want to make their data easily available through one unified API.

No matter how many databases, services, legacy systems, and third-party APIs you use, GraphQL can hide this complexity by providing a single endpoint that clients can talk to. The GraphQL server is responsible for fetching data from the right places, and clients never need to know the details of where different pieces of data are coming from. As a result, the GraphQL ecosystem provides maximum flexibility when it comes to making data easily available for customers and internal users.

2. No Over-Fetching or Under-Fetching

Another huge benefit for GraphQL API clients is that they can request exactly what data they need, even across related entities. This is especially important because different clients have different data requirements, either because of a different business logic or because they are simply presenting a different view of data (e.g., web vs. mobile) and may also have different hardware limitations.

By way of comparison, it’s much harder to efficiently retrieve nontrivial data from a REST API. Requesting data from a single endpoint will often return more data than is actually needed (overfetching), whereas requesting data about several related entities usually requires either several API calls (underfetching) or dedicated endpoints for specific client requests (which duplicates effort). GraphQL solves this issue by serving exactly the data which each client requests, nothing more and nothing less.

3. Better Developer Experience

The GraphQL ecosystem comes with a number of tools that make working with GraphQL a breeze. Tools such as GraphiQL and GraphQL Playground provide a rich experience, allowing developers to inspect and try out APIs with minimal effort, thanks to the self-documenting features which we will get to in the next section.

Also, code generation tools like GraphQL Code Generator can be used to further speed up development, while other tools and best practices exist to address specific problems including:

  • Client-side caching is available out of the box in several client libraries.

  • Cursor-based pagination provides a way to offer pagination across lists of data.

  • The DataLoader improves performance by batching data fetch requests and also provides a basic level of server-side caching.

4. Higher Quality of Your System

GraphQL APIs are built around a type system, which lays out the name and type of each field as well as the relationships between different entities. This type system, or schema, is used to validate queries sent by the client. The schema can be queried via a feature called introspection, which is often used to generate documentation and code that will be used when integrating the API on the client-side.

As a result, it requires minimal effort to have a well-documented API when using GraphQL. This provides great transparency to developers who are working with an API for the first time and makes development smoother and more efficient.

5. Build for Change

It is common for REST APIs to provide multiple versions of the same API so that it can change without breaking the existing functionality. GraphQL encourages a different approach to API modifications: evolution.

When breaking changes are required (for instance, renaming a field or changing its type), you can introduce a new field and deprecate the old one, possibly removing it completely later on when you’re sure it’s no longer being used. This means that you can still change your API while maintaining backward compatibility and a single API.

Considerations Before Adopting GraphQL

GraphQL is an excellent tool to build scalable and flexible APIs, but it is not a panacea and is certainly not for everyone.

Learning Curve

Whereas REST is a simple and familiar approach to building APIs, GraphQL is a different beast altogether. Developers and infrastructure engineers alike will need to learn how to effectively develop and deploy GraphQL APIs, a task that will take some getting used to.

As a result, teams that are on a tight schedule are probably better off using a technology with which they’re already familiar.

Infrastructure and Tooling

Deploying GraphQL, especially at scale, can require significant investment in infrastructure and tooling. Using it does not save you from having to deploy virtual machines or containers, set up a networking infrastructure, and deploy and maintain GraphQL server software across a large environment.

Performance and Security

You also have to be extra careful that the additional flexibility afforded by GraphQL does not result in queries that maliciously or accidentally degrade or take down your system. This can be addressed by rate limiting or limiting query complexity and depth.

Finally, it is always important to protect data that should not be public. Authentication and authorization mechanisms that are popular among other web technologies can also be used with GraphQL. Plus, pay attention to introspection, as it can leak internal types if not correctly secured.

Conclusion

There is no doubt that REST gets the job done, but if you’re at a point where you need a better way to build APIs and serve diverse clients, then you should probably give GraphQL a try.

GraphQL allows you to build evolvable and queryable APIs, hide the complexity of internal systems used to retrieve various pieces of data, and leverage a type system that results in automatic and up-to-date API documentation. These features, along with its tooling and ecosystem, make GraphQL an efficient and effective tool for API and client developers alike.

Although GraphQL does require some investment, this is far outweighed by its advantages in situations where there are lots of data and services that should be made accessible to various existing and future API clients.

For our latest insights and updates, follow us on LinkedIn

Top comments (0)