{"version":3,"file":"useLazyQuery-DTUk-8JG.js","sources":["../../../node_modules/@apollo/client/react/hooks/useLazyQuery.js"],"sourcesContent":["import { __assign } from \"tslib\";\nimport * as React from \"rehackt\";\nimport { mergeOptions } from \"../../utilities/index.js\";\nimport { createMakeWatchQueryOptions, getDefaultFetchPolicy, getObsQueryOptions, toQueryResult, useQueryInternals, } from \"./useQuery.js\";\nimport { useIsomorphicLayoutEffect } from \"./internal/useIsomorphicLayoutEffect.js\";\n// The following methods, when called will execute the query, regardless of\n// whether the useLazyQuery execute function was called before.\nvar EAGER_METHODS = [\n \"refetch\",\n \"reobserve\",\n \"fetchMore\",\n \"updateQuery\",\n \"startPolling\",\n \"stopPolling\",\n \"subscribeToMore\",\n];\n/**\n * A hook for imperatively executing queries in an Apollo application, e.g. in response to user interaction.\n *\n * > Refer to the [Queries - Manual execution with useLazyQuery](https://www.apollographql.com/docs/react/data/queries#manual-execution-with-uselazyquery) section for a more in-depth overview of `useLazyQuery`.\n *\n * @example\n * ```jsx\n * import { gql, useLazyQuery } from \"@apollo/client\";\n *\n * const GET_GREETING = gql`\n * query GetGreeting($language: String!) {\n * greeting(language: $language) {\n * message\n * }\n * }\n * `;\n *\n * function Hello() {\n * const [loadGreeting, { called, loading, data }] = useLazyQuery(\n * GET_GREETING,\n * { variables: { language: \"english\" } }\n * );\n * if (called && loading) return

Loading ...

\n * if (!called) {\n * return \n * }\n * return

Hello {data.greeting.message}!

;\n * }\n * ```\n * @since 3.0.0\n *\n * @param query - A GraphQL query document parsed into an AST by `gql`.\n * @param options - Default options to control how the query is executed.\n * @returns A tuple in the form of `[execute, result]`\n */\nexport function useLazyQuery(query, options) {\n var _a;\n var execOptionsRef = React.useRef();\n var optionsRef = React.useRef();\n var queryRef = React.useRef();\n var merged = mergeOptions(options, execOptionsRef.current || {});\n var document = (_a = merged === null || merged === void 0 ? void 0 : merged.query) !== null && _a !== void 0 ? _a : query;\n // Use refs to track options and the used query to ensure the `execute`\n // function remains referentially stable between renders.\n optionsRef.current = options;\n queryRef.current = document;\n var queryHookOptions = __assign(__assign({}, merged), { skip: !execOptionsRef.current });\n var _b = useQueryInternals(document, queryHookOptions), obsQueryFields = _b.obsQueryFields, useQueryResult = _b.result, client = _b.client, resultData = _b.resultData, observable = _b.observable, onQueryExecuted = _b.onQueryExecuted;\n var initialFetchPolicy = observable.options.initialFetchPolicy ||\n getDefaultFetchPolicy(queryHookOptions.defaultOptions, client.defaultOptions);\n var forceUpdateState = React.useReducer(function (tick) { return tick + 1; }, 0)[1];\n // We use useMemo here to make sure the eager methods have a stable identity.\n var eagerMethods = React.useMemo(function () {\n var eagerMethods = {};\n var _loop_1 = function (key) {\n var method = obsQueryFields[key];\n eagerMethods[key] = function () {\n if (!execOptionsRef.current) {\n execOptionsRef.current = Object.create(null);\n // Only the first time populating execOptionsRef.current matters here.\n forceUpdateState();\n }\n // @ts-expect-error this is just too generic to type\n return method.apply(this, arguments);\n };\n };\n for (var _i = 0, EAGER_METHODS_1 = EAGER_METHODS; _i < EAGER_METHODS_1.length; _i++) {\n var key = EAGER_METHODS_1[_i];\n _loop_1(key);\n }\n return eagerMethods;\n }, [forceUpdateState, obsQueryFields]);\n var called = !!execOptionsRef.current;\n var result = React.useMemo(function () { return (__assign(__assign(__assign({}, useQueryResult), eagerMethods), { called: called })); }, [useQueryResult, eagerMethods, called]);\n var execute = React.useCallback(function (executeOptions) {\n execOptionsRef.current =\n executeOptions ? __assign(__assign({}, executeOptions), { fetchPolicy: executeOptions.fetchPolicy || initialFetchPolicy }) : {\n fetchPolicy: initialFetchPolicy,\n };\n var options = mergeOptions(optionsRef.current, __assign({ query: queryRef.current }, execOptionsRef.current));\n var promise = executeQuery(resultData, observable, client, document, __assign(__assign({}, options), { skip: false }), onQueryExecuted).then(function (queryResult) { return Object.assign(queryResult, eagerMethods); });\n // Because the return value of `useLazyQuery` is usually floated, we need\n // to catch the promise to prevent unhandled rejections.\n promise.catch(function () { });\n return promise;\n }, [\n client,\n document,\n eagerMethods,\n initialFetchPolicy,\n observable,\n resultData,\n onQueryExecuted,\n ]);\n var executeRef = React.useRef(execute);\n useIsomorphicLayoutEffect(function () {\n executeRef.current = execute;\n });\n var stableExecute = React.useCallback(function () {\n var args = [];\n for (var _i = 0; _i < arguments.length; _i++) {\n args[_i] = arguments[_i];\n }\n return executeRef.current.apply(executeRef, args);\n }, []);\n return [stableExecute, result];\n}\nfunction executeQuery(resultData, observable, client, currentQuery, options, onQueryExecuted) {\n var query = options.query || currentQuery;\n var watchQueryOptions = createMakeWatchQueryOptions(client, query, options, false)(observable);\n var concast = observable.reobserveAsConcast(getObsQueryOptions(observable, client, options, watchQueryOptions));\n onQueryExecuted(watchQueryOptions);\n return new Promise(function (resolve) {\n var result;\n // Subscribe to the concast independently of the ObservableQuery in case\n // the component gets unmounted before the promise resolves. This prevents\n // the concast from terminating early and resolving with `undefined` when\n // there are no more subscribers for the concast.\n concast.subscribe({\n next: function (value) {\n result = value;\n },\n error: function () {\n resolve(toQueryResult(observable.getCurrentResult(), resultData.previousData, observable, client));\n },\n complete: function () {\n resolve(toQueryResult(result, resultData.previousData, observable, client));\n },\n });\n });\n}\n//# sourceMappingURL=useLazyQuery.js.map"],"names":["EAGER_METHODS","useLazyQuery","query","options","_a","execOptionsRef","React.useRef","optionsRef","queryRef","merged","mergeOptions","document","queryHookOptions","__assign","_b","useQueryInternals","obsQueryFields","useQueryResult","client","resultData","observable","onQueryExecuted","initialFetchPolicy","getDefaultFetchPolicy","forceUpdateState","React.useReducer","tick","eagerMethods","React.useMemo","_loop_1","key","method","_i","EAGER_METHODS_1","called","result","execute","React.useCallback","executeOptions","promise","executeQuery","queryResult","executeRef","useIsomorphicLayoutEffect","stableExecute","args","currentQuery","watchQueryOptions","createMakeWatchQueryOptions","concast","getObsQueryOptions","resolve","value","toQueryResult"],"mappings":"gMAOA,IAAIA,EAAgB,CAChB,UACA,YACA,YACA,cACA,eACA,cACA,iBACJ,EAoCO,SAASC,EAAaC,EAAOC,EAAS,CACz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