@@ -21,7 +21,10 @@ import type {
2121} from "@apollo/client" ;
2222import type { IgnoreModifier } from "@apollo/client/cache" ;
2323import type { NoInfer , Prettify } from "@apollo/client/utilities/internal" ;
24- import { mergeOptions } from "@apollo/client/utilities/internal" ;
24+ import {
25+ mergeOptions ,
26+ preventUnhandledRejection ,
27+ } from "@apollo/client/utilities/internal" ;
2528
2629import { useIsomorphicLayoutEffect } from "./internal/useIsomorphicLayoutEffect.js" ;
2730import { useApolloClient } from "./useApolloClient.js" ;
@@ -284,72 +287,80 @@ export function useMutation<
284287 const mutationId = ++ ref . current . mutationId ;
285288 const clientOptions = mergeOptions ( baseOptions , executeOptions as any ) ;
286289
287- return client
288- . mutate (
289- clientOptions as ApolloClient . MutateOptions < TData , OperationVariables >
290- )
291- . then (
292- ( response ) => {
293- const { data, error } = response ;
290+ return preventUnhandledRejection (
291+ client
292+ . mutate (
293+ clientOptions as ApolloClient . MutateOptions <
294+ TData ,
295+ OperationVariables
296+ >
297+ )
298+ . then (
299+ ( response ) => {
300+ const { data, error } = response ;
301+
302+ const onError =
303+ executeOptions . onError || ref . current . options ?. onError ;
304+
305+ if ( error && onError ) {
306+ onError ( error , clientOptions ) ;
307+ }
294308
295- const onError =
296- executeOptions . onError || ref . current . options ?. onError ;
309+ if ( mutationId === ref . current . mutationId ) {
310+ const result = {
311+ called : true ,
312+ loading : false ,
313+ data,
314+ error,
315+ client,
316+ } ;
317+
318+ if (
319+ ref . current . isMounted &&
320+ ! equal ( ref . current . result , result )
321+ ) {
322+ setResult ( ( ref . current . result = result ) ) ;
323+ }
324+ }
297325
298- if ( error && onError ) {
299- onError ( error , clientOptions ) ;
300- }
326+ const onCompleted =
327+ executeOptions . onCompleted || ref . current . options ?. onCompleted ;
301328
302- if ( mutationId === ref . current . mutationId ) {
303- const result = {
304- called : true ,
305- loading : false ,
306- data,
307- error,
308- client,
309- } ;
310-
311- if ( ref . current . isMounted && ! equal ( ref . current . result , result ) ) {
312- setResult ( ( ref . current . result = result ) ) ;
329+ if ( ! error ) {
330+ onCompleted ?.( response . data ! , clientOptions ) ;
313331 }
314- }
315332
316- const onCompleted =
317- executeOptions . onCompleted || ref . current . options ?. onCompleted ;
333+ return response ;
334+ } ,
335+ ( error ) => {
336+ if (
337+ mutationId === ref . current . mutationId &&
338+ ref . current . isMounted
339+ ) {
340+ const result = {
341+ loading : false ,
342+ error,
343+ data : void 0 ,
344+ called : true ,
345+ client,
346+ } ;
347+
348+ if ( ! equal ( ref . current . result , result ) ) {
349+ setResult ( ( ref . current . result = result ) ) ;
350+ }
351+ }
318352
319- if ( ! error ) {
320- onCompleted ?.( response . data ! , clientOptions ) ;
321- }
353+ const onError =
354+ executeOptions . onError || ref . current . options ?. onError ;
322355
323- return response ;
324- } ,
325- ( error ) => {
326- if (
327- mutationId === ref . current . mutationId &&
328- ref . current . isMounted
329- ) {
330- const result = {
331- loading : false ,
332- error,
333- data : void 0 ,
334- called : true ,
335- client,
336- } ;
337-
338- if ( ! equal ( ref . current . result , result ) ) {
339- setResult ( ( ref . current . result = result ) ) ;
356+ if ( onError ) {
357+ onError ( error , clientOptions ) ;
340358 }
341- }
342-
343- const onError =
344- executeOptions . onError || ref . current . options ?. onError ;
345359
346- if ( onError ) {
347- onError ( error , clientOptions ) ;
360+ throw error ;
348361 }
349-
350- throw error ;
351- }
352- ) ;
362+ )
363+ ) ;
353364 } ,
354365 [ ]
355366 ) ;
0 commit comments