-
Notifications
You must be signed in to change notification settings - Fork 3
/
useApollo.ts
106 lines (95 loc) Β· 2.93 KB
/
useApollo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* eslint-disable @typescript-eslint/ban-types */
import {
useApolloClient,
useQuery,
QueryHookOptions,
MutationHookOptions,
ApolloError,
ServerParseError,
QueryResult,
ApolloClient,
ApolloQueryResult,
useMutation,
MutationTuple,
OperationVariables,
} from '@apollo/react-hooks';
import { DocumentNode } from 'graphql';
import { REQUEST_TOKEN } from '@queries/token';
import { RequestToken } from '@/types/api';
const SUCCESS = 'success';
interface QueryWrapper<TData, TVariables> extends QueryResult<TData, TVariables> {
callQuery: (variables?: Partial<TVariables>) => Promise<ApolloQueryResult<TData>>;
}
const errorHandler = async <TData>(
error: ApolloError,
apolloClient: ApolloClient<object>,
request: (...args: any) => TData,
) => {
const { statusCode } = (error.networkError as ServerParseError) || 0;
if (statusCode !== 401) {
throw new ApolloError(error);
}
const result = await apolloClient.mutate<RequestToken>({ mutation: REQUEST_TOKEN });
if (result?.data?.requestToken.result === SUCCESS) {
const reResponseData = await request();
return reResponseData;
}
throw new ApolloError(error);
};
export const useCustomQuery = <TData = any, TVariables = OperationVariables>(
query: DocumentNode,
options?: QueryHookOptions<TData, TVariables>,
): QueryWrapper<TData, TVariables> => {
const apolloClient = useApolloClient();
const queryResult = useQuery<TData, TVariables>(query, {
...options,
onError: (error: ApolloError) => {
errorHandler(error, apolloClient, queryResult.refetch)
.then(async (reQueryResult) => {
const { data } = await reQueryResult;
onStartCompleted(data);
})
.catch((apolloError) => {
if (options?.onError) {
options.onError(apolloError);
}
});
},
});
const callQuery = async (variables?: Partial<TVariables>) => {
try {
const result = await queryResult.refetch(variables);
onStartCompleted(result.data);
return result;
} catch (error) {
const result = await errorHandler(error, apolloClient, queryResult.refetch).catch(
(err) => err,
);
onStartCompleted(result.data);
return result;
}
};
return { ...queryResult, callQuery };
function onStartCompleted(data: TData) {
if (options?.onCompleted) {
options.onCompleted(data);
}
}
};
export const useCustomMutation = <TData = any, TVariables = OperationVariables>(
query: DocumentNode,
options?: MutationHookOptions<TData, TVariables>,
): MutationTuple<TData, TVariables> => {
const apolloClient = useApolloClient();
const mutationTuple = useMutation<TData, TVariables>(query, {
...options,
onError: (error: ApolloError) => {
errorHandler(error, apolloClient, mutationTuple[0]).catch((apolloError) => {
if (options?.onError) {
options.onError(apolloError);
}
});
},
});
return mutationTuple;
};