-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
useFragment.ts
144 lines (129 loc) · 4.13 KB
/
useFragment.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import * as React from "rehackt";
import { equal } from "@wry/equality";
import type { DeepPartial } from "../../utilities/index.js";
import { mergeDeepArray } from "../../utilities/index.js";
import type {
Cache,
Reference,
StoreObject,
MissingTree,
} from "../../cache/index.js";
import { useApolloClient } from "./useApolloClient.js";
import { useSyncExternalStore } from "./useSyncExternalStore.js";
import type { ApolloClient, OperationVariables } from "../../core/index.js";
import type { NoInfer } from "../types/types.js";
import { useDeepMemo, useLazyRef, wrapHook } from "./internal/index.js";
export interface UseFragmentOptions<TData, TVars>
extends Omit<
Cache.DiffOptions<NoInfer<TData>, NoInfer<TVars>>,
"id" | "query" | "optimistic" | "previousResult" | "returnPartialData"
>,
Omit<
Cache.ReadFragmentOptions<TData, TVars>,
"id" | "variables" | "returnPartialData"
> {
from: StoreObject | Reference | string;
// Override this field to make it optional (default: true).
optimistic?: boolean;
/**
* The instance of `ApolloClient` to use to look up the fragment.
*
* By default, the instance that's passed down via context is used, but you
* can provide a different instance here.
*
* @docGroup 1. Operation options
*/
client?: ApolloClient<any>;
}
export type UseFragmentResult<TData> =
| {
data: TData;
complete: true;
missing?: never;
}
| {
data: DeepPartial<TData>;
complete: false;
missing?: MissingTree;
};
export function useFragment<TData = any, TVars = OperationVariables>(
options: UseFragmentOptions<TData, TVars>
): UseFragmentResult<TData> {
return wrapHook(
"useFragment",
_useFragment,
useApolloClient(options.client)
)(options);
}
function _useFragment<TData = any, TVars = OperationVariables>(
options: UseFragmentOptions<TData, TVars>
): UseFragmentResult<TData> {
const { cache } = useApolloClient(options.client);
const diffOptions = useDeepMemo<Cache.DiffOptions<TData, TVars>>(() => {
const {
fragment,
fragmentName,
from,
optimistic = true,
...rest
} = options;
return {
...rest,
returnPartialData: true,
id: typeof from === "string" ? from : cache.identify(from),
query: cache["getFragmentDoc"](fragment, fragmentName),
optimistic,
};
}, [options]);
const resultRef = useLazyRef<UseFragmentResult<TData>>(() =>
diffToResult(cache.diff<TData>(diffOptions))
);
// Since .next is async, we need to make sure that we
// get the correct diff on the next render given new diffOptions
React.useMemo(() => {
resultRef.current = diffToResult(cache.diff<TData>(diffOptions));
}, [diffOptions, cache]);
// Used for both getSnapshot and getServerSnapshot
const getSnapshot = React.useCallback(() => resultRef.current, []);
return useSyncExternalStore(
React.useCallback(
(forceUpdate) => {
let lastTimeout = 0;
const unsubscribe = cache.watch({
...diffOptions,
immediate: true,
callback(diff) {
if (!equal(diff.result, resultRef.current.data)) {
resultRef.current = diffToResult(diff);
// If we get another update before we've re-rendered, bail out of
// the update and try again. This ensures that the relative timing
// between useQuery and useFragment stays roughly the same as
// fixed in https://github.com/apollographql/apollo-client/pull/11083
clearTimeout(lastTimeout);
lastTimeout = setTimeout(forceUpdate) as any;
}
},
});
return () => {
unsubscribe();
clearTimeout(lastTimeout);
};
},
[cache, diffOptions]
),
getSnapshot,
getSnapshot
);
}
function diffToResult<TData>(
diff: Cache.DiffResult<TData>
): UseFragmentResult<TData> {
const result = {
data: diff.result!,
complete: !!diff.complete,
} as UseFragmentResult<TData>;
if (diff.missing) {
result.missing = mergeDeepArray(diff.missing.map((error) => error.missing));
}
return result;
}