Skip to content

Commit

Permalink
Add the ability to preload a query outside of React (#11412)
Browse files Browse the repository at this point in the history
Co-authored-by: Lenz Weber-Tronic <lorenz.weber-tronic@apollographql.com>
Co-authored-by: jerelmiller <jerelmiller@users.noreply.github.com>
  • Loading branch information
3 people committed Dec 18, 2023
1 parent 07fcf6a commit 58db5c3
Show file tree
Hide file tree
Showing 37 changed files with 5,441 additions and 153 deletions.
4 changes: 4 additions & 0 deletions .api-reports/api-report-core.md
Expand Up @@ -1519,6 +1519,8 @@ export class ObservableQuery<TData = any, TVariables extends OperationVariables
//
// (undocumented)
reobserveAsConcast(newOptions?: Partial<WatchQueryOptions<TVariables, TData>>, newNetworkStatus?: NetworkStatus): Concast<ApolloQueryResult<TData>>;
// @internal (undocumented)
resetDiff(): void;
// (undocumented)
resetLastResults(): void;
// (undocumented)
Expand Down Expand Up @@ -1690,6 +1692,8 @@ class QueryInfo {
// (undocumented)
reset(): void;
// (undocumented)
resetDiff(): void;
// (undocumented)
resetLastWrite(): void;
// (undocumented)
setDiff(diff: Cache_2.DiffResult<any> | null): void;
Expand Down
106 changes: 94 additions & 12 deletions .api-reports/api-report-react.md
Expand Up @@ -556,6 +556,9 @@ type ConcastSourcesIterable<T> = Iterable<Source<T>>;
export interface Context extends Record<string, any> {
}

// @public
export function createQueryPreloader(client: ApolloClient<any>): PreloadQueryFunction;

// @public (undocumented)
namespace DataProxy {
// (undocumented)
Expand Down Expand Up @@ -936,13 +939,15 @@ interface IncrementalPayload<TData, TExtensions> {
// @public (undocumented)
class InternalQueryReference<TData = unknown> {
// Warning: (ae-forgotten-export) The symbol "InternalQueryReferenceOptions" needs to be exported by the entry point index.d.ts
constructor(observable: ObservableQuery<TData>, options: InternalQueryReferenceOptions);
constructor(observable: ObservableQuery<TData, any>, options: InternalQueryReferenceOptions);
// (undocumented)
applyOptions(watchQueryOptions: ObservedOptions): QueryRefPromise<TData>;
// Warning: (ae-forgotten-export) The symbol "ObservedOptions" needs to be exported by the entry point index.d.ts
//
// (undocumented)
didChangeOptions(watchQueryOptions: ObservedOptions): boolean;
// (undocumented)
get disposed(): boolean;
// Warning: (ae-forgotten-export) The symbol "FetchMoreOptions" needs to be exported by the entry point index.d.ts
//
// (undocumented)
Expand All @@ -964,6 +969,8 @@ class InternalQueryReference<TData = unknown> {
// (undocumented)
refetch(variables: OperationVariables | undefined): Promise<ApolloQueryResult<TData>>;
// (undocumented)
reinitialize(): void;
// (undocumented)
result: ApolloQueryResult<TData>;
// (undocumented)
retain(): () => void;
Expand Down Expand Up @@ -1347,6 +1354,8 @@ class ObservableQuery<TData = any, TVariables extends OperationVariables = Opera
//
// (undocumented)
reobserveAsConcast(newOptions?: Partial<WatchQueryOptions<TVariables, TData>>, newNetworkStatus?: NetworkStatus): Concast<ApolloQueryResult<TData>>;
// @internal (undocumented)
resetDiff(): void;
// (undocumented)
resetLastResults(): void;
// (undocumented)
Expand Down Expand Up @@ -1451,6 +1460,47 @@ interface PendingPromise<TValue> extends Promise<TValue> {
status: "pending";
}

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

// @public
export interface PreloadQueryFunction {
// 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<TVariables>, TOptions>): QueryReference<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<TVariables>> & {
returnPartialData: true;
errorPolicy: "ignore" | "all";
}): QueryReference<DeepPartial<TData> | undefined, TVariables>;
<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer<TVariables>> & {
errorPolicy: "ignore" | "all";
}): QueryReference<TData | undefined, TVariables>;
<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: PreloadQueryOptions<NoInfer<TVariables>> & {
returnPartialData: true;
}): QueryReference<DeepPartial<TData>, TVariables>;
<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, ...[options]: PreloadQueryOptionsArg<NoInfer<TVariables>>): QueryReference<TData, TVariables>;
}

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

// @public (undocumented)
type PreloadQueryOptionsArg<TVariables extends OperationVariables, TOptions = unknown> = [TVariables] extends [never] ? [
options?: PreloadQueryOptions<never> & TOptions
] : {} extends OnlyRequiredProperties<TVariables> ? [
options?: PreloadQueryOptions<NoInfer<TVariables>> & Omit<TOptions, "variables">
] : [
options: PreloadQueryOptions<NoInfer<TVariables>> & Omit<TOptions, "variables">
];

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

Expand Down Expand Up @@ -1543,6 +1593,8 @@ class QueryInfo {
// (undocumented)
reset(): void;
// (undocumented)
resetDiff(): void;
// (undocumented)
resetLastWrite(): void;
// (undocumented)
setDiff(diff: Cache_2.DiffResult<any> | null): void;
Expand Down Expand Up @@ -1716,13 +1768,15 @@ interface QueryOptions<TVariables = OperationVariables, TData = any> {
// Warning: (ae-unresolved-link) The @link reference could not be resolved: The reference is ambiguous because "useBackgroundQuery" has more than one declaration; you need to add a TSDoc member reference selector
//
// @public
export interface QueryReference<TData = unknown> {
export interface QueryReference<TData = unknown, TVariables = unknown> {
// (undocumented)
[PROMISE_SYMBOL]: QueryRefPromise<TData>;
// Warning: (ae-forgotten-export) The symbol "InternalQueryReference" needs to be exported by the entry point index.d.ts
//
// (undocumented)
readonly [QUERY_REFERENCE_SYMBOL]: InternalQueryReference<TData>;
// (undocumented)
toPromise(): Promise<QueryReference<TData, TVariables>>;
}

// Warning: (ae-forgotten-export) The symbol "PromiseWithState" needs to be exported by the entry point index.d.ts
Expand Down Expand Up @@ -2072,7 +2126,7 @@ export function useApolloClient(override?: ApolloClient<object>): ApolloClient<o
//
// @public (undocumented)
export function useBackgroundQuery<TData, TVariables extends OperationVariables, TOptions extends Omit<BackgroundQueryHookOptions<TData>, "variables">>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & TOptions): [
(QueryReference<TOptions["errorPolicy"] extends "ignore" | "all" ? TOptions["returnPartialData"] extends true ? DeepPartial<TData> | undefined : TData | undefined : TOptions["returnPartialData"] extends true ? DeepPartial<TData> : TData> | (TOptions["skip"] extends boolean ? undefined : never)),
(QueryReference<TOptions["errorPolicy"] extends "ignore" | "all" ? TOptions["returnPartialData"] extends true ? DeepPartial<TData> | undefined : TData | undefined : TOptions["returnPartialData"] extends true ? DeepPartial<TData> : TData, TVariables> | (TOptions["skip"] extends boolean ? undefined : never)),
UseBackgroundQueryResult<TData, TVariables>
];

Expand All @@ -2081,15 +2135,15 @@ export function useBackgroundQuery<TData = unknown, TVariables extends Operation
returnPartialData: true;
errorPolicy: "ignore" | "all";
}): [
QueryReference<DeepPartial<TData> | undefined>,
QueryReference<DeepPartial<TData> | undefined, TVariables>,
UseBackgroundQueryResult<TData, TVariables>
];

// @public (undocumented)
export function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {
errorPolicy: "ignore" | "all";
}): [
QueryReference<TData | undefined>,
QueryReference<TData | undefined, TVariables>,
UseBackgroundQueryResult<TData, TVariables>
];

Expand All @@ -2098,28 +2152,31 @@ export function useBackgroundQuery<TData = unknown, TVariables extends Operation
skip: boolean;
returnPartialData: true;
}): [
QueryReference<DeepPartial<TData>> | undefined,
QueryReference<DeepPartial<TData>, TVariables> | undefined,
UseBackgroundQueryResult<TData, TVariables>
];

// @public (undocumented)
export function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {
returnPartialData: true;
}): [
QueryReference<DeepPartial<TData>>,
QueryReference<DeepPartial<TData>, TVariables>,
UseBackgroundQueryResult<TData, TVariables>
];

// @public (undocumented)
export function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {
skip: boolean;
}): [
QueryReference<TData> | undefined,
QueryReference<TData, TVariables> | undefined,
UseBackgroundQueryResult<TData, TVariables>
];

// @public (undocumented)
export function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: BackgroundQueryHookOptionsNoInfer<TData, TVariables>): [QueryReference<TData>, UseBackgroundQueryResult<TData, TVariables>];
export function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: BackgroundQueryHookOptionsNoInfer<TData, TVariables>): [
QueryReference<TData, TVariables>,
UseBackgroundQueryResult<TData, TVariables>
];

// @public (undocumented)
export function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: SkipToken): [undefined, UseBackgroundQueryResult<TData, TVariables>];
Expand All @@ -2128,13 +2185,13 @@ export function useBackgroundQuery<TData = unknown, TVariables extends Operation
export function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: SkipToken | (BackgroundQueryHookOptionsNoInfer<TData, TVariables> & {
returnPartialData: true;
})): [
QueryReference<DeepPartial<TData>> | undefined,
QueryReference<DeepPartial<TData>, TVariables> | undefined,
UseBackgroundQueryResult<TData, TVariables>
];

// @public (undocumented)
export function useBackgroundQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: SkipToken | BackgroundQueryHookOptionsNoInfer<TData, TVariables>): [
QueryReference<TData> | undefined,
QueryReference<TData, TVariables> | undefined,
UseBackgroundQueryResult<TData, TVariables>
];

Expand Down Expand Up @@ -2194,7 +2251,7 @@ export function useLoadableQuery<TData = unknown, TVariables extends OperationVa
// @public (undocumented)
export type UseLoadableQueryResult<TData = unknown, TVariables extends OperationVariables = OperationVariables> = [
LoadQueryFunction<TVariables>,
QueryReference<TData> | null,
QueryReference<TData, TVariables> | null,
{
fetchMore: FetchMoreFunction<TData, TVariables>;
refetch: RefetchFunction<TData, TVariables>;
Expand All @@ -2208,6 +2265,17 @@ export function useMutation<TData = any, TVariables = OperationVariables, TConte
// @public (undocumented)
export function useQuery<TData = any, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: QueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>): QueryResult<TData, TVariables>;

// @public
export function useQueryRefHandlers<TData = unknown, TVariables extends OperationVariables = OperationVariables>(queryRef: QueryReference<TData, TVariables>): UseQueryRefHandlersResult<TData, TVariables>;

// @public (undocumented)
export interface UseQueryRefHandlersResult<TData = unknown, TVariables extends OperationVariables = OperationVariables> {
// (undocumented)
fetchMore: FetchMoreFunction<TData, TVariables>;

refetch: RefetchFunction<TData, TVariables>;
}

// Warning: (ae-forgotten-export) The symbol "ReactiveVar" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
Expand Down Expand Up @@ -2287,6 +2355,17 @@ export interface UseSuspenseQueryResult<TData = unknown, TVariables extends Oper
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 (undocumented)
type WatchQueryFetchPolicy = FetchPolicy | "cache-and-network";

Expand Down Expand Up @@ -2336,6 +2415,9 @@ interface WatchQueryOptions<TVariables extends OperationVariables = OperationVar
// src/react/hooks/useBackgroundQuery.ts:30:3 - (ae-forgotten-export) The symbol "FetchMoreFunction" needs to be exported by the entry point index.d.ts
// src/react/hooks/useBackgroundQuery.ts:31:3 - (ae-forgotten-export) The symbol "RefetchFunction" needs to be exported by the entry point index.d.ts
// src/react/hooks/useLoadableQuery.ts:50:5 - (ae-forgotten-export) The symbol "ResetFunction" needs to be exported by the entry point index.d.ts
// src/react/query-preloader/createQueryPreloader.ts:76:1 - (ae-unresolved-link) The @link reference could not be resolved: The package "@apollo/client" does not have an export "NetworkStatus"
// src/react/query-preloader/createQueryPreloader.ts:79:5 - (ae-unresolved-link) The @link reference could not be resolved: The package "@apollo/client" does not have an export "ErrorPolicy"
// src/react/query-preloader/createQueryPreloader.ts:84:5 - (ae-unresolved-link) The @link reference could not be resolved: The package "@apollo/client" does not have an export "FetchPolicy"

// (No @packageDocumentation comment for this package)

Expand Down
4 changes: 4 additions & 0 deletions .api-reports/api-report-react_components.md
Expand Up @@ -1139,6 +1139,8 @@ class ObservableQuery<TData = any, TVariables extends OperationVariables = Opera
//
// (undocumented)
reobserveAsConcast(newOptions?: Partial<WatchQueryOptions<TVariables, TData>>, newNetworkStatus?: NetworkStatus): Concast<ApolloQueryResult<TData>>;
// @internal (undocumented)
resetDiff(): void;
// (undocumented)
resetLastResults(): void;
// (undocumented)
Expand Down Expand Up @@ -1299,6 +1301,8 @@ class QueryInfo {
// (undocumented)
reset(): void;
// (undocumented)
resetDiff(): void;
// (undocumented)
resetLastWrite(): void;
// (undocumented)
setDiff(diff: Cache_2.DiffResult<any> | null): void;
Expand Down
4 changes: 4 additions & 0 deletions .api-reports/api-report-react_context.md
Expand Up @@ -1072,6 +1072,8 @@ class ObservableQuery<TData = any, TVariables extends OperationVariables = Opera
//
// (undocumented)
reobserveAsConcast(newOptions?: Partial<WatchQueryOptions<TVariables, TData>>, newNetworkStatus?: NetworkStatus): Concast<ApolloQueryResult<TData>>;
// @internal (undocumented)
resetDiff(): void;
// (undocumented)
resetLastResults(): void;
// (undocumented)
Expand Down Expand Up @@ -1207,6 +1209,8 @@ class QueryInfo {
// (undocumented)
reset(): void;
// (undocumented)
resetDiff(): void;
// (undocumented)
resetLastWrite(): void;
// (undocumented)
setDiff(diff: Cache_2.DiffResult<any> | null): void;
Expand Down
4 changes: 4 additions & 0 deletions .api-reports/api-report-react_hoc.md
Expand Up @@ -1124,6 +1124,8 @@ class ObservableQuery<TData = any, TVariables extends OperationVariables = Opera
//
// (undocumented)
reobserveAsConcast(newOptions?: Partial<WatchQueryOptions<TVariables, TData>>, newNetworkStatus?: NetworkStatus): Concast<ApolloQueryResult<TData>>;
// @internal (undocumented)
resetDiff(): void;
// (undocumented)
resetLastResults(): void;
// (undocumented)
Expand Down Expand Up @@ -1276,6 +1278,8 @@ class QueryInfo {
// (undocumented)
reset(): void;
// (undocumented)
resetDiff(): void;
// (undocumented)
resetLastWrite(): void;
// (undocumented)
setDiff(diff: Cache_2.DiffResult<any> | null): void;
Expand Down

0 comments on commit 58db5c3

Please sign in to comment.