55

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,

Saber Alex
  • 1,544
  • 3
  • 24
  • 39

3 Answers3

113

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.

yachaka
  • 5,429
  • 1
  • 23
  • 35
2

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

Ali haider
  • 21
  • 1
-1

Over-fetching and under-fetching In a dynamic language like Ruby, over and under fetching are two common pitfalls.

Over-fetching Over-fetching occurs when additional fields are declared in a fragment but are not actually used in the template. This will likely happen when template code is modified to remove usage of a certain field.

If the fragment is not updated along with this changed, the property will still be fetched when we no longer need it. A simple title field may not be a big deal in practice but this property could have been a more expensive nested data tree.

Under-fetching Under-fetching occurs when fields are not declared in a fragment but are used in the template. This missing data will likely surface as a NoFieldError or nil value.

Worse, there may be a latent under-fetch bug when a template does not declare a data dependency but appears to be working because its caller happens to fetch the correct data upstream. But when this same template is rendered from a different path, it errors on missing data.

ATIQ UR REHMAN
  • 431
  • 3
  • 12