From 7b95b2468594db8cffec4db4f96f60131bffcc7a Mon Sep 17 00:00:00 2001 From: Brian Kim Date: Wed, 18 Aug 2021 18:12:31 -0400 Subject: [PATCH] tweak useMutation --- .../__tests__/mutations/lifecycle.test.tsx | 1 + src/react/hooks/useMutation.ts | 41 ++++++++++--------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/react/hoc/__tests__/mutations/lifecycle.test.tsx b/src/react/hoc/__tests__/mutations/lifecycle.test.tsx index 13c188c9d0f..4834da4324b 100644 --- a/src/react/hoc/__tests__/mutations/lifecycle.test.tsx +++ b/src/react/hoc/__tests__/mutations/lifecycle.test.tsx @@ -77,6 +77,7 @@ describe('graphql(mutation) lifecycle', () => { interface Props { listId: number; } + function options(props: Props) { return { variables: { diff --git a/src/react/hooks/useMutation.ts b/src/react/hooks/useMutation.ts index ade44580ebf..ebff8f5230f 100644 --- a/src/react/hooks/useMutation.ts +++ b/src/react/hooks/useMutation.ts @@ -50,7 +50,9 @@ export function useMutation< TCache > = {}, ) => { - if (!ref.current.result.loading && !options?.ignoreResults) { + + const baseOptions = { ...options, mutation }; + if (!ref.current.result.loading && !baseOptions.ignoreResults) { setResult(ref.current.result = { loading: true, error: void 0, @@ -62,17 +64,21 @@ export function useMutation< const mutationId = ++ref.current.mutationId; const clientOptions = mergeOptions( - { ...options, mutation }, + baseOptions, executeOptions as any, ); - return client.mutate(clientOptions).then((response) => { + + return client.mutate(clientOptions).then((response) =>{ const { data, errors } = response; const error = errors && errors.length > 0 ? new ApolloError({ graphQLErrors: errors }) : void 0; - if (mutationId === ref.current.mutationId && !options?.ignoreResults) { + if ( + mutationId === ref.current.mutationId && + !baseOptions.ignoreResults + ) { const result = { called: true, loading: false, @@ -81,19 +87,18 @@ export function useMutation< client, }; - if ( - ref.current.isMounted && - !equal(ref.current.result, result) - ) { - ref.current.result = result; - setResult(result); + if (ref.current.isMounted && !equal(ref.current.result, result)) { + setResult(ref.current.result = result); } } - options?.onCompleted?.(data!); + baseOptions.onCompleted?.(response.data!); return response; }).catch((error) => { - if (mutationId === ref.current.mutationId) { + if ( + mutationId === ref.current.mutationId && + ref.current.isMounted + ) { const result = { loading: false, error, @@ -102,17 +107,13 @@ export function useMutation< client, }; - if ( - ref.current.isMounted && - !equal(ref.current.result, result) - ) { - ref.current.result = result; - setResult(result); + if (!equal(ref.current.result, result)) { + setResult(ref.current.result = result); } } - if (options?.onError) { - options.onError(error); + if (baseOptions.onError) { + baseOptions.onError(error); // TODO(brian): why are we returning this here??? return { data: void 0, errors: error }; }