Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

also wrap createQueryPreloader #11719

Merged
merged 6 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion .api-reports/api-report-react_internal.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,11 @@ class Concast<T> extends Observable<T> {
// @public (undocumented)
type ConcastSourcesIterable<T> = Iterable<Source<T>>;

// Warning: (ae-forgotten-export) The symbol "PreloadQueryFunction" needs to be exported by the entry point index.d.ts
//
// @public
function createQueryPreloader(client: ApolloClient<any>): PreloadQueryFunction;

// @public (undocumented)
namespace DataProxy {
// (undocumented)
Expand Down Expand Up @@ -1252,6 +1257,11 @@ const OBSERVED_CHANGED_OPTIONS: readonly ["canonizeResults", "context", "errorPo
// @public (undocumented)
type ObservedOptions = Pick<WatchQueryOptions, (typeof OBSERVED_CHANGED_OPTIONS)[number]>;

// @public
type OnlyRequiredProperties<T> = {
[K in keyof T as {} extends Pick<T, K> ? never : K]: T[K];
};

// @public (undocumented)
type OnQueryUpdated<TResult> = (observableQuery: ObservableQuery<any>, diff: Cache_2.DiffResult<any>, lastDiff: Cache_2.DiffResult<any> | undefined) => boolean | TResult;

Expand Down Expand Up @@ -1291,6 +1301,50 @@ export interface PreloadedQueryRef<TData = unknown, TVariables = unknown> extend
toPromise(): Promise<PreloadedQueryRef<TData, TVariables>>;
}

// @public (undocumented)
type PreloadQueryFetchPolicy = Extract<WatchQueryFetchPolicy, "cache-first" | "network-only" | "no-cache" | "cache-and-network">;

// @public
interface PreloadQueryFunction {
// Warning: (ae-forgotten-export) The symbol "PreloadQueryOptions" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "PreloadQueryOptionsArg" needs to be exported by the entry point index.d.ts
<TData, TVariables extends OperationVariables, TOptions extends Omit<PreloadQueryOptions, "variables">>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, ...[options]: PreloadQueryOptionsArg<NoInfer_2<TVariables>, TOptions>): PreloadedQueryRef<TOptions["errorPolicy"] extends "ignore" | "all" ? TOptions["returnPartialData"] extends true ? DeepPartial<TData> | undefined : TData | undefined : TOptions["returnPartialData"] extends true ? DeepPartial<TData> : TData, TVariables>;
<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer_2<TVariables>> & {
returnPartialData: true;
errorPolicy: "ignore" | "all";
}): PreloadedQueryRef<DeepPartial<TData> | undefined, TVariables>;
<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer_2<TVariables>> & {
errorPolicy: "ignore" | "all";
}): PreloadedQueryRef<TData | undefined, TVariables>;
<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer_2<TVariables>> & {
returnPartialData: true;
}): PreloadedQueryRef<DeepPartial<TData>, TVariables>;
<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, ...[options]: PreloadQueryOptionsArg<NoInfer_2<TVariables>>): PreloadedQueryRef<TData, TVariables>;
}

// Warning: (ae-forgotten-export) The symbol "VariablesOption" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
type PreloadQueryOptions<TVariables extends OperationVariables = OperationVariables> = {
canonizeResults?: boolean;
context?: DefaultContext;
errorPolicy?: ErrorPolicy;
fetchPolicy?: PreloadQueryFetchPolicy;
returnPartialData?: boolean;
refetchWritePolicy?: RefetchWritePolicy;
} & VariablesOption<TVariables>;

// Warning: (ae-forgotten-export) The symbol "OnlyRequiredProperties" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
type PreloadQueryOptionsArg<TVariables extends OperationVariables, TOptions = unknown> = [TVariables] extends [never] ? [
options?: PreloadQueryOptions<never> & TOptions
] : {} extends OnlyRequiredProperties<TVariables> ? [
options?: PreloadQueryOptions<NoInfer_2<TVariables>> & Omit<TOptions, "variables">
] : [
options: PreloadQueryOptions<NoInfer_2<TVariables>> & Omit<TOptions, "variables">
];

// @public (undocumented)
type Primitive = null | undefined | string | number | boolean | symbol | bigint;

Expand Down Expand Up @@ -1692,7 +1746,6 @@ interface SharedWatchQueryOptions<TVariables extends OperationVariables, TData>
// @deprecated
partialRefetch?: boolean;
pollInterval?: number;
// Warning: (ae-forgotten-export) The symbol "RefetchWritePolicy" needs to be exported by the entry point index.d.ts
refetchWritePolicy?: RefetchWritePolicy;
returnPartialData?: boolean;
skipPollAttempt?: () => boolean;
Expand Down Expand Up @@ -2049,6 +2102,17 @@ interface UseSuspenseQueryResult<TData = unknown, TVariables extends OperationVa
subscribeToMore: SubscribeToMoreFunction<TData, TVariables>;
}

// @public (undocumented)
type VariablesOption<TVariables extends OperationVariables> = [
TVariables
] extends [never] ? {
variables?: Record<string, never>;
} : {} extends OnlyRequiredProperties<TVariables> ? {
variables?: TVariables;
} : {
variables: TVariables;
};

// @public
interface WatchFragmentOptions<TData, TVars> {
// @deprecated (undocumented)
Expand Down Expand Up @@ -2081,6 +2145,10 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar

// @public (undocumented)
interface WrappableHooks {
// Warning: (ae-forgotten-export) The symbol "createQueryPreloader" needs to be exported by the entry point index.d.ts
//
// (undocumented)
createQueryPreloader: typeof createQueryPreloader;
// Warning: (ae-forgotten-export) The symbol "useBackgroundQuery" needs to be exported by the entry point index.d.ts
//
// (undocumented)
Expand Down Expand Up @@ -2141,6 +2209,8 @@ export function wrapQueryRef<TData, TVariables extends OperationVariables>(inter
// 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
// src/react/query-preloader/createQueryPreloader.ts:145:3 - (ae-forgotten-export) The symbol "PreloadQueryFetchPolicy" needs to be exported by the entry point index.d.ts
// src/react/query-preloader/createQueryPreloader.ts:167:5 - (ae-forgotten-export) The symbol "RefetchWritePolicy" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
5 changes: 5 additions & 0 deletions .changeset/thin-lies-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Allow wrapping `createQueryPreloader`
2 changes: 1 addition & 1 deletion .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 40015,
"dist/apollo-client.min.cjs": 40030,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32903
}
2 changes: 2 additions & 0 deletions src/react/hooks/internal/wrapHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import type {
import type { QueryManager } from "../../../core/QueryManager.js";
import type { ApolloClient } from "../../../core/ApolloClient.js";
import type { ObservableQuery } from "../../../core/ObservableQuery.js";
import type { createQueryPreloader } from "../../query-preloader/createQueryPreloader.js";

const wrapperSymbol = Symbol.for("apollo.hook.wrappers");

interface WrappableHooks {
createQueryPreloader: typeof createQueryPreloader;
useQuery: typeof useQuery;
useSuspenseQuery: typeof useSuspenseQuery;
useBackgroundQuery: typeof useBackgroundQuery;
Expand Down
11 changes: 10 additions & 1 deletion src/react/query-preloader/createQueryPreloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
import { InternalQueryReference, wrapQueryRef } from "../internal/index.js";
import type { PreloadedQueryRef } from "../internal/index.js";
import type { NoInfer } from "../index.js";
import { wrapHook } from "../hooks/internal/index.js";

type VariablesOption<TVariables extends OperationVariables> =
[TVariables] extends [never] ?
Expand Down Expand Up @@ -168,6 +169,14 @@ export interface PreloadQueryFunction {
export function createQueryPreloader(
client: ApolloClient<any>
): PreloadQueryFunction {
return wrapHook(
"createQueryPreloader",
_createQueryPreloader,
client
)(client);
}

const _createQueryPreloader: typeof createQueryPreloader = (client) => {
return function preloadQuery<
TData = unknown,
TVariables extends OperationVariables = OperationVariables,
Expand All @@ -189,4 +198,4 @@ export function createQueryPreloader(

return wrapQueryRef(queryRef) as PreloadedQueryRef<TData, TVariables>;
};
}
};