-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathhooks.ts
37 lines (35 loc) · 891 Bytes
/
hooks.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
"use client";
import {
useFragment_experimental,
useSuspenseQuery_experimental,
useQuery as orig_useQuery,
} from "@apollo/client";
import { useTransportValue } from "./useTransportValue";
export const useFragment = wrap(useFragment_experimental, [
"data",
"complete",
"missing",
]);
export const useQuery = wrap(orig_useQuery, [
"data",
"loading",
"networkStatus",
"called",
]);
export const useSuspenseQuery = wrap(useSuspenseQuery_experimental, [
"data",
"networkStatus",
]);
function wrap<T extends (...args: any[]) => any>(
useFn: T,
transportKeys: (keyof ReturnType<T>)[]
): T {
return ((...args: any[]) => {
const result = useFn(...args);
const transported: Partial<typeof result> = {};
for (const key of transportKeys) {
transported[key] = result[key];
}
return { ...result, ...useTransportValue(transported) };
}) as T;
}