Why use GraphQL?
GraphQL is hot right now. It’s a promising technology – adopted by major players such as GitHub, Shopify, Netflix and The New York Times. Venture capitalists seem equally bullish on its future promise, with Apollo recently raising $22 million for its suite of developer tools for GraphQL.
But is the hype justified? Is it better than alternatives (namely REST)? And, is it right for your product?
What is GraphQL
GraphQL stands for “Graph Query Language.” It’s an open-source backend language for writing data queries, data manipulation and API development, coupled with a runtime for fulfilling these queries. It’s similar to REST, but offers a host of advantages that we’ll explore in this post.
It started at Facebook. Back in 2011, engineers at the social media giant weren’t happy with the performance of their mobile app. Facebook engineers came to the realization that existing REST API technology didn’t offer the performance they needed, so they looked to develop a tool that overcame its limitations. Based on their learnings they developed GraphQL and publicly released it as an open-source project in 2015.
Why use GraphQL? From a technical perspective
As the numbers of API endpoints required by modern applications increases rapidly, GraphQL offers an alternative to REST API technology.
It negates the need to write additional server-side logic and make multiple queries to the database. What does that mean exactly? Faster, more efficient applications!
GraphQL can significantly reduce the number of queries needed to fetch data, decrease the amount of data transferred and therefore increase application performance.
It offers a flexible and efficient approach to API design. Data can be fetched in a single request, and only the data needed will be returned via the request.
In order to use GraphQL on the client-side, a GraphQL server must be setup that can interpret these queries and fetch the necessary data. But it’s not tied to any database, meaning it can easily be retrofitted to existing tech stacks.
GraphQL vs REST
The beauty of GraphQL is that it only fetches the data that is needed to complete a request. This improves performance relative to REST APIs that upon each request, fetch more data than is required (known as “over-fetching”). Since less data is transferred by GraphQL requests, there is a corresponding increase in performance, as queries are made more efficient by only fetching the necessary data.
While this is particularly important for applications running on a lower bandwidth connection, GraphQL API technology can speed up these apps through greater efficiency, regardless of connection speeds.
Another common issue with REST APIs is under-fetching, where not enough data is available to complete a request from an endpoint. In this case additional endpoints may have to be called (and created, if they don’t already exist!) – which is another surefire way to slow down your application, as well as your development cycle.
Additionally, since it’s easy to view analytics on which data is being requested with GraphQL, unused fields can easily be identified and deprecated – resulting in cleaner databases.
Why use GraphQL? From a business perspective
Faster applications, particularly for users with low-speed connections provide a better user experience. Speed is crucial, the latest research from Google finds that “as page load time goes from one second to 10 seconds, the probability of a mobile site visitor bouncing increases 123%.” Scary stuff.
Improves business agility
Instead of creating new endpoints each time new fields are required, GraphQL developers can add those new fields to pre-existing Graph architecture. This reduces development time and increases the speed and efficiency that businesses can update their applications. GraphQL is a great solution for rapidly changing business requirements.
Frontend changes don’t require work on the backend
With typical REST API endpoint design, changes to the frontend typically require corresponding backend tweaks. With GraphQL, frontend views don’t need to be structured around endpoints. Backend changes aren’t required for tweaks to the UI (as long as data is incorporated in the Graph architecture) as client queries can simply specify which data needs to be fetched from the backend.
So, should I use GraphQL?
GraphQL is an elegant and modern solution to API design and development. It offers many advantages over traditional REST APIs. And despite the reactionary updates we’ve seen to REST, GraphQL still has many advantages.
While it’s not recommended to panic and throw away existing REST APIs, the improvements in efficiency and performance offered by GraphQL make it a compelling choice on which to build new applications.
And for existing applications, if the business case for increased efficiency is justified, re-architecting may make sense if the jumps in performance and ease of future iterations outweigh the time and cost of the effort.