Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Over-Fetching or Under-fetching?

I've been playing sometimes with graphQL. Before graphQL, we normally use REST API. Many developers said that graphQL fixes some problems of the REST. (e.g. over-fetching & under-fetching). I confuses with this terms.

Can somebody explain what is over and under fetching in this context?

Thanks,

like image 508
Saber Alex Avatar asked Jun 15 '17 10:06

Saber Alex


People also ask

What is over-fetching and under-fetching?

Over-fetching and under-fetching are two key signs of exhaustion in REST APIs. Both can cause slowness and possible crashes in applications. That's why it is important to apply measures to avoid these problems.

Which is better GraphQL or REST?

GraphQL stability are less error prone, automatic validation and type checking and Rest are better choice for complex queries. GraphQL use cases are multiple microservices, mobile apps and Rest are simple apps, resource-driven apps.

What is the main difference between GraphQL and REST?

The core difference between GraphQL and REST APIs is that GraphQL is a specification, a query language, while REST is an architectural concept for network-based software.

What is a common benefit of using GraphQL over REST?

The main advantage of GraphQl over REST is that REST responses contain too much data or sometimes not enough data, which creates the need for another request. GraphQL solves this problem by fetching only the exact and specific data in a single request.


2 Answers

Over-fetching is fetching too much data, meaning there is data in the response you don't use.

Under-fetching is not having enough data with a call to an endpoint, forcing you to call a second endpoint.

In both cases, they are performance issues: you either use more bandwidth than ideal, or you are making more HTTP requests than ideal.

In a perfect world, these problems would never arise; you would have exactly the right endpoints to give exactly the right data to your products.

These problems often appear when you scale and iterate on your products. The data you use on your pages often change, and the cost to maintain a separate endpoint with exactly the right data for each component becomes too much.

So, you end up with a compromise between not having too many endpoints, and having the endpoints fit each component needs best. This will lead to over-fetching in some cases (the endpoint will provide more data than needed for one specific component), and under-fetching in some others (you will need to call a second endpoint).


GraphQL fixes this problem because it allows you to request which data you want from the server. You specify what you need and will get this data, and only this data, in one trip to the server.

like image 104
yachaka Avatar answered Oct 14 '22 10:10

yachaka


Over fetching means you are fetching irrelevant variables that are useless at this point. Under fetching means you are fetching less variables that are required at this point

like image 20
Ali haider Avatar answered Oct 14 '22 08:10

Ali haider