From 95ebc09a70719dedcaaa1e713eb5450ff540bd35 Mon Sep 17 00:00:00 2001 From: Brian Kim Date: Thu, 12 Aug 2021 21:03:41 -0400 Subject: [PATCH] clean up hooks --- src/react/hooks/useLazyQuery.ts | 2 +- src/react/hooks/useMutation.ts | 94 +++++++++++++++--------------- src/react/hooks/useSubscription.ts | 2 +- 3 files changed, 48 insertions(+), 50 deletions(-) diff --git a/src/react/hooks/useLazyQuery.ts b/src/react/hooks/useLazyQuery.ts index d6882d85cc5..9e5b76532bd 100644 --- a/src/react/hooks/useLazyQuery.ts +++ b/src/react/hooks/useLazyQuery.ts @@ -26,7 +26,7 @@ export function useLazyQuery( >((lazyOptions?: QueryLazyOptions) => { setExecution((execution) => { if (execution.called) { - result && result.refetch(execution.lazyOptions); + result && result.refetch(lazyOptions); } return { called: true, lazyOptions }; diff --git a/src/react/hooks/useMutation.ts b/src/react/hooks/useMutation.ts index e0babac1ff3..ade44580ebf 100644 --- a/src/react/hooks/useMutation.ts +++ b/src/react/hooks/useMutation.ts @@ -17,7 +17,6 @@ import { import { equal } from '@wry/equality'; import { DocumentType, verifyDocumentType } from '../parser'; import { ApolloError } from '../../errors'; -import { FetchResult } from '../../link/core'; import { useApolloClient } from './useApolloClient'; export function useMutation< @@ -66,61 +65,60 @@ export function useMutation< { ...options, mutation }, executeOptions as any, ); - return client.mutate(clientOptions) - .then((response: FetchResult) => { - const { data, errors } = response; - const error = - errors && errors.length > 0 - ? new ApolloError({ graphQLErrors: errors }) - : void 0; + 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) { - const result = { - called: true, - loading: false, - data, - error, - client, - }; + if (mutationId === ref.current.mutationId && !options?.ignoreResults) { + const result = { + called: true, + loading: false, + data, + error, + 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) + ) { + ref.current.result = result; + setResult(result); } + } - options?.onCompleted?.(data!); - return response; - }).catch((error) => { - if (mutationId === ref.current.mutationId) { - const result = { - loading: false, - error, - data: void 0, - called: true, - client, - }; + options?.onCompleted?.(data!); + return response; + }).catch((error) => { + if (mutationId === ref.current.mutationId) { + const result = { + loading: false, + error, + data: void 0, + called: true, + 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) + ) { + ref.current.result = result; + setResult(result); } + } - if (options?.onError) { - options.onError(error); - // TODO(brian): why are we returning this here??? - return { data: void 0, errors: error }; - } + if (options?.onError) { + options.onError(error); + // TODO(brian): why are we returning this here??? + return { data: void 0, errors: error }; + } - throw error; - }); + throw error; + }); }, [client, options, mutation]); useEffect(() => () => { diff --git a/src/react/hooks/useSubscription.ts b/src/react/hooks/useSubscription.ts index ea349b05a1d..bddc35c6c7b 100644 --- a/src/react/hooks/useSubscription.ts +++ b/src/react/hooks/useSubscription.ts @@ -37,7 +37,7 @@ export function useSubscription( }); }); - const ref = useRef({ client, subscription, options, observable }); + const ref = useRef({ client, subscription, options }); useEffect(() => { let shouldResubscribe = options?.shouldResubscribe; if (typeof shouldResubscribe === 'function') {