-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathloadQuery.js
410 lines (384 loc) · 14.4 KB
/
loadQuery.js
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall relay
*/
'use strict';
import type {
EnvironmentProviderOptions,
LoadQueryOptions,
PreloadableConcreteRequest,
PreloadedQueryInner,
} from './EntryPointTypes.flow';
import type {
ConcreteRequest,
GraphQLResponse,
GraphQLTaggedNode,
IEnvironment,
OperationDescriptor,
OperationType,
Query,
RequestIdentifier,
RequestParameters,
} from 'relay-runtime';
const invariant = require('invariant');
const React = require('react');
const {
__internal: {fetchQueryDeduped},
Observable,
PreloadableQueryRegistry,
RelayFeatureFlags,
ReplaySubject,
createOperationDescriptor,
getRequest,
getRequestIdentifier,
} = require('relay-runtime');
const warning = require('warning');
let RenderDispatcher = null;
let fetchKey = 100001;
function useTrackLoadQueryInRender() {
if (RenderDispatcher === null) {
// Flow does not know of React internals (rightly so), but we need to
// ensure here that this function isn't called inside render.
RenderDispatcher =
// $FlowFixMe[prop-missing]
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
?.ReactCurrentDispatcher?.current;
}
}
type QueryType<T> = T extends Query<infer V, infer D, infer RR>
? {
variables: V,
response: D,
rawResponse?: $NonMaybeType<RR>,
}
: $Call<<T>(PreloadableConcreteRequest<T>) => T, T>;
declare function loadQuery<
T,
TEnvironmentProviderOptions = EnvironmentProviderOptions,
>(
environment: IEnvironment,
preloadableRequest: T,
variables: QueryType<T>['variables'],
options?: ?LoadQueryOptions,
environmentProviderOptions?: ?TEnvironmentProviderOptions,
): PreloadedQueryInner<QueryType<T>, TEnvironmentProviderOptions>;
function loadQuery<
TQuery: OperationType,
TEnvironmentProviderOptions = EnvironmentProviderOptions,
>(
environment: IEnvironment,
preloadableRequest: PreloadableConcreteRequest<TQuery>,
variables: TQuery['variables'],
options?: ?LoadQueryOptions,
environmentProviderOptions?: ?TEnvironmentProviderOptions,
): PreloadedQueryInner<TQuery, TEnvironmentProviderOptions> {
// This code ensures that we don't call loadQuery during render.
const CurrentDispatcher =
// $FlowFixMe[prop-missing]
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
?.ReactCurrentDispatcher?.current;
warning(
RenderDispatcher == null || CurrentDispatcher !== RenderDispatcher,
'Relay: `%s` should not be called inside a React render function.',
options?.__nameForWarning ?? 'loadQuery',
);
// Every time you call loadQuery, we will generate a new fetchKey.
// This will ensure that every query reference that is created and
// passed to usePreloadedQuery is independently evaluated,
// even if they are for the same query/variables.
// Specifically, we want to avoid a case where we try to refetch a
// query by calling loadQuery a second time, and have the Suspense
// cache in usePreloadedQuery reuse the cached result instead of
// re-evaluating the new query ref and triggering a refetch if
// necessary.
fetchKey++;
const fetchPolicy = options?.fetchPolicy ?? 'store-or-network';
const networkCacheConfig = {
...options?.networkCacheConfig,
force: true,
};
// executeWithNetworkSource will retain and execute an operation
// against the Relay store, given an Observable that would provide
// the network events for the operation.
let retainReference;
let didExecuteNetworkSource = false;
const executeWithNetworkSource = (
operation: OperationDescriptor,
networkObservable: Observable<GraphQLResponse>,
): Observable<GraphQLResponse> => {
didExecuteNetworkSource = true;
return environment.executeWithSource({
operation,
source: networkObservable,
});
};
// N.B. For loadQuery, we unconventionally want to return an Observable
// that isn't lazily executed, meaning that we don't want to wait
// until the returned Observable is subscribed to to actually start
// fetching and executing an operation; i.e. we want to execute the
// operation eagerly, when loadQuery is called.
// For this reason, we use an intermediate executionSubject which
// allows us to capture the events that occur during the eager execution
// of the operation, and then replay them to the Observable we
// ultimately return.
const executionSubject = new ReplaySubject<GraphQLResponse>();
const returnedObservable = Observable.create<GraphQLResponse>(sink =>
executionSubject.subscribe(sink),
);
let unsubscribeFromNetworkRequest;
let networkError = null;
// makeNetworkRequest will immediately start a raw network request if
// one isn't already in flight and return an Observable that when
// subscribed to will replay the network events that have occured so far,
// as well as subsequent events.
let didMakeNetworkRequest = false;
const makeNetworkRequest = (
params: RequestParameters,
): Observable<GraphQLResponse> => {
// N.B. this function is called synchronously or not at all
// didMakeNetworkRequest is safe to rely on in the returned value
// Even if the request gets deduped below, we still wan't to return an
// observable that provides the replayed network events for the query,
// so we set this to true before deduping, to guarantee that the
// `source` observable is returned.
didMakeNetworkRequest = true;
let observable;
const subject = new ReplaySubject<GraphQLResponse>();
if (RelayFeatureFlags.ENABLE_LOAD_QUERY_REQUEST_DEDUPING === true) {
// Here, we are calling fetchQueryDeduped at the network layer level,
// which ensures that only a single network request is active for a given
// (environment, identifier) pair.
// Since network requests can be started /before/ we have the query ast
// necessary to process the results, we need to dedupe the raw requests
// separately from deduping the operation execution; specifically,
// if `loadQuery` is called multiple times before the query ast is available,
// we still want the network request to be deduped.
// - If a duplicate active network request is found, it will return an
// Observable that replays the events of the already active request.
// - If no duplicate active network request is found, it will call the fetchFn
// to start the request, and return an Observable that will replay
// the events from the network request.
// We provide an extra key to the identifier to distinguish deduping
// of raw network requests vs deduping of operation executions.
const identifier: RequestIdentifier =
'raw-network-request-' + getRequestIdentifier(params, variables);
observable = fetchQueryDeduped(environment, identifier, () => {
const network = environment.getNetwork();
return network.execute(params, variables, networkCacheConfig);
});
} else {
const network = environment.getNetwork();
observable = network.execute(params, variables, networkCacheConfig);
}
const {unsubscribe} = observable.subscribe({
error(err) {
networkError = err;
subject.error(err);
},
next(data) {
subject.next(data);
},
complete() {
subject.complete();
},
});
unsubscribeFromNetworkRequest = unsubscribe;
return Observable.create(sink => {
const subjectSubscription = subject.subscribe(sink);
return () => {
subjectSubscription.unsubscribe();
unsubscribeFromNetworkRequest();
};
});
};
let unsubscribeFromExecution;
const executeDeduped = (
operation: OperationDescriptor,
fetchFn: () => Observable<GraphQLResponse>,
) => {
if (RelayFeatureFlags.ENABLE_LOAD_QUERY_REQUEST_DEDUPING === true) {
// N.B. at this point, if we're calling execute with a query ast (OperationDescriptor),
// we are guaranteed to have started a network request. We set this to
// true here as well since `makeNetworkRequest` might get skipped in the case
// where the query ast is already available and the query executions get deduped.
// Even if the execution gets deduped below, we still wan't to return
// an observable that provides the replayed network events for the query,
// so we set this to true before deduping, to guarantee that the `source`
// observable is returned.
didMakeNetworkRequest = true;
}
// Here, we are calling fetchQueryDeduped, which ensures that only
// a single operation is active for a given (environment, identifier) pair,
// and also tracks the active state of the operation, which is necessary
// for our Suspense infra to later be able to suspend (or not) on
// active operations. Even though we already dedupe raw network requests,
// we also need to dedupe and keep track operation execution for our Suspense
// infra, and we also want to avoid processing responses more than once, for
// the cases where `loadQuery` might be called multiple times after the query ast
// is available.
// - If a duplicate active operation is found, it will return an
// Observable that replays the events of the already active operation.
// - If no duplicate active operation is found, it will call the fetchFn
// to execute the operation, and return an Observable that will provide
// the events for executing the operation.
({unsubscribe: unsubscribeFromExecution} = fetchQueryDeduped(
environment,
operation.request.identifier,
fetchFn,
).subscribe({
error(err) {
executionSubject.error(err);
},
next(data) {
executionSubject.next(data);
},
complete() {
executionSubject.complete();
},
}));
};
const checkAvailabilityAndExecute = (concreteRequest: ConcreteRequest) => {
const operation = createOperationDescriptor(
concreteRequest,
variables,
networkCacheConfig,
);
retainReference = environment.retain(operation);
if (fetchPolicy === 'store-only') {
return;
}
// N.B. If the fetch policy allows fulfillment from the store but the
// environment already has the data for that operation cached in the store,
// then we do nothing.
const shouldFetch =
fetchPolicy !== 'store-or-network' ||
environment.check(operation).status !== 'available';
if (shouldFetch) {
executeDeduped(operation, () => {
// N.B. Since we have the operation synchronously available here,
// we can immediately fetch and execute the operation.
const networkObservable = makeNetworkRequest(concreteRequest.params);
const executeObservable = executeWithNetworkSource(
operation,
networkObservable,
);
return executeObservable;
});
}
};
let params;
let cancelOnLoadCallback: () => void;
let queryId;
if (preloadableRequest.kind === 'PreloadableConcreteRequest') {
const preloadableConcreteRequest: PreloadableConcreteRequest<TQuery> =
(preloadableRequest: $FlowFixMe);
({params} = preloadableConcreteRequest);
({id: queryId} = params);
invariant(
queryId !== null,
'Relay: `loadQuery` requires that preloadable query `%s` has a persisted query id',
params.name,
);
const module = PreloadableQueryRegistry.get(queryId);
if (module != null) {
checkAvailabilityAndExecute(module);
} else {
// If the module isn't synchronously available, we launch the
// network request immediately if the fetchPolicy might produce
// a network fetch, regardless of the state of the store cache. We
// do this because we can't check if a query is cached without the
// ast, and we know that if we don't have the query ast
// available, then this query could've never been written to the
// store in the first place, so it couldn't have been cached.
const networkObservable =
fetchPolicy === 'store-only' ? null : makeNetworkRequest(params);
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
({dispose: cancelOnLoadCallback} = PreloadableQueryRegistry.onLoad(
queryId,
preloadedModule => {
cancelOnLoadCallback();
const operation = createOperationDescriptor(
preloadedModule,
variables,
networkCacheConfig,
);
retainReference = environment.retain(operation);
if (networkObservable != null) {
executeDeduped(operation, () =>
executeWithNetworkSource(operation, networkObservable),
);
}
},
));
}
} else {
const graphQlTaggedNode: GraphQLTaggedNode =
(preloadableRequest: $FlowFixMe);
const request = getRequest(graphQlTaggedNode);
params = request.params;
queryId = params.cacheID != null ? params.cacheID : params.id;
checkAvailabilityAndExecute(request);
}
let isDisposed = false;
let isReleased = false;
let isNetworkRequestCancelled = false;
const releaseQuery = () => {
if (isReleased) {
return;
}
retainReference && retainReference.dispose();
isReleased = true;
};
const cancelNetworkRequest = () => {
if (isNetworkRequestCancelled) {
return;
}
if (didExecuteNetworkSource) {
unsubscribeFromExecution && unsubscribeFromExecution();
} else {
unsubscribeFromNetworkRequest && unsubscribeFromNetworkRequest();
}
cancelOnLoadCallback && cancelOnLoadCallback();
isNetworkRequestCancelled = true;
};
return {
kind: 'PreloadedQuery',
environment,
environmentProviderOptions,
dispose() {
if (isDisposed) {
return;
}
releaseQuery();
cancelNetworkRequest();
isDisposed = true;
},
releaseQuery,
cancelNetworkRequest,
fetchKey,
id: queryId,
// $FlowFixMe[unsafe-getters-setters] - this has no side effects
get isDisposed() {
return isDisposed || isReleased;
},
// $FlowFixMe[unsafe-getters-setters] - this has no side effects
get networkError() {
return networkError;
},
name: params.name,
networkCacheConfig,
fetchPolicy,
source: didMakeNetworkRequest ? returnedObservable : undefined,
variables,
};
}
module.exports = {
loadQuery,
useTrackLoadQueryInRender,
};