UNPKG

5 kBTypeScriptView Raw
1import type { ApolloClient, DefaultOptions, OperationVariables, WatchQueryFetchPolicy } from "../../core/index.js";
2import { ApolloError } from "../../errors/index.js";
3import type { ApolloQueryResult, ObservableQuery, DocumentNode, TypedDocumentNode, WatchQueryOptions } from "../../core/index.js";
4import type { QueryHookOptions, QueryResult, ObservableQueryFields, NoInfer } from "../types/types.js";
5import { useApolloClient } from "./useApolloClient.js";
6type InternalQueryResult<TData, TVariables extends OperationVariables> = Omit<QueryResult<TData, TVariables>, Exclude<keyof ObservableQueryFields<TData, TVariables>, "variables">>;
7export declare const lastWatchOptions: unique symbol;
8export interface ObsQueryWithMeta<TData, TVariables extends OperationVariables> extends ObservableQuery<TData, TVariables> {
9 [lastWatchOptions]?: WatchQueryOptions<TVariables, TData>;
10}
11export interface InternalResult<TData, TVariables extends OperationVariables> {
12 current?: undefined | InternalQueryResult<TData, TVariables>;
13 previousData?: undefined | TData;
14}
15interface InternalState<TData, TVariables extends OperationVariables> {
16 client: ReturnType<typeof useApolloClient>;
17 query: DocumentNode | TypedDocumentNode<TData, TVariables>;
18 observable: ObsQueryWithMeta<TData, TVariables>;
19 resultData: InternalResult<TData, TVariables>;
20}
21export type UpdateInternalState<TData, TVariables extends OperationVariables> = (state: InternalState<TData, TVariables>) => void;
22/**
23 * A hook for executing queries in an Apollo application.
24 *
25 * To run a query within a React component, call `useQuery` and pass it a GraphQL query document.
26 *
27 * When your component renders, `useQuery` returns an object from Apollo Client that contains `loading`, `error`, and `data` properties you can use to render your UI.
28 *
29 * > Refer to the [Queries](https://www.apollographql.com/docs/react/data/queries) section for a more in-depth overview of `useQuery`.
30 *
31 * @example
32 * ```jsx
33 * import { gql, useQuery } from '@apollo/client';
34 *
35 * const GET_GREETING = gql`
36 * query GetGreeting($language: String!) {
37 * greeting(language: $language) {
38 * message
39 * }
40 * }
41 * `;
42 *
43 * function Hello() {
44 * const { loading, error, data } = useQuery(GET_GREETING, {
45 * variables: { language: 'english' },
46 * });
47 * if (loading) return <p>Loading ...</p>;
48 * return <h1>Hello {data.greeting.message}!</h1>;
49 * }
50 * ```
51 * @since 3.0.0
52 * @param query - A GraphQL query document parsed into an AST by `gql`.
53 * @param options - Options to control how the query is executed.
54 * @returns Query result object
55 */
56export declare function useQuery<TData = any, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options?: QueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>): QueryResult<TData, TVariables>;
57export declare function useQueryInternals<TData = any, TVariables extends OperationVariables = OperationVariables>(query: DocumentNode | TypedDocumentNode<TData, TVariables>, options: QueryHookOptions<NoInfer<TData>, NoInfer<TVariables>>): {
58 result: InternalQueryResult<TData, TVariables> | InternalQueryResult<any, TVariables>;
59 obsQueryFields: Omit<ObservableQueryFields<TData, TVariables>, "variables">;
60 observable: ObsQueryWithMeta<TData, TVariables>;
61 resultData: InternalResult<TData, TVariables>;
62 client: ApolloClient<object>;
63 onQueryExecuted: (watchQueryOptions: WatchQueryOptions<TVariables, TData>) => void;
64};
65export declare function createMakeWatchQueryOptions<TData = any, TVariables extends OperationVariables = OperationVariables>(client: ApolloClient<object>, query: DocumentNode | TypedDocumentNode<TData, TVariables>, { skip, ssr, onCompleted, onError, defaultOptions, ...otherOptions }: QueryHookOptions<TData, TVariables> | undefined, isSyncSSR: boolean): (observable?: ObservableQuery<TData, TVariables>) => WatchQueryOptions<TVariables, TData>;
66export declare function getObsQueryOptions<TData, TVariables extends OperationVariables>(observable: ObservableQuery<TData, TVariables> | undefined, client: ApolloClient<object>, queryHookOptions: QueryHookOptions<TData, TVariables>, watchQueryOptions: Partial<WatchQueryOptions<TVariables, TData>>): WatchQueryOptions<TVariables, TData>;
67export declare function getDefaultFetchPolicy<TData, TVariables extends OperationVariables>(queryHookDefaultOptions?: Partial<WatchQueryOptions<TVariables, TData>>, clientDefaultOptions?: DefaultOptions): WatchQueryFetchPolicy;
68export declare function toApolloError<TData>(result: Pick<ApolloQueryResult<TData>, "errors" | "error">): ApolloError | undefined;
69export declare function toQueryResult<TData, TVariables extends OperationVariables>(result: ApolloQueryResult<TData>, previousData: TData | undefined, observable: ObservableQuery<TData, TVariables>, client: ApolloClient<object>): InternalQueryResult<TData, TVariables>;
70export {};
71//# sourceMappingURL=useQuery.d.ts.map
\No newline at end of file