Technology
UNQTech: An Introduction to GraphQL
October 09, 2021

UNQTech: An introduction to GrpahQL
GraphQL is a query language that is used to reduce the complexity of systems. It was created by Facebook when their system got too big and too complex, which made them realize that they needed something else than traditional REST API's. The main problem was, that some of the responses in their API calls contained a lot of attributes that were not always needed. The only way to solve this was either creating a new REST API with less attributes or just use the exiting API, thus, clients with low bandwidth had lengthy wait times for the JSON. Facebook decided not to go with any of those solutions and started a team that eventually created GraphQL.
What is GraphQL and how does it solve the problems?
As mentioned, GraphQL is a querying language that makes the client side developers able to query the data needed. When using GraphQL, developers writes queries that describe the data they need. GraphQL is able to respond only with the data requested in the query, hence, it solves the problem with the unused data and low bandwidth.
A problem that Facebook accounted, was the complexity of their native mobile applications. When having a microservice architecture, eventually the system will have several microservice, and client applications might have to talk to many of them in order to provide functionality for the end user. In these cases, GraphQL can work similar to a middle-layer, that gives the client application the ability to communicate with several services from a single query.

Above, an example of a query utilizing a single query is shown. This query requests information that comes from two different services, FerryAPI and BookingAPI, in a single call. This reduced the number of request that the client has to make since they can be gathered in a single request. Could this not be done with a REST API? The exact example could very well be accomplished with REST if an API was created to return that exact query. The point is that GraphQL offers a flexibility to query the data needed without updating the API if the data already exits in the API.

Another condition that helps reducing the complexity is the number of endpoints. With a REST API you have several endpoints essentially for each function in your API. With GraphQL it is reduce to a single endpoint; "/graphql". Then the query determines which function that is used. Again it makes life easier for a client developer, because they do not need to worry about endpoints.
The essence of the above is, that GraphQL reduces the complexity by abstraction.
When does it make sense to use GraphQL?
For small application it might not be necessary to create a middle-layer with GraphQL, because a single request is the only thing needed from a single API. GraphQL makes sense to use when there is a certain complexity within a system. The Figure above shows a small system with the use of GraphQL as a middle-layer. Here, the client needs information from three API's. Instead of directly interacting with each API, the client only interact with GraphQL, which can be done in a single query or multiple depending on the client. This also points out another question. What if the GraphQL middle-layer needs to be updated e.g. because another client needs additional functionality that the exiting client do not need? In this case the exiting client do not have to worry (unless it is a breaking change, such as changing an exiting type name). Because GraphQL only respond with the data requested in the request query, new functionality will not impact the exiting. This means, that versions are not needed, because GraphQL comes as a single version that evolves over time.

Andreas Soelberg
Udvikler