-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathRelayModernEnvironment.js
More file actions
555 lines (513 loc) · 17.4 KB
/
Copy pathRelayModernEnvironment.js
File metadata and controls
555 lines (513 loc) · 17.4 KB
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/**
* 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
* @format
* @oncall relay
*/
'use strict';
import type {HandlerProvider} from '../handlers/RelayDefaultHandlerProvider';
import type {ActorIdentifier} from '../multi-actor-environment/ActorIdentifier';
import type {
GraphQLResponse,
INetwork,
PayloadData,
} from '../network/RelayNetworkTypes';
import type {Sink} from '../network/RelayObservable';
import type {Disposable, RenderPolicy} from '../util/RelayRuntimeTypes';
import type {ActiveState} from './OperationExecutor';
import type {GetDataID} from './RelayResponseNormalizer';
import type {
ExecuteMutationConfig,
IEnvironment,
LogFunction,
MissingFieldHandler,
MutationParameters,
NormalizeResponseFunction,
OperationAvailability,
OperationDescriptor,
OperationLoader,
OperationTracker,
OptimisticResponseConfig,
OptimisticUpdateFunction,
PublishQueue,
RelayFieldLogger,
SelectorStoreUpdater,
SingularReaderSelector,
Snapshot,
Store,
StoreUpdater,
TaskScheduler,
} from './RelayStoreTypes';
const RelayDefaultHandlerProvider = require('../handlers/RelayDefaultHandlerProvider');
const {
INTERNAL_ACTOR_IDENTIFIER_DO_NOT_USE,
assertInternalActorIdentifier,
} = require('../multi-actor-environment/ActorIdentifier');
const RelayObservable = require('../network/RelayObservable');
const wrapNetworkWithLogObserver = require('../network/wrapNetworkWithLogObserver');
const RelayOperationTracker = require('../store/RelayOperationTracker');
const registerEnvironmentWithDevTools = require('../util/registerEnvironmentWithDevTools');
const defaultGetDataID = require('./defaultGetDataID');
const defaultRelayFieldLogger = require('./defaultRelayFieldLogger');
const normalizeResponse = require('./normalizeResponse');
const OperationExecutor = require('./OperationExecutor');
const RelayModernStore = require('./RelayModernStore');
const RelayPublishQueue = require('./RelayPublishQueue');
const RelayRecordSource = require('./RelayRecordSource');
const invariant = require('invariant');
export type EnvironmentConfig = {
readonly configName?: string,
readonly handlerProvider?: ?HandlerProvider,
readonly treatMissingFieldsAsNull?: boolean,
readonly deferDeduplicatedFields?: boolean,
readonly log?: ?LogFunction,
readonly operationLoader?: ?OperationLoader,
readonly network: INetwork,
readonly normalizeResponse?: ?NormalizeResponseFunction,
readonly scheduler?: ?TaskScheduler,
readonly store?: Store,
readonly missingFieldHandlers?: ?ReadonlyArray<MissingFieldHandler>,
readonly operationTracker?: ?OperationTracker,
readonly getDataID?: ?GetDataID,
readonly UNSTABLE_defaultRenderPolicy?: ?RenderPolicy,
readonly options?: unknown,
readonly isServer?: boolean,
readonly relayFieldLogger?: ?RelayFieldLogger,
readonly shouldProcessClientComponents?: ?boolean,
};
class RelayModernEnvironment implements IEnvironment {
__log: LogFunction;
readonly _defaultRenderPolicy: RenderPolicy;
_operationLoader: ?OperationLoader;
_shouldProcessClientComponents: ?boolean;
_network: INetwork;
_publishQueue: PublishQueue;
_scheduler: ?TaskScheduler;
_store: Store;
configName: ?string;
_missingFieldHandlers: ReadonlyArray<MissingFieldHandler>;
_operationTracker: OperationTracker;
_getDataID: GetDataID;
_treatMissingFieldsAsNull: boolean;
_deferDeduplicatedFields: boolean;
_operationExecutions: Map<string, ActiveState>;
_inFlightOperationCompletions: Map<
string,
{promise: Promise<void>, resolve: () => void},
>;
readonly options: unknown;
readonly _isServer: boolean;
relayFieldLogger: RelayFieldLogger;
_normalizeResponse: NormalizeResponseFunction;
constructor(config: EnvironmentConfig) {
this.configName = config.configName;
this._treatMissingFieldsAsNull = config.treatMissingFieldsAsNull === true;
this._deferDeduplicatedFields = config.deferDeduplicatedFields === true;
const operationLoader = config.operationLoader;
if (__DEV__) {
if (operationLoader != null) {
invariant(
typeof operationLoader === 'object' &&
typeof operationLoader.get === 'function' &&
typeof operationLoader.load === 'function',
'RelayModernEnvironment: Expected `operationLoader` to be an object ' +
'with get() and load() functions, got `%s`.',
operationLoader,
);
}
}
const store =
config.store ??
new RelayModernStore(new RelayRecordSource(), {
getDataID: config.getDataID,
log: config.log,
operationLoader: config.operationLoader,
shouldProcessClientComponents: config.shouldProcessClientComponents,
});
this.__log = config.log ?? emptyFunction;
this.relayFieldLogger = config.relayFieldLogger ?? defaultRelayFieldLogger;
this._defaultRenderPolicy =
config.UNSTABLE_defaultRenderPolicy ?? 'partial';
this._operationLoader = operationLoader;
this._operationExecutions = new Map();
this._inFlightOperationCompletions = new Map();
this._network = wrapNetworkWithLogObserver(this, config.network);
this._getDataID = config.getDataID ?? defaultGetDataID;
this._missingFieldHandlers = config.missingFieldHandlers ?? [];
this._publishQueue = new RelayPublishQueue(
store,
config.handlerProvider ?? RelayDefaultHandlerProvider,
this._getDataID,
this._missingFieldHandlers,
this.__log,
);
this._scheduler = config.scheduler ?? null;
this._store = store;
this.options = config.options;
this._isServer = config.isServer ?? false;
this._normalizeResponse = config.normalizeResponse ?? normalizeResponse;
(this as any).__setNet = newNet =>
(this._network = wrapNetworkWithLogObserver(this, newNet));
if (__DEV__) {
const {inspect} = require('./StoreInspector');
(this as any).DEBUG_inspect = (dataID: ?string) => inspect(this, dataID);
}
this._operationTracker =
config.operationTracker ?? new RelayOperationTracker();
this._shouldProcessClientComponents = config.shouldProcessClientComponents;
// Register this Relay Environment with Relay DevTools if it exists.
// Note: this must always be the last step in the constructor.
registerEnvironmentWithDevTools(this);
}
getStore(): Store {
return this._store;
}
getNetwork(): INetwork {
return this._network;
}
getOperationTracker(): RelayOperationTracker {
return this._operationTracker;
}
getScheduler(): ?TaskScheduler {
return this._scheduler;
}
isRequestActive(requestIdentifier: string): boolean {
const activeState = this._operationExecutions.get(requestIdentifier);
return activeState === 'active';
}
getPromiseForInFlightOperation(
requestIdentifier: string,
): Promise<void> | null {
const entry = this._inFlightOperationCompletions.get(requestIdentifier);
return entry?.promise ?? null;
}
UNSTABLE_getDefaultRenderPolicy(): RenderPolicy {
return this._defaultRenderPolicy;
}
applyUpdate(optimisticUpdate: OptimisticUpdateFunction): Disposable {
const dispose = () => {
this._scheduleUpdates(() => {
this._publishQueue.revertUpdate(optimisticUpdate);
this._publishQueue.run();
});
};
this._scheduleUpdates(() => {
this._publishQueue.applyUpdate(optimisticUpdate);
this._publishQueue.run();
});
return {dispose};
}
revertUpdate(update: OptimisticUpdateFunction): void {
this._scheduleUpdates(() => {
this._publishQueue.revertUpdate(update);
this._publishQueue.run();
});
}
replaceUpdate(
update: OptimisticUpdateFunction,
newUpdate: OptimisticUpdateFunction,
): void {
this._scheduleUpdates(() => {
this._publishQueue.revertUpdate(update);
this._publishQueue.applyUpdate(newUpdate);
this._publishQueue.run();
});
}
applyMutation<TMutation extends MutationParameters>(
optimisticConfig: OptimisticResponseConfig<TMutation>,
): Disposable {
const subscription = this._execute({
createSource: () => RelayObservable.create(_sink => {}),
isClientPayload: false,
operation: optimisticConfig.operation,
optimisticConfig,
updater: null,
}).subscribe({});
return {
dispose: () => subscription.unsubscribe(),
};
}
check(operation: OperationDescriptor): OperationAvailability {
if (
this._missingFieldHandlers.length === 0 &&
!operationHasClientAbstractTypes(operation)
) {
return this._store.check(operation);
}
return this._checkSelectorAndHandleMissingFields(
operation,
this._missingFieldHandlers,
);
}
commitPayload(operation: OperationDescriptor, payload: PayloadData): void {
this._execute({
createSource: () => RelayObservable.from({data: payload}),
isClientPayload: true,
operation,
optimisticConfig: null,
updater: null,
}).subscribe({});
}
commitUpdate(updater: StoreUpdater): void {
this._scheduleUpdates(() => {
this._publishQueue.commitUpdate(updater);
this._publishQueue.run();
});
}
lookup(readSelector: SingularReaderSelector): Snapshot {
return this._store.lookup(readSelector);
}
subscribe(
snapshot: Snapshot,
callback: (snapshot: Snapshot) => void,
): Disposable {
return this._store.subscribe(snapshot, callback);
}
retain(operation: OperationDescriptor): Disposable {
return this._store.retain(operation);
}
experimental_batchUpdates(callback: () => void): void {
// $FlowFixMe[prop-missing] - experimental method not on Store interface
const batchFn = this._store.experimental_batchUpdates;
invariant(
typeof batchFn === 'function',
'RelayModernEnvironment: The current store does not support experimental_batchUpdates.',
);
// We must use .call to preserve Flow's narrowing from the above typeof check.
batchFn.call(this._store, callback);
}
isServer(): boolean {
return this._isServer;
}
_checkSelectorAndHandleMissingFields(
operation: OperationDescriptor,
handlers: ReadonlyArray<MissingFieldHandler>,
): OperationAvailability {
const target = RelayRecordSource.create();
const source = this._store.getSource();
const result = this._store.check(operation, {
defaultActorIdentifier: INTERNAL_ACTOR_IDENTIFIER_DO_NOT_USE,
getSourceForActor(actorIdentifier: ActorIdentifier) {
assertInternalActorIdentifier(actorIdentifier);
return source;
},
getTargetForActor(actorIdentifier: ActorIdentifier) {
assertInternalActorIdentifier(actorIdentifier);
return target;
},
handlers,
});
if (target.size() > 0) {
this._scheduleUpdates(() => {
this._publishQueue.commitSource(target);
this._publishQueue.run();
});
}
return result;
}
_scheduleUpdates(task: () => void) {
const scheduler = this._scheduler;
if (scheduler != null) {
scheduler.schedule(task);
} else {
task();
}
}
/**
* Returns an Observable of GraphQLResponse resulting from executing the
* provided Query operation, each result of which is then
* normalized and committed to the publish queue.
*
* Note: Observables are lazy, so calling this method will do nothing until
* the result is subscribed to: environment.execute({...}).subscribe({...}).
*/
execute({
operation,
}: {
operation: OperationDescriptor,
}): RelayObservable<GraphQLResponse> {
return this._execute({
createSource: () => {
return this.getNetwork().execute(
operation.request.node.params,
operation.request.variables,
operation.request.cacheConfig || {},
null,
undefined,
undefined,
undefined,
() => this.check(operation),
);
},
isClientPayload: false,
operation,
optimisticConfig: null,
updater: null,
});
}
/**
* Returns an Observable of GraphQLResponse resulting from executing the
* provided Subscription operation, each result of which is then
* normalized and committed to the publish queue.
*
* Note: Observables are lazy, so calling this method will do nothing until
* the result is subscribed to: environment.execute({...}).subscribe({...}).
*/
executeSubscription<TMutation extends MutationParameters>({
operation,
updater,
}: {
operation: OperationDescriptor,
updater?: ?SelectorStoreUpdater<TMutation['response']>,
}): RelayObservable<GraphQLResponse> {
return this._execute({
createSource: () =>
this.getNetwork().execute(
operation.request.node.params,
operation.request.variables,
operation.request.cacheConfig || {},
null,
),
isClientPayload: false,
operation,
optimisticConfig: null,
updater,
});
}
/**
* Returns an Observable of GraphQLResponse resulting from executing the
* provided Mutation operation, the result of which is then normalized and
* committed to the publish queue along with an optional optimistic response
* or updater.
*
* Note: Observables are lazy, so calling this method will do nothing until
* the result is subscribed to:
* environment.executeMutation({...}).subscribe({...}).
*/
executeMutation<TMutation extends MutationParameters>({
operation,
optimisticResponse,
optimisticUpdater,
updater,
uploadables,
}: ExecuteMutationConfig<TMutation>): RelayObservable<GraphQLResponse> {
let optimisticConfig;
if (optimisticResponse || optimisticUpdater) {
optimisticConfig = {
operation,
response: optimisticResponse,
updater: optimisticUpdater,
};
}
return this._execute({
createSource: () =>
this.getNetwork().execute(
operation.request.node.params,
operation.request.variables,
{
...operation.request.cacheConfig,
force: true,
},
uploadables,
),
isClientPayload: false,
operation,
optimisticConfig,
updater,
});
}
/**
* Returns an Observable of GraphQLResponse resulting from executing the
* provided Query or Subscription operation responses, the result of which is
* then normalized and committed to the publish queue.
*
* Note: Observables are lazy, so calling this method will do nothing until
* the result is subscribed to:
* environment.executeWithSource({...}).subscribe({...}).
*/
executeWithSource({
operation,
source,
}: {
operation: OperationDescriptor,
source: RelayObservable<GraphQLResponse>,
}): RelayObservable<GraphQLResponse> {
return this._execute({
createSource: () => source,
isClientPayload: false,
operation,
optimisticConfig: null,
updater: null,
});
}
toJSON(): unknown {
return `RelayModernEnvironment(${this.configName ?? ''})`;
}
_execute<TMutation extends MutationParameters>({
createSource,
isClientPayload,
operation,
optimisticConfig,
updater,
}: {
createSource: () => RelayObservable<GraphQLResponse>,
isClientPayload: boolean,
operation: OperationDescriptor,
optimisticConfig: ?OptimisticResponseConfig<TMutation>,
updater: ?SelectorStoreUpdater<TMutation['response']>,
}): RelayObservable<GraphQLResponse> {
const publishQueue = this._publishQueue;
const store = this._store;
return RelayObservable.create((sink: Sink<GraphQLResponse>) => {
const executor = OperationExecutor.execute<$FlowFixMe>({
actorIdentifier: INTERNAL_ACTOR_IDENTIFIER_DO_NOT_USE,
getDataID: this._getDataID,
getPublishQueue(actorIdentifier: ActorIdentifier) {
assertInternalActorIdentifier(actorIdentifier);
return publishQueue;
},
getStore(actorIdentifier: ActorIdentifier) {
assertInternalActorIdentifier(actorIdentifier);
return store;
},
isClientPayload,
log: this.__log,
normalizeResponse: this._normalizeResponse,
operation,
operationExecutions: this._operationExecutions,
inFlightOperationCompletions: this._inFlightOperationCompletions,
operationLoader: this._operationLoader,
operationTracker: this._operationTracker,
optimisticConfig,
scheduler: this._scheduler,
shouldProcessClientComponents: this._shouldProcessClientComponents,
sink,
// NOTE: Some product tests expect `Network.execute` to be called only
// when the Observable is executed.
source: createSource(),
treatMissingFieldsAsNull: this._treatMissingFieldsAsNull,
deferDeduplicatedFields: this._deferDeduplicatedFields,
updater,
});
return () => executor.cancel();
});
}
}
function operationHasClientAbstractTypes(
operation: OperationDescriptor,
): boolean {
return (
operation.root.node.kind === 'Operation' &&
operation.root.node.clientAbstractTypes != null
);
}
// Add a sigil for detection by `isRelayModernEnvironment()` to avoid a
// realm-specific instanceof check, and to aid in module tree-shaking to
// avoid requiring all of RelayRuntime just to detect its environment.
(RelayModernEnvironment as any).prototype['@@RelayModernEnvironment'] = true;
function emptyFunction() {}
module.exports = RelayModernEnvironment;