relay icon indicating copy to clipboard operation
relay copied to clipboard

Add empty query detection and skipping

Open captbaritone opened this issue 1 month ago • 0 comments

Fixes https://github.com/facebook/relay/issues/5126

Summary

Some queries, when invoked with a set of variables are conceptually "empty". Meaning we can prove before sending them to the server that they will fetch zero fields. Some examples include:

  1. A query where all top level fields have been skipped via @skip.
  2. All fields are either Relay resolvers with no server data in their rootFragment, or are client schema extensions

When using a fetch policy like store-or-network we're able to avoid fetching these fields since we find that all server fields can be fulfilled from the cache. However, even when a fetch policy tells us we MUST go to the network, or we are using an API which is designed to explicitly fetch irrespective of fetch policy, these queries can still be safely skipped.

This PR introduces a new check parallel to DataChecker called EmptyChecker which is light-weight and can be called more aggressively than DataChecker. It's faster to run because it does not look up any data in the store, and exits immediately as soon as it finds any server data. This means its time complexity should be on the order of O(top level fields).

captbaritone avatar Jan 07 '26 01:01 captbaritone