{"version":3,"file":"index.cjs","sources":["../src/useLazyQuery.ts"],"sourcesContent":["import type { DocumentNode, MaybeMasked, OperationVariables, TypedDocumentNode } from '@apollo/client'\nimport type { MaybeRefOrGetter, ShallowRef } from '@vue/reactivity'\nimport type { useQuery } from './useQuery.ts'\nimport { until } from '@vueuse/core'\nimport { useQueryImpl } from './useQuery.ts'\n\n// #region Types\nexport declare namespace useLazyQuery {\n  // Re-export useQuery types for convenience\n  export type { useQuery }\n\n  /** Options for useLazyQuery - same as useQuery but `enabled` is not applicable. */\n  export type Options<\n    TData = unknown,\n    TVariables extends OperationVariables = OperationVariables,\n  > = Omit<useQuery.Options<TData, TVariables>, 'enabled'>\n\n  /** Options when query is disabled. */\n  export type DisabledOptions<\n    TData = unknown,\n    TVariables extends OperationVariables = OperationVariables,\n  > = Omit<useQuery.DisabledOptions<TData, TVariables>, 'enabled'>\n\n  /** Result returned by useLazyQuery - extends useQuery.Result with `load` function. */\n  export interface Result<\n    TData = unknown,\n    TVariables extends OperationVariables = OperationVariables,\n  > extends useQuery.Result<TData, TVariables> {\n    /**\n     * Load the query. Starts the query if not already started.\n     *\n     * Unlike `useQuery`, which executes immediately, `useLazyQuery` waits until\n     * `load()` is called. After loading, the query behaves like a normal `useQuery`.\n     *\n     * @param variables - Optional variables to use for this load. If provided,\n     *                    these will be merged with any variables from options.\n     * @returns Promise resolving to the query result data when complete.\n     *\n     * @example\n     * ```ts\n     * const { load, result, loading } = useLazyQuery(GetUser)\n     *\n     * // Query doesn't execute until load() is called\n     * async function fetchUser(id: string) {\n     *   const data = await load({ id })\n     *   console.log('User loaded:', data)\n     * }\n     * ```\n     *\n     * @group 4. Lifecycle\n     */\n    load: (\n      variables?: TVariables,\n    ) => Promise<MaybeMasked<TData> | undefined>\n  }\n\n  export namespace DocumentationTypes {\n    /** @group Composables Namespaces */\n    namespace useLazyQuery {\n      /** {@inheritDoc @vue/apollo-composable!useLazyQuery.Options:type} */\n      export interface Options extends Omit<useQuery.DocumentationTypes.useQuery.Options, 'enabled'> {}\n\n      /** {@inheritDoc @vue/apollo-composable!useLazyQuery.Result:interface} */\n      export interface Result extends useQuery.DocumentationTypes.useQuery.Result {\n        /** {@inheritDoc @vue/apollo-composable!useLazyQuery.Result#load:member} */\n        load: (variables?: OperationVariables) => Promise<object | undefined>\n      }\n    }\n\n    /** {@inheritDoc @vue/apollo-composable!useLazyQuery:function(1)} */\n    export function useLazyQuery(\n      query: MaybeRefOrGetter<DocumentNode>,\n      options?: MaybeRefOrGetter<useLazyQuery.Options>,\n    ): useLazyQuery.Result\n  }\n}\n// #endregion\n\n// #region Implementation\n/**\n * A composable for executing GraphQL queries lazily (on demand).\n *\n * Unlike `useQuery`, which executes immediately, `useLazyQuery` waits until\n * `load()` is called. This is useful for queries that should only run in\n * response to user actions (e.g., search, form submission).\n *\n * @param query - A GraphQL document.\n * @param options - Options to control how the query is executed.\n * @returns Query result object with reactive refs and `load` function.\n *\n * @group Composables\n *\n * @example\n * ```vue\n * <script setup lang=\"ts\">\n * import { useLazyQuery } from '@vue/apollo-composable'\n * import gql from 'graphql-tag'\n *\n * const SearchUsers = gql`query SearchUsers($term: String!) { users(search: $term) { id name } }`\n *\n * const { load, result, loading } = useLazyQuery(SearchUsers)\n *\n * async function search(term: string) {\n *   const data = await load({ term })\n *   console.log('Found users:', data?.users)\n * }\n * </script>\n *\n * <template>\n *   <input @keyup.enter=\"search($event.target.value)\" />\n *   <div v-if=\"loading\">Searching...</div>\n *   <ul v-else-if=\"result\">\n *     <li v-for=\"user in result.users\" :key=\"user.id\">{{ user.name }}</li>\n *   </ul>\n * </template>\n * ```\n */\nexport function useLazyQuery<\n  TData = unknown,\n  TVariables extends OperationVariables = OperationVariables,\n>(\n  query: MaybeRefOrGetter<DocumentNode | TypedDocumentNode<TData, TVariables>>,\n  options?: MaybeRefOrGetter<useLazyQuery.Options<TData, TVariables>>,\n): useLazyQuery.Result<TData, TVariables> {\n  // Use the internal implementation with lazy=true\n  const queryResult = useQueryImpl<TData, TVariables>(query, options as any, true)\n\n  // Cast to mutable ref since we need to update variables\n  const variablesRef = queryResult.variables as ShallowRef<TVariables>\n\n  /**\n   * Load the query with optional variables.\n   * Variables are merged with composable-level variables.\n   */\n  async function load(variables?: TVariables): Promise<MaybeMasked<TData> | undefined> {\n    // If variables are provided, merge with existing variables and re-assign\n    if (variables != null) {\n      variablesRef.value = { ...variablesRef.value, ...variables }\n    }\n\n    // Start the query\n    queryResult.start()\n\n    // Wait for the query to complete or error. A retained result belongs to the previous\n    // variables, so it does not count as having loaded.\n    await until(() =>\n      (queryResult.current.value.resultState === 'complete'\n        && !queryResult.current.value.isPreviousResult)\n      || queryResult.current.value.error != null,\n    ).toBe(true, { timeout: 30000 })\n\n    // If there's an error, throw it\n    if (queryResult.error.value) {\n      throw queryResult.error.value\n    }\n\n    return queryResult.result.value as MaybeMasked<TData>\n  }\n\n  return {\n    ...queryResult,\n    load,\n  } as useLazyQuery.Result<TData, TVariables>\n}\n// #endregion\n"],"names":["useQueryImpl","until"],"mappings":";;;;;;;;;AAqHO,SAAS,YAAA,CAId,OACA,OAAA,EACwC;AAExC,EAAA,MAAM,WAAA,GAAcA,4BAAA,CAAgC,KAAA,EAAO,OAAA,EAAgB,IAAI,CAAA;AAG/E,EAAA,MAAM,eAAe,WAAA,CAAY,SAAA;AAMjC,EAAA,eAAe,KAAK,SAAA,EAAiE;AAEnF,IAAA,IAAI,aAAa,IAAA,EAAM;AACrB,MAAA,YAAA,CAAa,QAAQ,EAAE,GAAG,YAAA,CAAa,KAAA,EAAO,GAAG,SAAA,EAAU;AAAA,IAC7D;AAGA,IAAA,WAAA,CAAY,KAAA,EAAM;AAIlB,IAAA,MAAMC,UAAA;AAAA,MAAM,MACT,WAAA,CAAY,OAAA,CAAQ,KAAA,CAAM,gBAAgB,UAAA,IACtC,CAAC,WAAA,CAAY,OAAA,CAAQ,KAAA,CAAM,gBAAA,IAC7B,WAAA,CAAY,OAAA,CAAQ,MAAM,KAAA,IAAS;AAAA,MACtC,IAAA,CAAK,IAAA,EAAM,EAAE,OAAA,EAAS,KAAO,CAAA;AAG/B,IAAA,IAAI,WAAA,CAAY,MAAM,KAAA,EAAO;AAC3B,MAAA,MAAM,YAAY,KAAA,CAAM,KAAA;AAAA,IAC1B;AAEA,IAAA,OAAO,YAAY,MAAA,CAAO,KAAA;AAAA,EAC5B;AAEA,EAAA,OAAO;AAAA,IACL,GAAG,WAAA;AAAA,IACH;AAAA,GACF;AACF;;;;;;;;;;;;;;;;;;;"}