Better Programming

Advice for programmers.

Follow publication

Member-only story

GraphQL vs. Rest API: Data Fetching In Python

Ng Wai Foong
Better Programming
Published in
8 min readAug 5, 2021

--

GraphQL logo vs. Rest logo
Image by the author

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 to employee 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 the employee and supplier endpoints.

GraphQL is not a replacement for REST, as both have their own advantages and disadvantages. It is an alternative architecture that provides…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

--

--

Ng Wai Foong
Ng Wai Foong

Written by Ng Wai Foong

Senior AI Engineer@Yoozoo | Content Writer #NLP #datascience #programming #machinelearning | Linkedin: https://www.linkedin.com/in/wai-foong-ng-694619185/

Responses (2)

Write a response