GraphQL: evolution of API design

INFRASTRUCTURE, OPERATIONS.
img blog graphql

We continue our series on best practices for distributed application development and microservices and this time we dedicate our blog post to GraphQL.

More and more companies and applications are taking advantage of GraphQL, a data query language. What is it and what are the main benefits that are making it a global trend?

What is GraphQL?

GraphQL is a data query protocol originally developed by Facebook for internal use. Since 2015, it has been available in an open source version, which has boosted its development and generated many practical advantages and benefits.

If you are familiar with how applications work, you will know that data query protocols are essential to obtain all kinds of reports and information. REST is the most widely used mechanism, but now GraphQL has emerged as an alternative that aims to better adapt to today's needs.

GraphQL's core objective is to offer customers a more direct, simple and efficient way to get exactly the data they require, through a powerful and dynamic protocol.

Growing community

The openness of the code has allowed GraphQL to achieve great development worldwide in just a few years. Today, an increasingly large community has been consolidated, which participates by creating guides and tutorials for its use, as well as tools to create APIs with great ease.

The latest and more stable version was released in October 2016 and new specifications have been added since then. One of the most important ones was officially adhered to in February 2018: elSchema Definition Language (SDL).

Surely you have used it

If you have used Facebook, you have already used GraphQL at least as a user, possibly without knowing it. Moreover, its expansion has also allowed hundreds of apps and companies to adopt this protocol as the basis for their queries, including for example Shopify, Twitter, Yelp, Coursera, Drupal and even media outlets such as the New York Times, the Financial Times and 20 minutes, among many others.

High compatibility

GraphQL is neither a library nor a framework, it is a query language that can be used interchangeably in different programming codes, including some of today's most widely used ones such as JavaScript, Ruby, Java, PHP and Python. Although it does not originally offer many resources on the backend, it is designed to generate results that are easily interpreted by humans.

Simplify consultations

Unlike REST, which requires a call for each data query, GraphQL uses a single request, through which information from different tables or databases can be accessed. This is important, as it not only results in a more straightforward and simple environment, but also saves the consumption of a large amount of resources, which are often necessary through environments such as REST.

While REST only offers an ad-hoc query system, with GraphQL you have a cleaner and more straightforward layered scheme for the connection between servers and clients.

Exactly the data you need and nothing more

In REST, it is common that we have to deal with problems such as over-retrieval of data, i.e. receiving more information than we really need, as it is more complicated to retrieve only some specific fields. This implies an excessive consumption of bandwidth and, consequently, a longer response time.

For example, if we want to know the first surname of a group of users, but it happens to be the full name in the same field, REST gives us the full name and then we have to filter the surname, which is not very efficient.

Another common problem, not only in REST but also in other environments such as Soap, is under-retrieval or underfetching, i.e. receiving less information than we need.

For example, we have a database with employee information and we want to know names and contract types. If a single instruction does not generate this information, several calls have to be made for each field, and if hundreds of fields are needed, the number of calls multiplies exorbitantly. GraphQL aims to optimize these functions through parameters that allow more direct and specific generation, from single calls, of all the data required.

Makes life easier for developers

GraphQL is also designed to sort out the chaos in API manipulation, through a well-defined protocol that any engineer can easily learn to handle in a few minutes.

This environment avoids complications such as having to generate documentation for API operation or having to spend too much time understanding how API procedures are structured.

This has enabled companies to reduce development costs as well as speed up the realization process, resulting in higher productivity.

Robust scheme

One of the most common challenges for developers was dealing with outdated API documentation, where the control parameters were not clear. On the contrary, one of the main qualities of GraphQL is that it has a very solid, reliable and well-defined schema that allows it to have control of the configurations.

There are 3 main operations or Querys: API queries, mutations (which are functions that perform modifications and send a response) and subscriptions (which allow receiving change data on the server). All this is done under the well-defined parameters of the SDL.

Composition of APIs

Another common problem for developers occurs when you have more than one client for the same project. For example, one framework for iOS, one for Android and one for the web. However, thanks to GraphQL's compatibility and multi-system capability, it is possible to connect information and communicate results from different environments.

GraphQL's schema stitching allows you to connect and combine multiple APIs to form a single backend interface, which is very useful when you want to connect to third-party APIs, such as Shopify, Github and others, as you can delegate or combine information and extract data from all these interfaces.

Ultimately, there are many reasons why GraphQL is a protocol that is not only currently strong, but also has a good projection for the future. Although Rest previously had a clearly predominant role in the market, the growth and expansion in the use of the language created by Facebook is not only becoming a trend, but will probably soon be the main method for obtaining information due to its functionality and ability to adapt to today's technologies and needs.

If your organization is interested in implementing projects with GraphQL from the hand of experts, we invite you to contact us.

You may also be interested in:You may also be interested in: