GraphQL: Created to Solve REST API Limitations
GraphQL is a query language that was created by the Facebook team. They wanted to solve one of the REST API problems. Let’s imagine a blog where you need to query the database to get a list of authors who have written hundreds of posts. And here the question arises: how to get these posts? […]
Technologies
GraphQL is a query language that was created by the Facebook team. They wanted to solve one of the REST API problems.
Let’s imagine a blog where you need to query the database to get a list of authors who have written hundreds of posts.

And here the question arises: how to get these posts? If we return a list of all his posts together with the author, then in addition to the entities of the authors, we get a large number of entities of posts. It turns out that the size of the answer will increase several times. But such a number of posts is almost never needed.
There is another option: send a request for the posts of each author. But in this case there is another problem. If posts are still needed, then many additional requests must be made for each author. This will cause an increased load on the system.
You can handle just two requests by making another request for posts with instructions on which authors should return those posts. But in this case, you need to take into account the limitations of most browsers. The URL should not be longer than 2048 characters.
The complexity and number of queries will increase multiple times with each new related entity.
There is also a way out of this situation, with REST APIs building different levels of requests. For example, with the answer, they return a link that can be used to get additional data.
And there is another approach. For example, GraphQL provides one endpoint that can be called whatever you want. And all the logic here is contained in the query. This is the language in which you can make a request and specify which specific parameter is required. In these requests, we specify all the necessary fields from the author’s model. If posts are needed, then we indicate them as well, and we do not indicate everything superfluous at all.
Another Advantage

Another advantage of GraphQL can be called work on top of the usual HTTP. Thanks to this, you can use any client convenient for you. For correct operation, you need to add the query parameter to the body of the request, where the query itself is passed as a string. In response, we will receive only what we asked for. Moreover, only one request is sent and only one clearly structured response is received. Automatically, thanks to this, we avoid sorting or not selecting data and thousands of unnecessary requests.
It is quite a popular query language and therefore has support with many programming languages, both back end and front end.
At Swan Software Solutions, we are skilled in a variety of programming languages, technologies, and tools. To find out how our team can help you team, schedule a free assessment.