What is Over-Fetching or Under-fetching?
Asked Answered
C

3

58

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,

Carrico answered 15/6, 2017 at 10:17 Comment(0)
S
118

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.

Seaquake answered 15/6, 2017 at 13:6 Comment(2)
"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. Worth Highlighting.Coombs
GraphQL focuses talking points on not over-fetching from API perspective. "Client gets exactly & only what they ask for". Important to consider GraphQL "handlers" you've written to fetch from database. Typically we over-fetch from database, selecting all columns user might ask for, but gets trimmed down to 1 or 2 user actually requested. For "joins", or "computed" or lookup fields, we write resolvers that don't cause those database queries unless requested by the client. So GraphQL normally over-fetches by design from db. More traffic to/from db, but less back to client.Swick
I
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

Inpatient answered 18/9, 2020 at 6:45 Comment(0)
F
-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.

Farro answered 16/3, 2019 at 9:3 Comment(1)
This question is regarding graphQL and REST, not Ruby.Ferrer

© 2022 - 2024 — McMap. All rights reserved.