We Stand With Ukraine
Read full statement
menu

Why Use GraphQL as an API Gateway?

October 31st, 2022

Evgenia Kuzmenko KITRUM Brand ManagerEvgenia Kuzmenko

APIs allow you to connect any services you need without hiring developers.

According to the survey, 40% of big companies utilize over 250 APIs, and 71% of developers plan to use even more in the nearest years. API economy has already made a lot of buzzes, and in 2020, $2B was invested in API companies. All of this shows that more and more companies will use more and more APIs. It’s about time to learn which tools give you the most to manage APIs you use in your technical product. 

In highly loaded systems, such traffic is redirected to the API Gateway, a management system that is a common entry point in modern applications running through the API. Moreover, it can be both monolithic applications and applications based on microservices.

API gateway is a standard tool to manage an API architecture. But there is a new instrument that can replace it in the nearest future. GraphQL is a data query language developed by Facebook in 2015. Such companies as Airbnb, GitHub, Netflix, Shopify, Twitter, and many more use it. GraphQL also powers billions of API calls daily at Facebook. Now it is an open-source data query that can design single endpoint access to fetch data from a number of sources. 

Source: LoginRadius

How does the API gateway work?

Before getting into the details of how GraphQL can substitute API gateway, let’s first find out how API gateway works and why you need it. Thus we will have the notion of one technology and compare it to the other. 

API gateway is located between various backend services and a client. It manages and controls access to your APIs. It gets an API request and sends back the answer. With the help of an API gateway, there is no need to track each service and send individual requests to each API. All the necessary information the application needs can be returned with a single request. API gateway frees you from the necessity to create routing rules and custom code for access to your API performed by different clients in multiple ways. 

API gateway executes authentication, real-time metrics, rate limiting, and other common tasks through the API services system. It helps to handle security and access challenges, performance, visibility, reliability, and governance. It hides the multiple backend application of your network, consisting of code mixtures and various platforms, and presents the user with a single facade. 

Let’s see how the API gateway works on a simple example. Let’s say we have a server configuration heavily loaded with network requests. It needs to receive and process data from various sources to answer a single request.

For example, we have a store that works in mobile and desktop versions and communicates via API with mobile applications.

There are a variety of different services that we need to request to display the product. One contains information about item availability, another contains product characteristics, the third shows the product’s features, and one more has the prices. 

By connecting the API gateway, we can control the incoming traffic of server systems. There are rules for load distribution. Typically, the API Gateway acts as a buffer between users and several API services (APIs), acting as a reverse proxy, like Nginx and HAProxy.

All client API requests pass through this gateway and are redirected to the appropriate microservice with routing, structure, and protocol translation. Typically, the system analyzes the original request on the go, calling several microservices and collecting data to determine the best path.

GraphQL as API gateway

Essentially GraphQL is a language to query a database. It provides a clear description of the data in the API and allows to specify how the API should present the data on the backend. With its help, clients receive the data they queried and nothing else. 

Just as an API gateway, GraphQL serves as a single entry point to your APIs. The advantage of it is that it gives you a versionless API making a single request from multiple queries to various services. Unlike API gateway, which gives you control over a request, GraphQL provides control over its shape and result of it. 

For example, a GraphQL API customer needs only the response extension, while another requires the complete response. You will have to create a new endpoint for each response, as you can’t limit the data that should be returned without GraphQL. 

  • GraphQL creates a schema of all the response types and operations, so you receive the APIs structure’s insights. Though some of your APIs may not have specifications, GraphQL has a specification for them. With no need for additional documentation, you will improve the development experience. 
  • Libraries like GraphQL Mesh can empower GraphQL. They automatically combine various data sources into one GraphQL API. Any library may need customization, but it saves development time and improves performance. 
  • GraphQL Mesh can be a data mapper, taking various data sources, stitching them together, and transforming them into GraphQL APIs. It supports OpenAPI/Swagger REST APIs, gRPC APIs, databases, and more data sources. 
  • GraphQL is an obvious candidate for an API gateway. It’s a ready-made ecosystem perfect for this functionality. If you were building an API gateway over REST from scratch, you would have to set up new forms and conventions for the new interface. GraphQL provides free tools to quickly create them with text input, simulations, documentation, and other functionality.
  • GraphQL can utilize schema-build APIs and even create a schema for them, unlike API Gateway, which is not schema-driven. They also miss some features, like caching or authentication, and can manage just one backend. With GraphQL, you can aggregate data from multiple services, which gives your APIs good scalability. 

Of course, this is not a solution for every product. If your API is private and you don’t perform thousands of query variations daily, there is no point in utilizing GraphQL. 

Looking for experienced DevOps to properly manage your API getaways?