Skip to content

Commit

Permalink
Propagate errors thrown in onCompleted
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Dec 5, 2024
1 parent 3849806 commit 5904a06
Showing 1 changed file with 71 additions and 66 deletions.
137 changes: 71 additions & 66 deletions src/react/hooks/useMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,82 +138,87 @@ export function useMutation<

return client
.mutate(clientOptions as MutationOptions<TData, OperationVariables>)
.then((response) => {
const { data, errors } = response;
const error =
errors && errors.length > 0 ?
new ApolloError({ graphQLErrors: errors })
: void 0;

const onError =
executeOptions.onError || ref.current.options?.onError;

if (error && onError) {
onError(
error,
clientOptions as MutationOptions<TData, OperationVariables>
);
}
.then(
(response) => {
const { data, errors } = response;
const error =
errors && errors.length > 0 ?
new ApolloError({ graphQLErrors: errors })
: void 0;

const onError =
executeOptions.onError || ref.current.options?.onError;

if (error && onError) {
onError(
error,
clientOptions as MutationOptions<TData, OperationVariables>
);
}

if (
mutationId === ref.current.mutationId &&
!clientOptions.ignoreResults
) {
const result = {
called: true,
loading: false,
data,
error,
client,
};

if (ref.current.isMounted && !equal(ref.current.result, result)) {
setResult((ref.current.result = result));
if (
mutationId === ref.current.mutationId &&
!clientOptions.ignoreResults
) {
const result = {
called: true,
loading: false,
data,
error,
client,
};

if (ref.current.isMounted && !equal(ref.current.result, result)) {
setResult((ref.current.result = result));
}
}
}

const onCompleted =
executeOptions.onCompleted || ref.current.options?.onCompleted;
const onCompleted =
executeOptions.onCompleted || ref.current.options?.onCompleted;

if (!error) {
onCompleted?.(
response.data!,
clientOptions as MutationOptions<TData, OperationVariables>
);
}
if (!error) {
onCompleted?.(
response.data!,
clientOptions as MutationOptions<TData, OperationVariables>
);
}

return response;
})
.catch((error) => {
if (mutationId === ref.current.mutationId && ref.current.isMounted) {
const result = {
loading: false,
error,
data: void 0,
called: true,
client,
};

if (!equal(ref.current.result, result)) {
setResult((ref.current.result = result));
return response;
},
(error) => {
if (
mutationId === ref.current.mutationId &&
ref.current.isMounted
) {
const result = {
loading: false,
error,
data: void 0,
called: true,
client,
};

if (!equal(ref.current.result, result)) {
setResult((ref.current.result = result));
}
}
}

const onError =
executeOptions.onError || ref.current.options?.onError;
const onError =
executeOptions.onError || ref.current.options?.onError;

if (onError) {
onError(
error,
clientOptions as MutationOptions<TData, OperationVariables>
);
if (onError) {
onError(
error,
clientOptions as MutationOptions<TData, OperationVariables>
);

// TODO(brian): why are we returning this here???
return { data: void 0, errors: error };
}
// TODO(brian): why are we returning this here???
return { data: void 0, errors: error };
}

throw error;
});
throw error;
}
);
},
[]
);
Expand Down

0 comments on commit 5904a06

Please sign in to comment.