Skip to content

Commit

Permalink
experiment: keep ObservableQuery initiated from useQuery inactive b…
Browse files Browse the repository at this point in the history
…efore first subscription
  • Loading branch information
phryneas committed Jan 21, 2025
1 parent acf52e9 commit 907470a
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .api-reports/api-report-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2473,6 +2473,8 @@ export type WatchQueryFetchPolicy = FetchPolicy | "cache-and-network";
//
// @public
export interface WatchQueryOptions<TVariables extends OperationVariables = OperationVariables, TData = any> extends SharedWatchQueryOptions<TVariables, TData> {
// @internal (undocumented)
inactiveBeforeSubscription?: boolean;
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
}

Expand Down Expand Up @@ -2518,7 +2520,7 @@ interface WriteContext extends ReadMergeModifyContext {
// src/core/ObservableQuery.ts:121:5 - (ae-forgotten-export) The symbol "QueryInfo" needs to be exported by the entry point index.d.ts
// src/core/QueryManager.ts:159:5 - (ae-forgotten-export) The symbol "MutationStoreValue" needs to be exported by the entry point index.d.ts
// src/core/QueryManager.ts:414:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:277:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:279:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/link/http/selectHttpOptionsAndBody.ts:128:32 - (ae-forgotten-export) The symbol "HttpQueryOptions" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)
Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2513,6 +2513,8 @@ type WatchQueryFetchPolicy = FetchPolicy | "cache-and-network";

// @public
interface WatchQueryOptions<TVariables extends OperationVariables = OperationVariables, TData = any> extends SharedWatchQueryOptions<TVariables, TData> {
// @internal (undocumented)
inactiveBeforeSubscription?: boolean;
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
}

Expand All @@ -2531,7 +2533,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// src/core/QueryManager.ts:414:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
// src/core/types.ts:175:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:204:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:277:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:279:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:38:3 - (ae-forgotten-export) The symbol "SubscribeToMoreFunction" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:54:3 - (ae-forgotten-export) The symbol "FetchMoreFunction" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:78:4 - (ae-forgotten-export) The symbol "RefetchFunction" needs to be exported by the entry point index.d.ts
Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-react_components.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,8 @@ type WatchQueryFetchPolicy = FetchPolicy | "cache-and-network";

// @public
interface WatchQueryOptions<TVariables extends OperationVariables = OperationVariables, TData = any> extends SharedWatchQueryOptions<TVariables, TData> {
// @internal (undocumented)
inactiveBeforeSubscription?: boolean;
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
}

Expand All @@ -2013,7 +2015,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// src/core/QueryManager.ts:414:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
// src/core/types.ts:175:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:204:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:277:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:279:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-react_context.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,8 @@ type WatchQueryFetchPolicy = FetchPolicy | "cache-and-network";

// @public
interface WatchQueryOptions<TVariables extends OperationVariables = OperationVariables, TData = any> extends SharedWatchQueryOptions<TVariables, TData> {
// @internal (undocumented)
inactiveBeforeSubscription?: boolean;
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
}

Expand All @@ -1933,7 +1935,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// src/core/QueryManager.ts:414:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
// src/core/types.ts:175:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:204:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:277:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:279:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-react_hoc.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,8 @@ type WatchQueryFetchPolicy = FetchPolicy | "cache-and-network";

// @public
interface WatchQueryOptions<TVariables extends OperationVariables = OperationVariables, TData = any> extends SharedWatchQueryOptions<TVariables, TData> {
// @internal (undocumented)
inactiveBeforeSubscription?: boolean;
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
}

Expand Down Expand Up @@ -1960,7 +1962,7 @@ export function withSubscription<TProps extends TGraphQLVariables | {} = {}, TDa
// src/core/QueryManager.ts:414:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
// src/core/types.ts:175:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:204:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:277:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:279:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-react_hooks.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2337,6 +2337,8 @@ type WatchQueryFetchPolicy = FetchPolicy | "cache-and-network";

// @public
interface WatchQueryOptions<TVariables extends OperationVariables = OperationVariables, TData = any> extends SharedWatchQueryOptions<TVariables, TData> {
// @internal (undocumented)
inactiveBeforeSubscription?: boolean;
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
}

Expand All @@ -2355,7 +2357,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// src/core/QueryManager.ts:414:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
// src/core/types.ts:175:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:204:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:277:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:279:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:38:3 - (ae-forgotten-export) The symbol "SubscribeToMoreFunction" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:54:3 - (ae-forgotten-export) The symbol "FetchMoreFunction" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:78:4 - (ae-forgotten-export) The symbol "RefetchFunction" needs to be exported by the entry point index.d.ts
Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-react_internal.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2355,6 +2355,8 @@ type WatchQueryFetchPolicy = FetchPolicy | "cache-and-network";

// @public
interface WatchQueryOptions<TVariables extends OperationVariables = OperationVariables, TData = any> extends SharedWatchQueryOptions<TVariables, TData> {
// @internal (undocumented)
inactiveBeforeSubscription?: boolean;
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
}

Expand Down Expand Up @@ -2418,7 +2420,7 @@ export function wrapQueryRef<TData, TVariables extends OperationVariables>(inter
// src/core/QueryManager.ts:414:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
// src/core/types.ts:175:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:204:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:277:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:279:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:38:3 - (ae-forgotten-export) The symbol "SubscribeToMoreFunction" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:54:3 - (ae-forgotten-export) The symbol "FetchMoreFunction" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:78:4 - (ae-forgotten-export) The symbol "RefetchFunction" needs to be exported by the entry point index.d.ts
Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-react_ssr.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,8 @@ type WatchQueryFetchPolicy = FetchPolicy | "cache-and-network";

// @public
interface WatchQueryOptions<TVariables extends OperationVariables = OperationVariables, TData = any> extends SharedWatchQueryOptions<TVariables, TData> {
// @internal (undocumented)
inactiveBeforeSubscription?: boolean;
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
}

Expand All @@ -1918,7 +1920,7 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// src/core/QueryManager.ts:414:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
// src/core/types.ts:175:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:204:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:277:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:279:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-testing.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,8 @@ type WatchQueryFetchPolicy = FetchPolicy | "cache-and-network";
//
// @public
interface WatchQueryOptions<TVariables extends OperationVariables = OperationVariables, TData = any> extends SharedWatchQueryOptions<TVariables, TData> {
// @internal (undocumented)
inactiveBeforeSubscription?: boolean;
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
}

Expand Down Expand Up @@ -1986,7 +1988,7 @@ export function withWarningSpy<TArgs extends any[], TResult>(it: (...args: TArgs
// src/core/QueryManager.ts:414:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
// src/core/types.ts:175:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:204:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:277:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:279:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-testing_core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,8 @@ type WatchQueryFetchPolicy = FetchPolicy | "cache-and-network";
//
// @public
interface WatchQueryOptions<TVariables extends OperationVariables = OperationVariables, TData = any> extends SharedWatchQueryOptions<TVariables, TData> {
// @internal (undocumented)
inactiveBeforeSubscription?: boolean;
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
}

Expand Down Expand Up @@ -1943,7 +1945,7 @@ export function withWarningSpy<TArgs extends any[], TResult>(it: (...args: TArgs
// src/core/QueryManager.ts:414:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
// src/core/types.ts:175:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:204:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:277:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:279:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report-utilities.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2837,6 +2837,8 @@ type WatchQueryFetchPolicy = FetchPolicy | "cache-and-network";
//
// @public
interface WatchQueryOptions<TVariables extends OperationVariables = OperationVariables, TData = any> extends SharedWatchQueryOptions<TVariables, TData> {
// @internal (undocumented)
inactiveBeforeSubscription?: boolean;
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
}

Expand Down Expand Up @@ -2890,7 +2892,7 @@ interface WriteContext extends ReadMergeModifyContext {
// src/core/QueryManager.ts:414:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
// src/core/types.ts:175:3 - (ae-forgotten-export) The symbol "MutationQueryReducer" needs to be exported by the entry point index.d.ts
// src/core/types.ts:204:5 - (ae-forgotten-export) The symbol "Resolver" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:277:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:279:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/utilities/graphql/storeUtils.ts:226:12 - (ae-forgotten-export) The symbol "storeKeyNameStringify" needs to be exported by the entry point index.d.ts
// src/utilities/policies/pagination.ts:76:3 - (ae-forgotten-export) The symbol "TRelayEdge" needs to be exported by the entry point index.d.ts
// src/utilities/policies/pagination.ts:77:3 - (ae-forgotten-export) The symbol "TRelayPageInfo" needs to be exported by the entry point index.d.ts
Expand Down
4 changes: 3 additions & 1 deletion .api-reports/api-report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3184,6 +3184,8 @@ export type WatchQueryFetchPolicy = FetchPolicy | "cache-and-network";

// @public
export interface WatchQueryOptions<TVariables extends OperationVariables = OperationVariables, TData = any> extends SharedWatchQueryOptions<TVariables, TData> {
// @internal (undocumented)
inactiveBeforeSubscription?: boolean;
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
}

Expand Down Expand Up @@ -3229,7 +3231,7 @@ interface WriteContext extends ReadMergeModifyContext {
// src/core/ObservableQuery.ts:121:5 - (ae-forgotten-export) The symbol "QueryInfo" needs to be exported by the entry point index.d.ts
// src/core/QueryManager.ts:159:5 - (ae-forgotten-export) The symbol "MutationStoreValue" needs to be exported by the entry point index.d.ts
// src/core/QueryManager.ts:414:7 - (ae-forgotten-export) The symbol "UpdateQueries" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:277:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/core/watchQueryOptions.ts:279:2 - (ae-forgotten-export) The symbol "UpdateQueryFn" needs to be exported by the entry point index.d.ts
// src/link/http/selectHttpOptionsAndBody.ts:128:32 - (ae-forgotten-export) The symbol "HttpQueryOptions" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:38:3 - (ae-forgotten-export) The symbol "SubscribeToMoreFunction" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:54:3 - (ae-forgotten-export) The symbol "FetchMoreFunction" needs to be exported by the entry point index.d.ts
Expand Down
4 changes: 2 additions & 2 deletions .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 41640,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 34381
"dist/apollo-client.min.cjs": 41719,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 34458
}
6 changes: 6 additions & 0 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,13 @@ export class ObservableQuery<
queryInfo: QueryInfo;
options: WatchQueryOptions<TVariables, TData>;
}) {
let startedInactive = options.inactiveBeforeSubscription;
super((observer: Observer<ApolloQueryResult<MaybeMasked<TData>>>) => {
if (startedInactive && !queryManager["queries"].has(this.queryId)) {
queryManager["queries"].set(this.queryId, queryInfo);
startedInactive = false;
}

// Zen Observable has its own error function, so in order to log correctly
// we need to provide a custom error callback.
try {
Expand Down
4 changes: 3 additions & 1 deletion src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,9 @@ export class QueryManager<TStore> {
});
observable["lastQuery"] = query;

this.queries.set(observable.queryId, queryInfo);
if (!options.inactiveBeforeSubscription) {
this.queries.set(observable.queryId, queryInfo);
}

// We give queryInfo the transformed query to ensure the first cache diff
// uses the transformed query instead of the raw query
Expand Down
2 changes: 2 additions & 0 deletions src/core/watchQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ export interface WatchQueryOptions<
> extends SharedWatchQueryOptions<TVariables, TData> {
/** {@inheritDoc @apollo/client!QueryOptionsDocumentation#query:member} */
query: DocumentNode | TypedDocumentNode<TData, TVariables>;
/** @internal */
inactiveBeforeSubscription?: boolean;
}

export interface SharedWatchQueryOptions<
Expand Down
2 changes: 1 addition & 1 deletion src/react/hooks/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,7 @@ describe("useQuery Hook", () => {

function checkObservableQueries(expectedLinkCount: number) {
const obsQueries = client.getObservableQueries("all");
expect(obsQueries.size).toBe(2);
expect(obsQueries.size).toBe(1);

const activeSet = new Set<typeof result.current.observable>();
const inactiveSet = new Set<typeof result.current.observable>();
Expand Down
2 changes: 2 additions & 0 deletions src/react/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ export function createMakeWatchQueryOptions<
getDefaultFetchPolicy(defaultOptions, client.defaultOptions);
}

watchQueryOptions.inactiveBeforeSubscription = true;

return watchQueryOptions;
};
}
Expand Down

0 comments on commit 907470a

Please sign in to comment.