Add empty query detection and skipping#5130
Draft
captbaritone wants to merge 5 commits intofacebook:mainfrom
Draft
Add empty query detection and skipping#5130captbaritone wants to merge 5 commits intofacebook:mainfrom
captbaritone wants to merge 5 commits intofacebook:mainfrom
Conversation
Adds EmptyChecker to detect queries with no server-fetchable fields due to @skip/@include directives or client-only fields. When enabled via the ENABLE_EMPTY_QUERY_CHECK feature flag, empty queries are skipped and do not result in network requests. Key changes: - New EmptyChecker module to traverse query AST and detect empty queries - Add isEmpty() method to IEnvironment, IMultiActorEnvironment interfaces - Skip execution in RelayModernEnvironment.execute() and executeWithSource() - Update loadQuery to check isEmpty() before fetching - Add 'execute.skipped' log event with reason: 'empty' - Add comprehensive tests for EmptyChecker and React hooks integration - Feature flag ENABLE_EMPTY_QUERY_CHECK (default: false) Test plan: - EmptyChecker-test.js: Tests all query AST node types and conditional logic - useLazyLoadQueryNode-empty-query-test.js: Tests useLazyLoadQuery and usePreloadedQuery + loadQuery patterns with empty queries
82f584a to
cad1270
Compare
Removes errant throw statement that was making the subsequent return statement unreachable, fixing Flow typecheck CI failure.
- Sort imports alphabetically - Remove unused renderFn variable - Add clarifying comments for test assertions
Rename useLazyLoadQueryNode-empty-query-test.js to emptyQueriesAvoidSuspense-test.js and update GraphQL query names.
6db59f5 to
5efa66f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #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:
@skip.When using a fetch policy like
store-or-networkwe'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
DataCheckercalledEmptyCheckerwhich 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 ofO(top level fields).