How does GraphQL querying work?
GraphQL enables what is known as “declarative data fetching”. In this method, clients can specify the exact data they need from an API.
For example, with REST, an API endpoint may have existed in an application when there was a limited amount of data to be extracted. Over the years, the endpoint could see an increase in the amount of data being returned, even for a limited number of records. Unfortunately, REST would not have provided a mechanism to limit which attributes of an entity should be returned.
Since REST frequently fetches too much data from an endpoint, it can become difficult to ensure that the frontend is receiving the correct data set. With GraphQL on the other hand, the exact data set is declared, removing any doubt that the correct information is present.
With REST, multiple endpoints exist for addressing different entities; but related entities have to be queried independently. GraphQL, on the other hand, allows querying for related entities from one entity. Some (more standard) implementations of GraphQL provide only a single endpoint for the whole implementation and also specify the entry entity in the provided query.
Developers needed an alternative to REST due to the changing digital landscape that included:
- Increased mobile usage: As mobile devices exploded, low-powered devices became more common. That, coupled with slow network connections and the increased need for fast responses led to the need for fewer requests to fetch all required data.
- Faster development cycles: With more devices and frameworks, developers could now build web applications faster than ever before. Unfortunately, they were frequently slowed down by REST’s lack of flexibility for changing API endpoints.
As a result of these limitations of REST, GraphQL began to increase in popularity. But what made REST so popular in the first place?