Skip to content

Commit

Permalink
Depcreate GK relay_enable_load_query_request_deduping
Browse files Browse the repository at this point in the history
Reviewed By: captbaritone, omarchehab98

Differential Revision: D65621722

fbshipit-source-id: 1e600dc3a695d074c8d31a9253f6fc1c214ba933
  • Loading branch information
Nithik Balachandran authored and facebook-github-bot committed Nov 11, 2024
1 parent 81eab03 commit 4360f69
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 40 deletions.
68 changes: 30 additions & 38 deletions packages/react-relay/relay-hooks/loadQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const {
__internal: {fetchQueryDeduped},
Observable,
PreloadableQueryRegistry,
RelayFeatureFlags,
ReplaySubject,
createOperationDescriptor,
getRequest,
Expand Down Expand Up @@ -140,34 +139,29 @@ function loadQuery<
// `source` observable is returned.
didMakeNetworkRequest = true;

let observable;
const subject = new ReplaySubject<GraphQLResponse>();
if (RelayFeatureFlags.ENABLE_LOAD_QUERY_REQUEST_DEDUPING === true) {
// Here, we are calling fetchQueryDeduped at the network layer level,
// which ensures that only a single network request is active for a given
// (environment, identifier) pair.
// Since network requests can be started /before/ we have the query ast
// necessary to process the results, we need to dedupe the raw requests
// separately from deduping the operation execution; specifically,
// if `loadQuery` is called multiple times before the query ast is available,
// we still want the network request to be deduped.
// - If a duplicate active network request is found, it will return an
// Observable that replays the events of the already active request.
// - If no duplicate active network request is found, it will call the fetchFn
// to start the request, and return an Observable that will replay
// the events from the network request.
// We provide an extra key to the identifier to distinguish deduping
// of raw network requests vs deduping of operation executions.
const identifier: RequestIdentifier =
'raw-network-request-' + getRequestIdentifier(params, variables);
observable = fetchQueryDeduped(environment, identifier, () => {
const network = environment.getNetwork();
return network.execute(params, variables, networkCacheConfig);
});
} else {

// Here, we are calling fetchQueryDeduped at the network layer level,
// which ensures that only a single network request is active for a given
// (environment, identifier) pair.
// Since network requests can be started /before/ we have the query ast
// necessary to process the results, we need to dedupe the raw requests
// separately from deduping the operation execution; specifically,
// if `loadQuery` is called multiple times before the query ast is available,
// we still want the network request to be deduped.
// - If a duplicate active network request is found, it will return an
// Observable that replays the events of the already active request.
// - If no duplicate active network request is found, it will call the fetchFn
// to start the request, and return an Observable that will replay
// the events from the network request.
// We provide an extra key to the identifier to distinguish deduping
// of raw network requests vs deduping of operation executions.
const identifier: RequestIdentifier =
'raw-network-request-' + getRequestIdentifier(params, variables);
const observable = fetchQueryDeduped(environment, identifier, () => {
const network = environment.getNetwork();
observable = network.execute(params, variables, networkCacheConfig);
}
return network.execute(params, variables, networkCacheConfig);
});
const {unsubscribe} = observable.subscribe({
error(err) {
Expand Down Expand Up @@ -196,17 +190,15 @@ function loadQuery<
operation: OperationDescriptor,
fetchFn: () => Observable<GraphQLResponse>,
) => {
if (RelayFeatureFlags.ENABLE_LOAD_QUERY_REQUEST_DEDUPING === true) {
// N.B. at this point, if we're calling execute with a query ast (OperationDescriptor),
// we are guaranteed to have started a network request. We set this to
// true here as well since `makeNetworkRequest` might get skipped in the case
// where the query ast is already available and the query executions get deduped.
// Even if the execution gets deduped below, we still wan't to return
// an observable that provides the replayed network events for the query,
// so we set this to true before deduping, to guarantee that the `source`
// observable is returned.
didMakeNetworkRequest = true;
}
// N.B. at this point, if we're calling execute with a query ast (OperationDescriptor),
// we are guaranteed to have started a network request. We set this to
// true here as well since `makeNetworkRequest` might get skipped in the case
// where the query ast is already available and the query executions get deduped.
// Even if the execution gets deduped below, we still wan't to return
// an observable that provides the replayed network events for the query,
// so we set this to true before deduping, to guarantee that the `source`
// observable is returned.
didMakeNetworkRequest = true;

// Here, we are calling fetchQueryDeduped, which ensures that only
// a single operation is active for a given (environment, identifier) pair,
Expand Down
2 changes: 0 additions & 2 deletions packages/relay-runtime/util/RelayFeatureFlags.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export type FeatureFlags = {
ENABLE_RELAY_RESOLVERS: boolean,
ENABLE_GETFRAGMENTIDENTIFIER_OPTIMIZATION: boolean,
ENABLE_FRIENDLY_QUERY_NAME_GQL_URL: boolean,
ENABLE_LOAD_QUERY_REQUEST_DEDUPING: boolean,
ENABLE_DO_NOT_WRAP_LIVE_QUERY: boolean,
ENABLE_NOTIFY_SUBSCRIPTION: boolean,
BATCH_ASYNC_MODULE_UPDATES_FN: ?(() => void) => Disposable,
Expand Down Expand Up @@ -62,7 +61,6 @@ const RelayFeatureFlags: FeatureFlags = {
ENABLE_RELAY_RESOLVERS: false,
ENABLE_GETFRAGMENTIDENTIFIER_OPTIMIZATION: false,
ENABLE_FRIENDLY_QUERY_NAME_GQL_URL: false,
ENABLE_LOAD_QUERY_REQUEST_DEDUPING: true,
ENABLE_DO_NOT_WRAP_LIVE_QUERY: false,
ENABLE_NOTIFY_SUBSCRIPTION: false,
BATCH_ASYNC_MODULE_UPDATES_FN: null,
Expand Down

0 comments on commit 4360f69

Please sign in to comment.