GraphQL vs REST: 4 Benefits of GraphQL
Demand for GraphQL courses on the Udemy platform grew by 39x last year, making it the 5th most popular tech skill to pay attention to in 2018. Excitement around GraphQL has been building over the last few years, but is it warranted? GraphQL is a data query language developed internally by Facebook in 2012 to improve how data is retrieved and loaded on mobile phones. GraphQL was publicly released in 2015 and provides an alternative to REST and ad-hoc web service architectures.
What is GraphQL and why does it matter to your business?
Anytime you “like” a post on Facebook, or check out your latest messages from a friend on Snapchat, information gets exchanged between your device and a database connected to the internet. This exchange of information between your device and a database is frequently referred to as “data loading.” Virtually all mobile apps and websites use some sort of data loading procedure to make their content come to life.
Given the importance of this data loading function, engineers have developed several different systems to more efficiently and quickly transfer information. One of the currently popular systems, known as REST, has been used in tens of thousands of different products in the last decade, but it’s not a perfect solution. In particular, using REST to transfer data often results in trouble loading “relational data,” which can sometimes load more information than required, and imposes stricter requirements and limited flexibility for front end developers.
As a solution, Facebook developed a competing technique for transferring data between devices and databases called GraphQL. In recent years, this newer loading methodology has been adopted by hundreds of companies to solve issues with older REST approaches.
Here are some of the issues with REST, how GraphQL solves these problems, and some of the key benefits of GraphQL over REST.
4 Benefits of GraphQL vs. REST
1. Increased flexibility
REST became popular over the last decade due to its predictable implementation. An engineer who learns how to use REST at one engineering organization can usually transfer that knowledge easily to another organization. But this blessing is also a curse when it comes to flexibility.
One of the core ideas of REST is the practice of defining a very limited set of ways that a developer can interact with data. While creating standards, it also limits flexibility and slows performance. For example, if a blogging application implemented a REST interface, a developer would be restricted to interacting with blog posts in one of four (sometimes more) ways: retrieving a single blog post, retrieving a list of blog posts, updating a single blog post, or creating a single blog post. These methods compose the only way to interact with blog posts when following REST conventions. More advanced interactions, such as listing all blog posts a user has created and the number of comments for each blog post creates problems for REST and affects how quickly the data loads. GraphQL solves these problems by creating more flexibility for developers and allowing for more efficient loading of relational data (more on this below).
2. Better loading of relational data
GraphQL dramatically simplifies the process of loading relational data. An example of relational data might be a blog post that has some associated comments. The comments only make sense when applied to the blog post they were made on, or we might say that the comments ‘belong’ to the blog post.
GraphQL improves the retrieval of relational data by allowing developers to fetch multiple related pieces of information, such as a blog post and its related comments, in a single network request over the internet. In general, developers try to minimize the number of requests being made by an app to better accommodate users with lower quality network connections. In theory, there is no limit to the amount of information that can be retrieved in a single GraphQL request. A GraphQL server could share a list of blogs posts, comments that belong to them, the authors of each comment, and the total number of comments created by each author.
In a REST implementation, loading a blog post and an associated list of comments would be a two-step process. First, the app would have to make a request over the Internet for a blog post, then make a follow-up, or sequential request to fetch the related list of comments. This represents two separate requests being made over the internet which slows performance, especially on mobile devices.
3. Increased performance
Besides allowing a developer to more easily access related data in a single request, GraphQL also allows developers to reduce the size of each request—enhancing performance and speed. For example, imagine how a blog post is displayed to a user in a blogging app. The post might have a title, date of publication, and some amount of content—this is all information that the app would need to function properly. However, an application might also store many other pieces of information attached to that blog post, such as the number of views the blog post has, the topics it mentions, or even a list of related blog posts. These extra pieces of information might not be needed by the application at all to get the blog post to appear to the user, so if that information is communicated from the database to the app, more data is being shared than needed.
GraphQL allows developers to precisely define the properties they need to access on a particular record, such as a blog post. The benefit is clear: there is less of a chance of ‘oversharing’ data, and less data is transferred from a back end server to the user’s device, resulting in a performance gain.
This is in stark comparison to following REST conventions, which do not easily provide a developer the ability to tailor the amount of information that is served on each record.
4. Streamline front end and back end developer collaboration
Whereas REST implementations define a strict set of principles and conventions for fetching data, GraphQL allows front end developers to change the information they request on the fly to better suit the application they are trying to build. This can be done at the sole discretion of a front end developer with little to no input from a back end developer. In contrast, REST implementations traditionally require closer collaboration between front end and back end developers, which is commonly seen as a drain on productivity due to the need for one party to complete a programming task before the other can begin working on their feature.
Roadmap for implementation
Now that you understand the benefits of GraphQL, how can you implement this new technology in your organization and apply GraphQL to your next app? What kind of skills will your developer team need? What steps should you take to implement GraphQL? Here are some key steps you’ll need to take.
Train your developers on GraphQL. Udemy and Udemy for Business offer a collection of GraphQL courses that will get your development team up and running with this next-gen interface. My course GraphQL with React: The Complete Developer’s Guide can help you and your team get started on how to implement GraphQL at your organization.
Focus on developing the back end first. Once you’ve picked out a GraphQL implementation to follow, you’ll want to focus on developing the back end experience of your app first. Traditionally, the development of an app that uses GraphQL starts on the back end, to better allow flexible access to information stored in the database.
Move to the front end next. Once the back end server has integrated GraphQL support, move your engineers on to the front end. You’ll find that your front end developers can move along at a vastly improved pace, as they can more flexibly define the information required to implement each feature in your app. Gone are the days of front end developers waiting weeks for a database engineer to implement a particular interface.
If you get stuck along the way, remember that there are countless sources and guides available to help you get started with GraphQL.
GraphQL: Safe and solid bet for any project
Although REST has served us well for many years, its many limitations have made GraphQL more of a joy to work with. By using GraphQL, you’ll have an easier time working with relational data, enjoy improved performance, and increase your front end developer productivity. Thanks to the support of Facebook and the open source community, development of GraphQL has been evolving over the years—making it a safe and solid bet for any new professional project.
Top courses in GraphQL
GraphQL students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.