-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
useQuery.ts
356 lines (325 loc) · 11.5 KB
/
useQuery.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import { useContext, useEffect, useMemo, useRef, useState } from 'react';
import { equal } from '@wry/equality';
import { OperationVariables } from '../../core';
import { getApolloContext } from '../context';
import { ApolloError } from '../../errors';
import {
ApolloQueryResult,
NetworkStatus,
ObservableQuery,
DocumentNode,
TypedDocumentNode,
WatchQueryOptions,
} from '../../core';
import {
QueryHookOptions,
QueryResult,
} from '../types/types';
import { DocumentType, verifyDocumentType } from '../parser';
import { useApolloClient } from './useApolloClient';
export function useQuery<
TData = any,
TVariables = OperationVariables,
>(
query: DocumentNode | TypedDocumentNode<TData, TVariables>,
options?: QueryHookOptions<TData, TVariables>,
): QueryResult<TData> {
const context = useContext(getApolloContext());
const client = useApolloClient(options?.client);
verifyDocumentType(query, DocumentType.Query);
const [obsQuery, setObsQuery] = useState(() => {
const watchQueryOptions = createWatchQueryOptions(query, options);
// See if there is an existing observable that was used to fetch the same
// data and if so, use it instead since it will contain the proper queryId
// to fetch the result set. This is used during SSR.
let obsQuery: ObservableQuery<TData, TVariables> | null = null;
if (context.renderPromises) {
obsQuery = context.renderPromises.getSSRObservable(watchQueryOptions);
}
if (!obsQuery) {
// Is it safe (StrictMode/memory-wise) to call client.watchQuery here?
obsQuery = client.watchQuery(watchQueryOptions);
if (context.renderPromises) {
context.renderPromises.registerSSRObservable(
obsQuery,
watchQueryOptions,
);
}
}
if (
context.renderPromises &&
options?.ssr !== false &&
!options?.skip &&
obsQuery.getCurrentResult().loading
) {
// TODO: This is a legacy API which could probably be cleaned up
context.renderPromises.addQueryPromise(
{
// The only options which seem to actually be used by the
// RenderPromises class are query and variables.
getOptions: () => createWatchQueryOptions(query, options),
fetchData: () => new Promise<void>((resolve) => {
const sub = obsQuery!.subscribe({
next(result) {
if (!result.loading) {
resolve()
sub.unsubscribe();
}
},
error() {
resolve();
sub.unsubscribe();
},
complete() {
resolve();
},
});
}),
},
// This callback never seemed to do anything
() => null,
);
}
return obsQuery;
});
let [result, setResult] = useState(() => {
const result = obsQuery.getCurrentResult();
if (!result.loading && options) {
if (result.error) {
options.onError?.(result.error);
} else if (result.data) {
options.onCompleted?.(result.data);
}
}
return result;
});
const ref = useRef({
client,
query,
options,
result,
previousData: void 0 as TData | undefined,
watchQueryOptions: createWatchQueryOptions(query, options),
});
// An effect to recreate the obsQuery whenever the client or query changes.
// This effect is also responsible for checking and updating the obsQuery
// options whenever they change.
useEffect(() => {
const watchQueryOptions = createWatchQueryOptions(query, options);
let nextResult: ApolloQueryResult<TData> | undefined;
if (ref.current.client !== client || !equal(ref.current.query, query)) {
const obsQuery = client.watchQuery(watchQueryOptions);
setObsQuery(obsQuery);
nextResult = obsQuery.getCurrentResult();
} else if (!equal(ref.current.watchQueryOptions, watchQueryOptions)) {
obsQuery.setOptions(watchQueryOptions).catch(() => {});
nextResult = obsQuery.getCurrentResult();
ref.current.watchQueryOptions = watchQueryOptions;
}
if (nextResult) {
const previousResult = ref.current.result;
if (previousResult.data) {
ref.current.previousData = previousResult.data;
}
setResult(ref.current.result = nextResult);
if (!nextResult.loading && options) {
if (!result.loading) {
if (result.error) {
options.onError?.(result.error);
} else if (result.data) {
options.onCompleted?.(result.data);
}
}
}
}
Object.assign(ref.current, { client, query, options });
}, [obsQuery, client, query, options]);
// An effect to subscribe to the current observable query
useEffect(() => {
if (context.renderPromises) {
return;
}
let subscription = obsQuery.subscribe(onNext, onError);
// We use `getCurrentResult()` instead of the callback argument because
// the values differ slightly. Specifically, loading results will have
// an empty object for data instead of `undefined` for some reason.
function onNext() {
const previousResult = ref.current.result;
const result = obsQuery.getCurrentResult();
// Make sure we're not attempting to re-render similar results
if (
previousResult &&
previousResult.loading === result.loading &&
previousResult.networkStatus === result.networkStatus &&
equal(previousResult.data, result.data)
) {
return;
}
if (previousResult.data) {
ref.current.previousData = previousResult.data;
}
setResult(ref.current.result = result);
if (!result.loading) {
ref.current.options?.onCompleted?.(result.data);
}
}
function onError(error: Error) {
const last = obsQuery["last"];
subscription.unsubscribe();
// Unfortunately, if `lastError` is set in the current
// `observableQuery` when the subscription is re-created,
// the subscription will immediately receive the error, which will
// cause it to terminate again. To avoid this, we first clear
// the last error/result from the `observableQuery` before re-starting
// the subscription, and restore it afterwards (so the subscription
// has a chance to stay open).
try {
obsQuery.resetLastResults();
subscription = obsQuery.subscribe(onNext, onError);
} finally {
obsQuery["last"] = last;
}
if (!error.hasOwnProperty('graphQLErrors')) {
// The error is not a GraphQL error
throw error;
}
const previousResult = ref.current.result;
if (
(previousResult && previousResult.loading) ||
!equal(error, previousResult.error)
) {
setResult(ref.current.result = {
data: previousResult.data,
error: error as ApolloError,
loading: false,
networkStatus: NetworkStatus.error,
});
ref.current.options?.onError?.(error as ApolloError);
}
}
return () => subscription.unsubscribe();
}, [obsQuery, context.renderPromises, client.disableNetworkFetches]);
let partial: boolean | undefined;
({ partial, ...result } = result);
{
// BAD BOY CODE BLOCK WHERE WE PUT SIDE-EFFECTS IN THE RENDER FUNCTION
//
// TODO: This code should be removed when the partialRefetch option is
// removed. I was unable to get this hook to behave reasonably in certain
// edge cases when this block was put in an effect.
if (
partial &&
options?.partialRefetch &&
!result.loading &&
(!result.data || Object.keys(result.data).length === 0) &&
obsQuery.options.fetchPolicy !== 'cache-only'
) {
result = {
...result,
loading: true,
networkStatus: NetworkStatus.refetch,
};
obsQuery.refetch();
}
// TODO: This is a hack to make sure useLazyQuery executions update the
// obsevable query options for ssr.
if (
context.renderPromises &&
options?.ssr !== false &&
!options?.skip &&
result.loading
) {
obsQuery.setOptions(createWatchQueryOptions(query, options)).catch(() => {});
}
}
if (
(context.renderPromises || client.disableNetworkFetches) &&
options?.ssr === false
) {
// If SSR has been explicitly disabled, and this function has been called
// on the server side, return the default loading state.
result = ref.current.result = {
loading: true,
data: void 0 as unknown as TData,
error: void 0,
networkStatus: NetworkStatus.loading,
};
} else if (options?.skip || options?.fetchPolicy === 'standby') {
// When skipping a query (ie. we're not querying for data but still want to
// render children), make sure the `data` is cleared out and `loading` is
// set to `false` (since we aren't loading anything).
//
// NOTE: We no longer think this is the correct behavior. Skipping should
// not automatically set `data` to `undefined`, but instead leave the
// previous data in place. In other words, skipping should not mandate that
// previously received data is all of a sudden removed. Unfortunately,
// changing this is breaking, so we'll have to wait until Apollo Client 4.0
// to address this.
result = {
loading: false,
data: void 0 as unknown as TData,
error: void 0,
networkStatus: NetworkStatus.ready,
};
}
if (result.errors && result.errors.length) {
// Until a set naming convention for networkError and graphQLErrors is
// decided upon, we map errors (graphQLErrors) to the error options.
// TODO: Is it possible for both result.error and result.errors to be
// defined here?
result = {
...result,
error: result.error || new ApolloError({ graphQLErrors: result.errors }),
};
}
const obsQueryFields = useMemo(() => ({
refetch: obsQuery.refetch.bind(obsQuery),
fetchMore: obsQuery.fetchMore.bind(obsQuery),
updateQuery: obsQuery.updateQuery.bind(obsQuery),
startPolling: obsQuery.startPolling.bind(obsQuery),
stopPolling: obsQuery.stopPolling.bind(obsQuery),
subscribeToMore: obsQuery.subscribeToMore.bind(obsQuery),
}), [obsQuery]);
return {
...obsQueryFields,
variables: obsQuery.variables,
client,
called: true,
previousData: ref.current.previousData,
...result,
};
}
function createWatchQueryOptions<TData, TVariables>(
query: DocumentNode | TypedDocumentNode<TData, TVariables>,
options: QueryHookOptions<TData, TVariables> = {},
): WatchQueryOptions<TVariables, TData> {
// TODO: For some reason, we pass context, which is the React Apollo Context,
// into observable queries, and test for that.
// removing hook specific options
const {
skip,
ssr,
onCompleted,
onError,
displayName,
...watchQueryOptions
} = options;
if (skip) {
watchQueryOptions.fetchPolicy = 'standby';
} else if (
watchQueryOptions.context?.renderPromises &&
(
watchQueryOptions.fetchPolicy === 'network-only' ||
watchQueryOptions.fetchPolicy === 'cache-and-network'
)
) {
// this behavior was added to react-apollo without explanation in this PR
// https://github.com/apollographql/react-apollo/pull/1579
watchQueryOptions.fetchPolicy = 'cache-first';
} else if (!watchQueryOptions.fetchPolicy) {
// cache-first is the default policy, but we explicitly assign it here so
// the cache policies computed based on options can be cleared
watchQueryOptions.fetchPolicy = 'cache-first';
}
return { query, ...watchQueryOptions };
}