Tomek Poniatowicz
7/2/2020
GraphQL is a new approach for building an API that aims to get the better of two basic RESTful APIs limitations which are data filters & relationships. These two classic REST flaws force REST users to always get the full payload in response & hit multiple data sources separately.
GraphQL is a data query and manipulation language for APIs created and open-source by Facebook to solve the problem of unoptimized data flow in their mobile app. In general, GraphQL is a syntax that describing the way of requesting data from the server. The main difference comparing Graph QL to traditional REST API is that the response format is described in the query and defined by the client rather than the server. Another important thing about GraphQL is that it is language agnostic.
The core notion of using GraphQL is a schema. Designing a schema is one of the first steps in the development process, casting a shadow over the rest of the project. A carefully planned Graph QL schema is the core of a well-designed and secure product. GraphQL Schema is a center-piece of any Graph QL project and it strictly defines its structure & regulates how the data can be accessed.
There are two basic types of GrahQL operations which are GraphQL Queries & GraphQL Mutations:
The performed operations have a form of a string that a GraphQL server can parse and respond to with requested data in a specific format i.e JSON.
In order to understand what we are querying for the GraphQL server needs to have a defined set of functions/rules that our server would use to generate the response. These functions are called GraphQL Resolvers and they are responsible for handling our queries.
Imagine we want to retrieve posts from a company blog using RESTful API. First, we need to GET api/posts
, but posts have other data like authors so we need to call REST API again to get the details of the posts i.e. authors ending up with two server requests instead of one, and as you continue to scale, you may have even more requests to different endpoints in order to fetch all the needed data.
The performance drop, especially on slow cellular connections, was the main problem Facebook engineers observed, and the urge to find its solution was a substructure for GraphQL. In GraphQL API we have a single endpoint being able to process complex requests. You query the GraphQL server for specific data and it will respond with what was requested, which results in fewer bits transferred over the wire.
As you can see GraphQL is a very interesting concept. In the past couple of years, it gained a lot of traction and became a skill much appreciated and often required in job offers. If you are interested in deepening your knowledge of GraphQL there are plenty of neat resources you can learn from.