Member-only story
GraphQL vs. Rest API: Data Fetching In Python
A side-by-side comparison of both approaches

In this piece, you will learn about the major differences between GraphQL and REST for fetching data. Code snippets will be provided as a side-by-side reference for both approaches.
For your information, REST refers to representational state transfer
architecture in API, and it adheres to the following design principles:
uniform interface
— all requests for the same resources belong to one Uniform Resource Identifier (URI).client-server decoupling
— client and server are independent of each other and connected via an HTTP endpoint.statelessness
— each request must contain the relevant information required for processing.cacheability
— resources should be cacheable whenever possible for performance and scalability.layered system architecture
— it has to be designed in such a way that neither the client nor the server can tell whether it communicates with the end application or an intermediary middleware in between.
On the other hand, GraphQL was developed to solve some of the major pain points when fetching data via the REST architecture. The main objective is the need for better efficiency and flexibility when fetching large amounts of data from various data sources. In general, it tackles the following issues:
over-fetching of data
—refers to a situation in which a client downloads more data than required. For example, making a call toemployee
endpoint will return a JSON array that contains all the relevant information related to the employee (name, age, etc.). The response might contain unnecessary information that is useless to the client.under-fetching of data
— happens when a specific endpoint does not provide all the information required. In this case, the client has to call multiple endpoints to retrieve all the relevant information. For example, in order to get information for employee and supplier, a client might need to call both theemployee
andsupplier
endpoints.
GraphQL is not a replacement for REST, as both have their own advantages and disadvantages. It is an alternative architecture that provides…