{"version":3,"file":"useMutation-CGp8xGZ8.js","sources":["../../../node_modules/@apollo/client/react/hooks/useMutation.js"],"sourcesContent":["import { __assign } from \"tslib\";\nimport * as React from \"rehackt\";\nimport { mergeOptions } from \"../../utilities/index.js\";\nimport { equal } from \"@wry/equality\";\nimport { DocumentType, verifyDocumentType } from \"../parser/index.js\";\nimport { ApolloError } from \"../../errors/index.js\";\nimport { useApolloClient } from \"./useApolloClient.js\";\n/**\n *\n *\n * > Refer to the [Mutations](https://www.apollographql.com/docs/react/data/mutations/) section for a more in-depth overview of `useMutation`.\n *\n * @example\n * ```jsx\n * import { gql, useMutation } from '@apollo/client';\n *\n * const ADD_TODO = gql`\n * mutation AddTodo($type: String!) {\n * addTodo(type: $type) {\n * id\n * type\n * }\n * }\n * `;\n *\n * function AddTodo() {\n * let input;\n * const [addTodo, { data }] = useMutation(ADD_TODO);\n *\n * return (\n *
\n * {\n * e.preventDefault();\n * addTodo({ variables: { type: input.value } });\n * input.value = '';\n * }}\n * >\n * {\n * input = node;\n * }}\n * />\n * \n * \n *
\n * );\n * }\n * ```\n * @since 3.0.0\n * @param mutation - A GraphQL mutation document parsed into an AST by `gql`.\n * @param options - Options to control how the mutation is executed.\n * @returns A tuple in the form of `[mutate, result]`\n */\nexport function useMutation(mutation, options) {\n var client = useApolloClient(options === null || options === void 0 ? void 0 : options.client);\n verifyDocumentType(mutation, DocumentType.Mutation);\n var _a = React.useState({\n called: false,\n loading: false,\n client: client,\n }), result = _a[0], setResult = _a[1];\n var ref = React.useRef({\n result: result,\n mutationId: 0,\n isMounted: true,\n client: client,\n mutation: mutation,\n options: options,\n });\n // TODO: Trying to assign these in a useEffect or useLayoutEffect breaks\n // higher-order components.\n {\n Object.assign(ref.current, { client: client, options: options, mutation: mutation });\n }\n var execute = React.useCallback(function (executeOptions) {\n if (executeOptions === void 0) { executeOptions = {}; }\n var _a = ref.current, options = _a.options, mutation = _a.mutation;\n var baseOptions = __assign(__assign({}, options), { mutation: mutation });\n var client = executeOptions.client || ref.current.client;\n if (!ref.current.result.loading &&\n !baseOptions.ignoreResults &&\n ref.current.isMounted) {\n setResult((ref.current.result = {\n loading: true,\n error: void 0,\n data: void 0,\n called: true,\n client: client,\n }));\n }\n var mutationId = ++ref.current.mutationId;\n var clientOptions = mergeOptions(baseOptions, executeOptions);\n return client\n .mutate(clientOptions)\n .then(function (response) {\n var _a, _b;\n var data = response.data, errors = response.errors;\n var error = errors && errors.length > 0 ?\n new ApolloError({ graphQLErrors: errors })\n : void 0;\n var onError = executeOptions.onError || ((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onError);\n if (error && onError) {\n onError(error, clientOptions);\n }\n if (mutationId === ref.current.mutationId &&\n !clientOptions.ignoreResults) {\n var result_1 = {\n called: true,\n loading: false,\n data: data,\n error: error,\n client: client,\n };\n if (ref.current.isMounted && !equal(ref.current.result, result_1)) {\n setResult((ref.current.result = result_1));\n }\n }\n var onCompleted = executeOptions.onCompleted || ((_b = ref.current.options) === null || _b === void 0 ? void 0 : _b.onCompleted);\n if (!error) {\n onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(response.data, clientOptions);\n }\n return response;\n })\n .catch(function (error) {\n var _a;\n if (mutationId === ref.current.mutationId && ref.current.isMounted) {\n var result_2 = {\n loading: false,\n error: error,\n data: void 0,\n called: true,\n client: client,\n };\n if (!equal(ref.current.result, result_2)) {\n setResult((ref.current.result = result_2));\n }\n }\n var onError = executeOptions.onError || ((_a = ref.current.options) === null || _a === void 0 ? void 0 : _a.onError);\n if (onError) {\n onError(error, clientOptions);\n // TODO(brian): why are we returning this here???\n return { data: void 0, errors: error };\n }\n throw error;\n });\n }, []);\n var reset = React.useCallback(function () {\n if (ref.current.isMounted) {\n var result_3 = { called: false, loading: false, client: client };\n Object.assign(ref.current, { mutationId: 0, result: result_3 });\n setResult(result_3);\n }\n }, []);\n React.useEffect(function () {\n ref.current.isMounted = true;\n return function () {\n ref.current.isMounted = false;\n };\n }, []);\n return [execute, __assign({ reset: reset }, result)];\n}\n//# sourceMappingURL=useMutation.js.map"],"names":["useMutation","mutation","options","client","useApolloClient","verifyDocumentType","DocumentType","_a","React.useState","result","setResult","ref","React.useRef","execute","React.useCallback","executeOptions","baseOptions","__assign","mutationId","clientOptions","mergeOptions","response","_b","data","errors","error","ApolloError","onError","result_1","equal","onCompleted","result_2","reset","result_3","React.useEffect"],"mappings":"wOAsDO,SAASA,EAAYC,EAAUC,EAAS,CAC3C,IAAIC,EAASC,EAAgBF,GAAY,KAA6B,OAASA,EAAQ,MAAM,EAC7FG,EAAmBJ,EAAUK,EAAa,QAAQ,EAClD,IAAIC,EAAKC,EAAAA,SAAe,CACpB,OAAQ,GACR,QAAS,GACT,OAAQL,CAChB,CAAK,EAAGM,EAASF,EAAG,CAAC,EAAGG,EAAYH,EAAG,CAAC,EAChCI,EAAMC,EAAAA,OAAa,CACnB,OAAQH,EACR,WAAY,EACZ,UAAW,GACX,OAAQN,EACR,SAAUF,EACV,QAASC,CACjB,CAAK,EAIG,OAAO,OAAOS,EAAI,QAAS,CAAE,OAAQR,EAAQ,QAASD,EAAS,SAAUD,CAAU,CAAA,EAEvF,IAAIY,EAAUC,cAAkB,SAAUC,EAAgB,CAClDA,IAAmB,SAAUA,EAAiB,CAAE,GACpD,IAAIR,EAAKI,EAAI,QAAST,EAAUK,EAAG,QAASN,EAAWM,EAAG,SACtDS,EAAcC,EAASA,EAAS,CAAE,EAAEf,CAAO,EAAG,CAAE,SAAUD,CAAQ,CAAE,EACpEE,EAASY,EAAe,QAAUJ,EAAI,QAAQ,OAC9C,CAACA,EAAI,QAAQ,OAAO,SACpB,CAACK,EAAY,eACbL,EAAI,QAAQ,WACZD,EAAWC,EAAI,QAAQ,OAAS,CAC5B,QAAS,GACT,MAAO,OACP,KAAM,OACN,OAAQ,GACR,OAAQR,CACxB,GAEQ,IAAIe,EAAa,EAAEP,EAAI,QAAQ,WAC3BQ,EAAgBC,EAAaJ,EAAaD,CAAc,EAC5D,OAAOZ,EACF,OAA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