Skip to content

Commit

Permalink
clean up hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
brainkim committed Aug 13, 2021
1 parent ab254b8 commit 95ebc09
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/react/hooks/useLazyQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function useLazyQuery<TData = any, TVariables = OperationVariables>(
>((lazyOptions?: QueryLazyOptions<TVariables>) => {
setExecution((execution) => {
if (execution.called) {
result && result.refetch(execution.lazyOptions);
result && result.refetch(lazyOptions);
}

return { called: true, lazyOptions };
Expand Down
94 changes: 46 additions & 48 deletions src/react/hooks/useMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -66,61 +65,60 @@ export function useMutation<
{ ...options, mutation },
executeOptions as any,
);
return client.mutate(clientOptions)
.then((response: FetchResult<TData>) => {
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(() => () => {
Expand Down
2 changes: 1 addition & 1 deletion src/react/hooks/useSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function useSubscription<TData = any, TVariables = OperationVariables>(
});
});

const ref = useRef({ client, subscription, options, observable });
const ref = useRef({ client, subscription, options });
useEffect(() => {
let shouldResubscribe = options?.shouldResubscribe;
if (typeof shouldResubscribe === 'function') {
Expand Down

0 comments on commit 95ebc09

Please sign in to comment.