UNPKG

5.48 kBTypeScriptView Raw
1import type { DocumentNode, OperationVariables, TypedDocumentNode } from "../../core/index.js";
2import type { QueryRef } from "../internal/index.js";
3import type { LoadableQueryHookOptions } from "../types/types.js";
4import type { FetchMoreFunction, RefetchFunction, SubscribeToMoreFunction } from "./useSuspenseQuery.js";
5import type { DeepPartial, OnlyRequiredProperties } from "../../utilities/index.js";
6export type LoadQueryFunction<TVariables extends OperationVariables> = (...args: [TVariables] extends [never] ? [] : {} extends OnlyRequiredProperties<TVariables> ? [variables?: TVariables] : [variables: TVariables]) => void;
7type ResetFunction = () => void;
8export type UseLoadableQueryResult<TData = unknown, TVariables extends OperationVariables = OperationVariables> = [
9 loadQuery: LoadQueryFunction<TVariables>,
10 queryRef: QueryRef<TData, TVariables> | null,
11 handlers: {
12 /**
13 * A function that helps you fetch the next set of results for a [paginated list field](https://www.apollographql.com/docs/react/pagination/core-api/).
14 *
15 *
16 * @docGroup
17 *
18 * 3. Helper functions
19 */
20 fetchMore: FetchMoreFunction<TData, TVariables>;
21 /**
22 * A function that enables you to re-execute the query, optionally passing in new `variables`.
23 *
24 * To guarantee that the refetch performs a network request, its `fetchPolicy` is set to `network-only` (unless the original query's `fetchPolicy` is `no-cache` or `cache-and-network`, which also guarantee a network request).
25 *
26 * See also [Refetching](https://www.apollographql.com/docs/react/data/queries/#refetching).
27 *
28 * @docGroup
29 *
30 * 3. Helper functions
31 */
32 refetch: RefetchFunction<TData, TVariables>;
33 /**
34 * A function that enables you to execute a [subscription](https://www.apollographql.com/docs/react/data/subscriptions/), usually to subscribe to specific fields that were included in the query.
35 *
36 * This function returns _another_ function that you can call to terminate the subscription.
37 */
38 subscribeToMore: SubscribeToMoreFunction<TData, TVariables>;
39 /**
40 * A function that resets the `queryRef` back to `null`.
41 */
42 reset: ResetFunction;
43 }
44];
45export declare function useLoadableQuery<TData, TVariables extends OperationVariables, TOptions extends LoadableQueryHookOptions>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: LoadableQueryHookOptions & TOptions): UseLoadableQueryResult<TOptions["errorPolicy"] extends "ignore" | "all" ? TOptions["returnPartialData"] extends true ? DeepPartial<TData> | undefined : TData | undefined : TOptions["returnPartialData"] extends true ? DeepPartial<TData> : TData, TVariables>;
46export declare function useLoadableQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: LoadableQueryHookOptions & {
47 returnPartialData: true;
48 errorPolicy: "ignore" | "all";
49}): UseLoadableQueryResult<DeepPartial<TData> | undefined, TVariables>;
50export declare function useLoadableQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: LoadableQueryHookOptions & {
51 errorPolicy: "ignore" | "all";
52}): UseLoadableQueryResult<TData | undefined, TVariables>;
53export declare function useLoadableQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: LoadableQueryHookOptions & {
54 returnPartialData: true;
55}): UseLoadableQueryResult<DeepPartial<TData>, TVariables>;
56/**
57 * A hook for imperatively loading a query, such as responding to a user
58 * interaction.
59 *
60 * > Refer to the [Suspense - Fetching in response to user interaction](https://www.apollographql.com/docs/react/data/suspense#fetching-in-response-to-user-interaction) section for a more in-depth overview of `useLoadableQuery`.
61 *
62 * @example
63 * ```jsx
64 * import { gql, useLoadableQuery } from "@apollo/client";
65 *
66 * const GET_GREETING = gql`
67 * query GetGreeting($language: String!) {
68 * greeting(language: $language) {
69 * message
70 * }
71 * }
72 * `;
73 *
74 * function App() {
75 * const [loadGreeting, queryRef] = useLoadableQuery(GET_GREETING);
76 *
77 * return (
78 * <>
79 * <button onClick={() => loadGreeting({ language: "english" })}>
80 * Load greeting
81 * </button>
82 * <Suspense fallback={<div>Loading...</div>}>
83 * {queryRef && <Hello queryRef={queryRef} />}
84 * </Suspense>
85 * </>
86 * );
87 * }
88 *
89 * function Hello({ queryRef }) {
90 * const { data } = useReadQuery(queryRef);
91 *
92 * return <div>{data.greeting.message}</div>;
93 * }
94 * ```
95 *
96 * @since 3.9.0
97 * @param query - A GraphQL query document parsed into an AST by `gql`.
98 * @param options - Options to control how the query is executed.
99 * @returns A tuple in the form of `[loadQuery, queryRef, handlers]`
100 */
101export declare function useLoadableQuery<TData = unknown, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: LoadableQueryHookOptions): UseLoadableQueryResult<TData, TVariables>;
102export {};
103//# sourceMappingURL=useLoadableQuery.d.ts.map
\No newline at end of file