📜 ⬆️ ⬇️

GraphQL - a new look at the API. Part 1

Hello everyone, I want to share the accumulated knowledge of GraphQL, which was formed on the basis of about a hundred read articles / docks and the month of building an API using GraphQL.

Well, let's start with what GraphQL is?


GraphQL is a standard for declaring data structure and data retrieval methods, which acts as an additional layer between the client and the server.

One of the main features of GraphQL is that the structure and volume of data is determined by the client application.
')
Consider an example of a simple user request.



The client specifies exactly what data he wants to receive using a declarative, graph-like structure that is very similar to the JSON format.

In this case, the client requests three fields (name, email and age). But it can request both one field, for example name, and an arbitrary number of fields that are defined in the user type on the GraphQL server.

In this approach, in addition to convenience, we have reduced either the number of requests or the volume of data at the transport level.

GraphQL makes it easy to aggregate data from multiple sources.

Let's look at a simple client-server architecture.


We have a client application and one server. Data transport looks pretty simple
no matter which data transfer protocol is used for this. In the case of http, we send a request and get an answer, everything is quite simple.



As I said before, GraphQL is an additional layer between the client and the server, and if you look at this architecture, using GraphQL looks like it’s redundant.

But as soon as another service is added, everything falls into place.

Services can be written in any programming language, interact with different databases, Sql or NoSql, can have different APIs. Working with such an architecture becomes quite difficult, and adding each new service requires a lot of resources.

This is a classic problem of scaling a project, and you probably use some kind of “API Gateway” when working with several services.

GraphQL is this standardized API Gateway. Client-server data transport can be performed using any protocol (http, ssh, ws, cli, etc.).

Client requests resources from GraphQL server using GraphQL query. GraphQL server analyzes the query, recursively traverses the graph and performs its “resolver” function for each field. When all the data on the request will be collected, GraphQL server will return the answer.

It is important to note that adding a new service does not affect the existing application. Due to the fact that the client determines what data he wants to receive, you can without fear of expanding existing types.

Type system


GraphQL uses a type system to describe data.


In GraphQL, fields can be represented by both basic and custom types. In this example, the user field is represented by the user type User. The User type has a set of fields that are represented by base types.

Thus, the graph-like structure of an indefinite nesting level is realized.

Compare GraphQL API and REST API



At the end of the first part, the second part will be added soon. Thanks to all!)

Source: https://habr.com/ru/post/343872/


All Articles