REST vs graphQL - Which is better?
GraphQL is growing in support and popularity. Many apps, cloud providers, payment platforms, continuous integration and delivery, e-commerce solutions, CMS, and a myriad of other services are providing GraphQL support, in addition to the normal REST API setup.
But what exactly is GraphQL? Why does it matter? and how is the development process different from REST?
A concise introduction to GraphQL
Contrary to popular misconception, GraphQL isn't a database. Rather, it is another query language standard and structure that interfaces with the database to build APIs. It is a syntax system that describes how to ask for data. GraphQL also gives client-side development more power by being elastic in the data and data types it can call based on requirements.
The major perk of GraphQL is that it reduces the load on backend development, allowing the process of creating apps to be more agile in its ability to respond to change. In contrast to REST, where each API requires independent coding and configuration, GraphQL is dynamically produced based on the parameters requested by the client-side.
The idea behind GraphQL is simple - instead of having 'dumb' endpoints or APIs that are static in structure, GraphQL presents a 'smart' endpoint architecture that is able to fetch the necessary data based on a set of instructions.
How is this different from REST parameters?
REST parameters require configuration and filtering on the server-side by the backend developer. GraphQL, however, shifts the workload over the client-side but without the need to directly deal with the database. The data fetched is determined by the parameters given by the API's consumer and the GraphQL setup determines how to retrieve and return the required contents.
Is REST going to get phased out?
Before REST, there was SOAP. But SOAP had security issues and quickly died when REST came along. When it comes to GraphQL vs. REST, the battle between how data is fetched, formatted, and transferred between frontend and backend is not as clear cut as it was when the conversation was REST vs. SOAP.
REST is an industry staple. It is easy to understand and has widespread adoption. Using GraphQL is not hard. Implementing GraphQL in the backend from scratch, however, maybe a bit more challenging. This is where frameworks come in.
In node.js' ecosystem, there are two major GraphQL implementation frameworks that you can check out to help reduce development time and they are
- GraphQL-JS - the original implementation of GraphQL in JavaScript and can be used with express-graphql to create an API server, and,
- GraphQL-Server - which is created by the Apollo team and is well documented.
If you're looking to get started with GraphQL, it is a good idea to learn how to consume it first before you try and build the server for it. This is so that you properly understand how it works and knows how to deal with any nuances that you may encounter. To do this, here are 6 free GraphQL APIs that you can play with to get started.
6 Free public GraphQL APIs
Contentful
Contentful is an API-driven content management platform that lets developers connect to a pre-built interface. The idea behind Contentful is that developers can create content structures easily for non-developers to use. The API offers both REST and GraphQL, meaning that you can explore how both are implemented.
Contentful's GraphQL documentation is extensive with implementation tutorials for major languages such as JavaScript, PHP, Java, Python, Ruby, and .NET. It also includes how to implement the platform's API for Android and iOS.
GitLab
GitLab is a DevOps platform built for continuous development, integration, and deployments. Through GitLab, you can create simplified pipelines dealing with the entire development and deployment flow. By design, APIs consumption is expected to be part of the workflow for deployments through hooks, configurations, and data fetching.
GraphQL in GitLab solves the issue of dumb endpoints and extends the flexibility boundaries for developers. GitLab comes with an extensive GraphQL API resource list that lets you query a vast amount of things, making it good practice for implementing CI/CD projects.
Shopify Admin
Shopify is massive. With more than one million stores on the platform, there is a large market of opportunities for developing extensions, plugins, and themes.
GraphQL is an established method of retrieving and sending data between external third-party apps and the shopping platform. The GraphQL Admin API reference covers data and triggers that can be leveraged for your third-party apps.
Yelp
Yelp is your ultimate online directory that thrives on community-driven reviews of businesses. This means that there is a wealth of public data available for you to use. Yelp's implementation of GraphQL makes it easier to tap into this data source for your extensions and apps.
The guide gives an overview on how to use GraphQL, along with rate limits, localizations, and how to make requests. Unlike GitLab where you can construct your own pipelines with GraphQL, Yelp's implementation is geared towards retrieving data only.
Countries API
Countries API is a free API that lets users tap into a highly organized set of data. It covers data like country code, languages, area, currencies, capital cities, and regions. In a single JSON file, Countries API gives all the potential data nodes that a developer can use to create GraphQL calls. It also comes with examples on how to implement queries.
GraphQL Pokemon
Gotta catch 'em all! With the new generation games released, along with the series still going strong, creating a Pokedex might just be the right and fun side project for learning GraphQL.
GraphQL Pokemon is an open-source (and unofficial) Pokemon-based API project. The source code and data are also available on GitHub. This means that you can also see how GraphQL can be implemented on your own server.
Wrap up
GraphQL is an implementation of an idea to make retrieving data much easier. It is also a standard that encourages less work for backend developers and ultimate freedom for the frontend. If you haven't already tried it out, the GraphQL APIs listed above should give you a good kickstart in the right direction.