relay-runtime 7.0.0 → 9.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/handlers/RelayDefaultHandlerProvider.js.flow +34 -0
- package/handlers/connection/ConnectionHandler.js.flow +549 -0
- package/handlers/connection/ConnectionInterface.js.flow +92 -0
- package/index.js +1 -1
- package/index.js.flow +314 -0
- package/lib/handlers/RelayDefaultHandlerProvider.js +3 -2
- package/lib/handlers/connection/{RelayConnectionHandler.js → ConnectionHandler.js} +34 -35
- package/lib/handlers/connection/{RelayConnectionInterface.js → ConnectionInterface.js} +3 -30
- package/lib/index.js +29 -27
- package/lib/mutations/RelayDeclarativeMutationConfig.js +30 -52
- package/lib/mutations/RelayRecordProxy.js +6 -3
- package/lib/mutations/RelayRecordSourceMutator.js +3 -9
- package/lib/mutations/RelayRecordSourceProxy.js +21 -24
- package/lib/mutations/RelayRecordSourceSelectorProxy.js +18 -14
- package/lib/mutations/applyOptimisticMutation.js +2 -1
- package/lib/mutations/commitLocalUpdate.js +1 -0
- package/lib/mutations/commitMutation.js +26 -8
- package/lib/mutations/validateMutation.js +21 -11
- package/lib/network/ConvertToExecuteFunction.js +1 -0
- package/lib/network/RelayNetwork.js +1 -0
- package/lib/network/RelayNetworkTypes.js +1 -0
- package/lib/network/RelayObservable.js +10 -9
- package/lib/network/RelayQueryResponseCache.js +9 -7
- package/lib/query/{RelayModernGraphQLTag.js → GraphQLTag.js} +15 -8
- package/lib/query/fetchQuery.js +2 -1
- package/lib/query/fetchQueryInternal.js +30 -20
- package/lib/store/ClientID.js +1 -0
- package/lib/store/DataChecker.js +47 -97
- package/lib/store/RelayConcreteVariables.js +7 -2
- package/lib/store/RelayModernEnvironment.js +82 -41
- package/lib/store/RelayModernFragmentSpecResolver.js +61 -21
- package/lib/store/RelayModernOperationDescriptor.js +2 -1
- package/lib/store/RelayModernQueryExecutor.js +476 -333
- package/lib/store/RelayModernRecord.js +39 -9
- package/lib/store/RelayModernSelector.js +2 -1
- package/lib/store/RelayModernStore.js +359 -371
- package/lib/store/RelayOperationTracker.js +36 -78
- package/lib/store/RelayOptimisticRecordSource.js +8 -5
- package/lib/store/RelayPublishQueue.js +66 -53
- package/lib/store/RelayReader.js +2 -24
- package/lib/store/RelayRecordSource.js +3 -9
- package/lib/store/RelayRecordSourceMapImpl.js +14 -18
- package/lib/store/RelayRecordState.js +1 -0
- package/lib/store/RelayReferenceMarker.js +8 -58
- package/lib/store/RelayResponseNormalizer.js +15 -144
- package/lib/store/RelayStoreTypes.js +1 -0
- package/lib/store/RelayStoreUtils.js +34 -10
- package/lib/store/StoreInspector.js +11 -5
- package/lib/store/ViewerPattern.js +1 -0
- package/lib/store/cloneRelayHandleSourceField.js +1 -0
- package/lib/store/createFragmentSpecResolver.js +1 -0
- package/lib/store/createRelayContext.js +1 -0
- package/lib/store/defaultGetDataID.js +1 -0
- package/lib/store/hasOverlappingIDs.js +1 -0
- package/lib/store/isRelayModernEnvironment.js +1 -0
- package/lib/store/normalizeRelayPayload.js +8 -4
- package/lib/store/readInlineData.js +2 -1
- package/lib/subscription/requestSubscription.js +6 -3
- package/lib/util/JSResourceTypes.flow.js +12 -0
- package/lib/util/NormalizationNode.js +1 -0
- package/lib/util/ReaderNode.js +1 -0
- package/lib/util/RelayConcreteNode.js +3 -0
- package/lib/util/RelayDefaultHandleKey.js +1 -0
- package/lib/util/RelayError.js +2 -1
- package/lib/util/RelayFeatureFlags.js +3 -2
- package/lib/util/RelayProfiler.js +1 -0
- package/lib/util/RelayReplaySubject.js +2 -3
- package/lib/util/RelayRuntimeTypes.js +1 -0
- package/lib/util/createPayloadFor3DField.js +34 -0
- package/lib/util/deepFreeze.js +1 -0
- package/lib/util/generateID.js +1 -0
- package/lib/util/getFragmentIdentifier.js +1 -0
- package/lib/util/getRelayHandleKey.js +1 -0
- package/lib/util/getRequestIdentifier.js +1 -0
- package/lib/util/isPromise.js +1 -0
- package/lib/util/isScalarAndEqual.js +1 -0
- package/lib/util/recycleNodesInto.js +1 -0
- package/lib/util/resolveImmediate.js +1 -0
- package/lib/util/stableCopy.js +1 -0
- package/mutations/RelayDeclarativeMutationConfig.js.flow +380 -0
- package/mutations/RelayRecordProxy.js.flow +165 -0
- package/mutations/RelayRecordSourceMutator.js.flow +238 -0
- package/mutations/RelayRecordSourceProxy.js.flow +164 -0
- package/mutations/RelayRecordSourceSelectorProxy.js.flow +119 -0
- package/mutations/applyOptimisticMutation.js.flow +76 -0
- package/mutations/commitLocalUpdate.js.flow +24 -0
- package/mutations/commitMutation.js.flow +184 -0
- package/mutations/validateMutation.js.flow +211 -0
- package/network/ConvertToExecuteFunction.js.flow +49 -0
- package/network/RelayNetwork.js.flow +84 -0
- package/network/RelayNetworkTypes.js.flow +123 -0
- package/network/RelayObservable.js.flow +634 -0
- package/network/RelayQueryResponseCache.js.flow +111 -0
- package/package.json +1 -1
- package/query/GraphQLTag.js.flow +166 -0
- package/query/fetchQuery.js.flow +47 -0
- package/query/fetchQueryInternal.js.flow +349 -0
- package/relay-runtime.js +2 -2
- package/relay-runtime.min.js +2 -2
- package/store/ClientID.js.flow +43 -0
- package/store/DataChecker.js.flow +426 -0
- package/store/RelayConcreteVariables.js.flow +96 -0
- package/store/RelayModernEnvironment.js.flow +526 -0
- package/store/RelayModernFragmentSpecResolver.js.flow +426 -0
- package/store/RelayModernOperationDescriptor.js.flow +88 -0
- package/store/RelayModernQueryExecutor.js.flow +1327 -0
- package/store/RelayModernRecord.js.flow +403 -0
- package/store/RelayModernSelector.js.flow +444 -0
- package/store/RelayModernStore.js.flow +757 -0
- package/store/RelayOperationTracker.js.flow +164 -0
- package/store/RelayOptimisticRecordSource.js.flow +119 -0
- package/store/RelayPublishQueue.js.flow +401 -0
- package/store/RelayReader.js.flow +376 -0
- package/store/RelayRecordSource.js.flow +29 -0
- package/store/RelayRecordSourceMapImpl.js.flow +87 -0
- package/store/RelayRecordState.js.flow +37 -0
- package/store/RelayReferenceMarker.js.flow +236 -0
- package/store/RelayResponseNormalizer.js.flow +556 -0
- package/store/RelayStoreTypes.js.flow +873 -0
- package/store/RelayStoreUtils.js.flow +218 -0
- package/store/StoreInspector.js.flow +173 -0
- package/store/ViewerPattern.js.flow +26 -0
- package/store/cloneRelayHandleSourceField.js.flow +66 -0
- package/store/createFragmentSpecResolver.js.flow +55 -0
- package/store/createRelayContext.js.flow +44 -0
- package/store/defaultGetDataID.js.flow +27 -0
- package/store/hasOverlappingIDs.js.flow +34 -0
- package/store/isRelayModernEnvironment.js.flow +27 -0
- package/store/normalizeRelayPayload.js.flow +51 -0
- package/store/readInlineData.js.flow +75 -0
- package/subscription/requestSubscription.js.flow +100 -0
- package/util/JSResourceTypes.flow.js.flow +20 -0
- package/util/NormalizationNode.js.flow +191 -0
- package/util/ReaderNode.js.flow +208 -0
- package/util/RelayConcreteNode.js.flow +80 -0
- package/util/RelayDefaultHandleKey.js.flow +17 -0
- package/util/RelayError.js.flow +33 -0
- package/util/RelayFeatureFlags.js.flow +30 -0
- package/util/RelayProfiler.js.flow +284 -0
- package/util/RelayReplaySubject.js.flow +134 -0
- package/util/RelayRuntimeTypes.js.flow +70 -0
- package/util/createPayloadFor3DField.js.flow +43 -0
- package/util/deepFreeze.js.flow +36 -0
- package/util/generateID.js.flow +21 -0
- package/util/getFragmentIdentifier.js.flow +52 -0
- package/util/getRelayHandleKey.js.flow +41 -0
- package/util/getRequestIdentifier.js.flow +41 -0
- package/util/isPromise.js.flow +21 -0
- package/util/isScalarAndEqual.js.flow +26 -0
- package/util/recycleNodesInto.js.flow +80 -0
- package/util/resolveImmediate.js.flow +30 -0
- package/util/stableCopy.js.flow +35 -0
- package/lib/handlers/RelayDefaultMissingFieldHandlers.js +0 -26
- package/lib/store/RelayConnection.js +0 -36
- package/lib/store/RelayConnectionResolver.js +0 -177
- package/lib/store/RelayRecordSourceObjectImpl.js +0 -78
- package/lib/util/getFragmentSpecIdentifier.js +0 -26
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
* @flow
|
|
8
|
+
* @emails oncall+relay
|
|
9
|
+
* @format
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// flowlint ambiguous-object-type:error
|
|
13
|
+
|
|
14
|
+
'use strict';
|
|
15
|
+
|
|
16
|
+
const RelayDefaultHandlerProvider = require('../handlers/RelayDefaultHandlerProvider');
|
|
17
|
+
const RelayFeatureFlags = require('../util/RelayFeatureFlags');
|
|
18
|
+
const RelayModernQueryExecutor = require('./RelayModernQueryExecutor');
|
|
19
|
+
const RelayObservable = require('../network/RelayObservable');
|
|
20
|
+
const RelayOperationTracker = require('../store/RelayOperationTracker');
|
|
21
|
+
const RelayPublishQueue = require('./RelayPublishQueue');
|
|
22
|
+
const RelayRecordSource = require('./RelayRecordSource');
|
|
23
|
+
|
|
24
|
+
const defaultGetDataID = require('./defaultGetDataID');
|
|
25
|
+
const generateID = require('../util/generateID');
|
|
26
|
+
const invariant = require('invariant');
|
|
27
|
+
|
|
28
|
+
import type {HandlerProvider} from '../handlers/RelayDefaultHandlerProvider';
|
|
29
|
+
import type {
|
|
30
|
+
GraphQLResponse,
|
|
31
|
+
INetwork,
|
|
32
|
+
LogRequestInfoFunction,
|
|
33
|
+
PayloadData,
|
|
34
|
+
UploadableMap,
|
|
35
|
+
} from '../network/RelayNetworkTypes';
|
|
36
|
+
import type {Observer} from '../network/RelayObservable';
|
|
37
|
+
import type {RequestParameters} from '../util/RelayConcreteNode';
|
|
38
|
+
import type {
|
|
39
|
+
CacheConfig,
|
|
40
|
+
Disposable,
|
|
41
|
+
RenderPolicy,
|
|
42
|
+
Variables,
|
|
43
|
+
} from '../util/RelayRuntimeTypes';
|
|
44
|
+
import type {ActiveState} from './RelayModernQueryExecutor';
|
|
45
|
+
import type {TaskScheduler} from './RelayModernQueryExecutor';
|
|
46
|
+
import type {GetDataID} from './RelayResponseNormalizer';
|
|
47
|
+
import type {
|
|
48
|
+
IEnvironment,
|
|
49
|
+
LogFunction,
|
|
50
|
+
MissingFieldHandler,
|
|
51
|
+
OperationAvailability,
|
|
52
|
+
OperationDescriptor,
|
|
53
|
+
OperationLoader,
|
|
54
|
+
OperationTracker,
|
|
55
|
+
OptimisticResponseConfig,
|
|
56
|
+
OptimisticUpdateFunction,
|
|
57
|
+
PublishQueue,
|
|
58
|
+
SelectorStoreUpdater,
|
|
59
|
+
SingularReaderSelector,
|
|
60
|
+
Snapshot,
|
|
61
|
+
Store,
|
|
62
|
+
StoreUpdater,
|
|
63
|
+
} from './RelayStoreTypes';
|
|
64
|
+
|
|
65
|
+
export type EnvironmentConfig = {|
|
|
66
|
+
+configName?: string,
|
|
67
|
+
+handlerProvider?: ?HandlerProvider,
|
|
68
|
+
+treatMissingFieldsAsNull?: boolean,
|
|
69
|
+
+log?: ?LogFunction,
|
|
70
|
+
+operationLoader?: ?OperationLoader,
|
|
71
|
+
+network: INetwork,
|
|
72
|
+
+scheduler?: ?TaskScheduler,
|
|
73
|
+
+store: Store,
|
|
74
|
+
+missingFieldHandlers?: ?$ReadOnlyArray<MissingFieldHandler>,
|
|
75
|
+
+operationTracker?: ?OperationTracker,
|
|
76
|
+
/**
|
|
77
|
+
* This method is likely to change in future versions, use at your own risk.
|
|
78
|
+
* It can potentially break existing calls like store.get(<id>),
|
|
79
|
+
* because the internal ID might not be the `id` field on the node anymore
|
|
80
|
+
*/
|
|
81
|
+
+UNSTABLE_DO_NOT_USE_getDataID?: ?GetDataID,
|
|
82
|
+
+UNSTABLE_defaultRenderPolicy?: ?RenderPolicy,
|
|
83
|
+
+options?: mixed,
|
|
84
|
+
+isServer?: boolean,
|
|
85
|
+
|};
|
|
86
|
+
|
|
87
|
+
class RelayModernEnvironment implements IEnvironment {
|
|
88
|
+
__log: LogFunction;
|
|
89
|
+
+_defaultRenderPolicy: RenderPolicy;
|
|
90
|
+
_operationLoader: ?OperationLoader;
|
|
91
|
+
_network: INetwork;
|
|
92
|
+
_publishQueue: PublishQueue;
|
|
93
|
+
_scheduler: ?TaskScheduler;
|
|
94
|
+
_store: Store;
|
|
95
|
+
configName: ?string;
|
|
96
|
+
_missingFieldHandlers: ?$ReadOnlyArray<MissingFieldHandler>;
|
|
97
|
+
_operationTracker: OperationTracker;
|
|
98
|
+
_getDataID: GetDataID;
|
|
99
|
+
_treatMissingFieldsAsNull: boolean;
|
|
100
|
+
_operationExecutions: Map<string, ActiveState>;
|
|
101
|
+
+options: mixed;
|
|
102
|
+
+_isServer: boolean;
|
|
103
|
+
|
|
104
|
+
constructor(config: EnvironmentConfig) {
|
|
105
|
+
this.configName = config.configName;
|
|
106
|
+
const handlerProvider = config.handlerProvider
|
|
107
|
+
? config.handlerProvider
|
|
108
|
+
: RelayDefaultHandlerProvider;
|
|
109
|
+
this._treatMissingFieldsAsNull = config.treatMissingFieldsAsNull === true;
|
|
110
|
+
const operationLoader = config.operationLoader;
|
|
111
|
+
if (__DEV__) {
|
|
112
|
+
if (operationLoader != null) {
|
|
113
|
+
invariant(
|
|
114
|
+
typeof operationLoader === 'object' &&
|
|
115
|
+
typeof operationLoader.get === 'function' &&
|
|
116
|
+
typeof operationLoader.load === 'function',
|
|
117
|
+
'RelayModernEnvironment: Expected `operationLoader` to be an object ' +
|
|
118
|
+
'with get() and load() functions, got `%s`.',
|
|
119
|
+
operationLoader,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
this.__log = config.log ?? emptyFunction;
|
|
124
|
+
this._defaultRenderPolicy =
|
|
125
|
+
config.UNSTABLE_defaultRenderPolicy ??
|
|
126
|
+
RelayFeatureFlags.ENABLE_PARTIAL_RENDERING_DEFAULT === true
|
|
127
|
+
? 'partial'
|
|
128
|
+
: 'full';
|
|
129
|
+
this._operationLoader = operationLoader;
|
|
130
|
+
this._operationExecutions = new Map();
|
|
131
|
+
this._network = config.network;
|
|
132
|
+
this._getDataID = config.UNSTABLE_DO_NOT_USE_getDataID ?? defaultGetDataID;
|
|
133
|
+
this._publishQueue = new RelayPublishQueue(
|
|
134
|
+
config.store,
|
|
135
|
+
handlerProvider,
|
|
136
|
+
this._getDataID,
|
|
137
|
+
);
|
|
138
|
+
this._scheduler = config.scheduler ?? null;
|
|
139
|
+
this._store = config.store;
|
|
140
|
+
this.options = config.options;
|
|
141
|
+
this._isServer = config.isServer ?? false;
|
|
142
|
+
|
|
143
|
+
(this: any).__setNet = newNet => (this._network = newNet);
|
|
144
|
+
|
|
145
|
+
if (__DEV__) {
|
|
146
|
+
const {inspect} = require('./StoreInspector');
|
|
147
|
+
(this: any).DEBUG_inspect = (dataID: ?string) => inspect(this, dataID);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Register this Relay Environment with Relay DevTools if it exists.
|
|
151
|
+
// Note: this must always be the last step in the constructor.
|
|
152
|
+
const _global =
|
|
153
|
+
typeof global !== 'undefined'
|
|
154
|
+
? global
|
|
155
|
+
: typeof window !== 'undefined'
|
|
156
|
+
? window
|
|
157
|
+
: undefined;
|
|
158
|
+
const devToolsHook = _global && _global.__RELAY_DEVTOOLS_HOOK__;
|
|
159
|
+
if (devToolsHook) {
|
|
160
|
+
devToolsHook.registerEnvironment(this);
|
|
161
|
+
}
|
|
162
|
+
this._missingFieldHandlers = config.missingFieldHandlers;
|
|
163
|
+
this._operationTracker =
|
|
164
|
+
config.operationTracker ?? new RelayOperationTracker();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
getStore(): Store {
|
|
168
|
+
return this._store;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
getNetwork(): INetwork {
|
|
172
|
+
return this._network;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
getOperationTracker(): RelayOperationTracker {
|
|
176
|
+
return this._operationTracker;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
isRequestActive(requestIdentifier: string): boolean {
|
|
180
|
+
const activeState = this._operationExecutions.get(requestIdentifier);
|
|
181
|
+
return activeState === 'active';
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
UNSTABLE_getDefaultRenderPolicy(): RenderPolicy {
|
|
185
|
+
return this._defaultRenderPolicy;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
applyUpdate(optimisticUpdate: OptimisticUpdateFunction): Disposable {
|
|
189
|
+
const dispose = () => {
|
|
190
|
+
this._publishQueue.revertUpdate(optimisticUpdate);
|
|
191
|
+
this._publishQueue.run();
|
|
192
|
+
};
|
|
193
|
+
this._publishQueue.applyUpdate(optimisticUpdate);
|
|
194
|
+
this._publishQueue.run();
|
|
195
|
+
return {dispose};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
revertUpdate(update: OptimisticUpdateFunction): void {
|
|
199
|
+
this._publishQueue.revertUpdate(update);
|
|
200
|
+
this._publishQueue.run();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
replaceUpdate(
|
|
204
|
+
update: OptimisticUpdateFunction,
|
|
205
|
+
newUpdate: OptimisticUpdateFunction,
|
|
206
|
+
): void {
|
|
207
|
+
this._publishQueue.revertUpdate(update);
|
|
208
|
+
this._publishQueue.applyUpdate(newUpdate);
|
|
209
|
+
this._publishQueue.run();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
applyMutation(optimisticConfig: OptimisticResponseConfig): Disposable {
|
|
213
|
+
const subscription = RelayObservable.create(sink => {
|
|
214
|
+
const source = RelayObservable.create(_sink => {});
|
|
215
|
+
const executor = RelayModernQueryExecutor.execute({
|
|
216
|
+
operation: optimisticConfig.operation,
|
|
217
|
+
operationExecutions: this._operationExecutions,
|
|
218
|
+
operationLoader: this._operationLoader,
|
|
219
|
+
optimisticConfig,
|
|
220
|
+
publishQueue: this._publishQueue,
|
|
221
|
+
scheduler: this._scheduler,
|
|
222
|
+
sink,
|
|
223
|
+
source,
|
|
224
|
+
store: this._store,
|
|
225
|
+
updater: null,
|
|
226
|
+
operationTracker: this._operationTracker,
|
|
227
|
+
getDataID: this._getDataID,
|
|
228
|
+
treatMissingFieldsAsNull: this._treatMissingFieldsAsNull,
|
|
229
|
+
});
|
|
230
|
+
return () => executor.cancel();
|
|
231
|
+
}).subscribe({});
|
|
232
|
+
return {
|
|
233
|
+
dispose: () => subscription.unsubscribe(),
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
check(operation: OperationDescriptor): OperationAvailability {
|
|
238
|
+
if (
|
|
239
|
+
this._missingFieldHandlers == null ||
|
|
240
|
+
this._missingFieldHandlers.length === 0
|
|
241
|
+
) {
|
|
242
|
+
return this._store.check(operation);
|
|
243
|
+
}
|
|
244
|
+
return this._checkSelectorAndHandleMissingFields(
|
|
245
|
+
operation,
|
|
246
|
+
this._missingFieldHandlers,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
commitPayload(operation: OperationDescriptor, payload: PayloadData): void {
|
|
251
|
+
RelayObservable.create(sink => {
|
|
252
|
+
const executor = RelayModernQueryExecutor.execute({
|
|
253
|
+
operation: operation,
|
|
254
|
+
operationExecutions: this._operationExecutions,
|
|
255
|
+
operationLoader: this._operationLoader,
|
|
256
|
+
optimisticConfig: null,
|
|
257
|
+
publishQueue: this._publishQueue,
|
|
258
|
+
scheduler: null, // make sure the first payload is sync
|
|
259
|
+
sink,
|
|
260
|
+
source: RelayObservable.from({
|
|
261
|
+
data: payload,
|
|
262
|
+
}),
|
|
263
|
+
store: this._store,
|
|
264
|
+
updater: null,
|
|
265
|
+
operationTracker: this._operationTracker,
|
|
266
|
+
getDataID: this._getDataID,
|
|
267
|
+
isClientPayload: true,
|
|
268
|
+
treatMissingFieldsAsNull: this._treatMissingFieldsAsNull,
|
|
269
|
+
});
|
|
270
|
+
return () => executor.cancel();
|
|
271
|
+
}).subscribe({});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
commitUpdate(updater: StoreUpdater): void {
|
|
275
|
+
this._publishQueue.commitUpdate(updater);
|
|
276
|
+
this._publishQueue.run();
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
lookup(readSelector: SingularReaderSelector): Snapshot {
|
|
280
|
+
return this._store.lookup(readSelector);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
subscribe(
|
|
284
|
+
snapshot: Snapshot,
|
|
285
|
+
callback: (snapshot: Snapshot) => void,
|
|
286
|
+
): Disposable {
|
|
287
|
+
return this._store.subscribe(snapshot, callback);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
retain(operation: OperationDescriptor): Disposable {
|
|
291
|
+
return this._store.retain(operation);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
isServer(): boolean {
|
|
295
|
+
return this._isServer;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
_checkSelectorAndHandleMissingFields(
|
|
299
|
+
operation: OperationDescriptor,
|
|
300
|
+
handlers: $ReadOnlyArray<MissingFieldHandler>,
|
|
301
|
+
): OperationAvailability {
|
|
302
|
+
const target = RelayRecordSource.create();
|
|
303
|
+
const result = this._store.check(operation, {target, handlers});
|
|
304
|
+
if (target.size() > 0) {
|
|
305
|
+
this._publishQueue.commitSource(target);
|
|
306
|
+
this._publishQueue.run();
|
|
307
|
+
}
|
|
308
|
+
return result;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Returns an Observable of GraphQLResponse resulting from executing the
|
|
313
|
+
* provided Query or Subscription operation, each result of which is then
|
|
314
|
+
* normalized and committed to the publish queue.
|
|
315
|
+
*
|
|
316
|
+
* Note: Observables are lazy, so calling this method will do nothing until
|
|
317
|
+
* the result is subscribed to: environment.execute({...}).subscribe({...}).
|
|
318
|
+
*/
|
|
319
|
+
execute({
|
|
320
|
+
operation,
|
|
321
|
+
cacheConfig,
|
|
322
|
+
updater,
|
|
323
|
+
}: {
|
|
324
|
+
operation: OperationDescriptor,
|
|
325
|
+
cacheConfig?: ?CacheConfig,
|
|
326
|
+
updater?: ?SelectorStoreUpdater,
|
|
327
|
+
...
|
|
328
|
+
}): RelayObservable<GraphQLResponse> {
|
|
329
|
+
const [logObserver, logRequestInfo] = this.__createLogObserver(
|
|
330
|
+
operation.request.node.params,
|
|
331
|
+
operation.request.variables,
|
|
332
|
+
);
|
|
333
|
+
return RelayObservable.create(sink => {
|
|
334
|
+
const source = this._network.execute(
|
|
335
|
+
operation.request.node.params,
|
|
336
|
+
operation.request.variables,
|
|
337
|
+
cacheConfig || {},
|
|
338
|
+
null,
|
|
339
|
+
logRequestInfo,
|
|
340
|
+
);
|
|
341
|
+
const executor = RelayModernQueryExecutor.execute({
|
|
342
|
+
operation,
|
|
343
|
+
operationExecutions: this._operationExecutions,
|
|
344
|
+
operationLoader: this._operationLoader,
|
|
345
|
+
optimisticConfig: null,
|
|
346
|
+
publishQueue: this._publishQueue,
|
|
347
|
+
scheduler: this._scheduler,
|
|
348
|
+
sink,
|
|
349
|
+
source,
|
|
350
|
+
store: this._store,
|
|
351
|
+
updater,
|
|
352
|
+
operationTracker: this._operationTracker,
|
|
353
|
+
getDataID: this._getDataID,
|
|
354
|
+
treatMissingFieldsAsNull: this._treatMissingFieldsAsNull,
|
|
355
|
+
});
|
|
356
|
+
return () => executor.cancel();
|
|
357
|
+
}).do(logObserver);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Returns an Observable of GraphQLResponse resulting from executing the
|
|
362
|
+
* provided Mutation operation, the result of which is then normalized and
|
|
363
|
+
* committed to the publish queue along with an optional optimistic response
|
|
364
|
+
* or updater.
|
|
365
|
+
*
|
|
366
|
+
* Note: Observables are lazy, so calling this method will do nothing until
|
|
367
|
+
* the result is subscribed to:
|
|
368
|
+
* environment.executeMutation({...}).subscribe({...}).
|
|
369
|
+
*/
|
|
370
|
+
executeMutation({
|
|
371
|
+
cacheConfig,
|
|
372
|
+
operation,
|
|
373
|
+
optimisticResponse,
|
|
374
|
+
optimisticUpdater,
|
|
375
|
+
updater,
|
|
376
|
+
uploadables,
|
|
377
|
+
}: {|
|
|
378
|
+
cacheConfig?: ?CacheConfig,
|
|
379
|
+
operation: OperationDescriptor,
|
|
380
|
+
optimisticUpdater?: ?SelectorStoreUpdater,
|
|
381
|
+
optimisticResponse?: ?Object,
|
|
382
|
+
updater?: ?SelectorStoreUpdater,
|
|
383
|
+
uploadables?: ?UploadableMap,
|
|
384
|
+
|}): RelayObservable<GraphQLResponse> {
|
|
385
|
+
const [logObserver, logRequestInfo] = this.__createLogObserver(
|
|
386
|
+
operation.request.node.params,
|
|
387
|
+
operation.request.variables,
|
|
388
|
+
);
|
|
389
|
+
return RelayObservable.create(sink => {
|
|
390
|
+
let optimisticConfig;
|
|
391
|
+
if (optimisticResponse || optimisticUpdater) {
|
|
392
|
+
optimisticConfig = {
|
|
393
|
+
operation: operation,
|
|
394
|
+
response: optimisticResponse,
|
|
395
|
+
updater: optimisticUpdater,
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
const source = this._network.execute(
|
|
399
|
+
operation.request.node.params,
|
|
400
|
+
operation.request.variables,
|
|
401
|
+
{
|
|
402
|
+
...cacheConfig,
|
|
403
|
+
force: true,
|
|
404
|
+
},
|
|
405
|
+
uploadables,
|
|
406
|
+
logRequestInfo,
|
|
407
|
+
);
|
|
408
|
+
const executor = RelayModernQueryExecutor.execute({
|
|
409
|
+
operation,
|
|
410
|
+
operationExecutions: this._operationExecutions,
|
|
411
|
+
operationLoader: this._operationLoader,
|
|
412
|
+
optimisticConfig,
|
|
413
|
+
publishQueue: this._publishQueue,
|
|
414
|
+
scheduler: this._scheduler,
|
|
415
|
+
sink,
|
|
416
|
+
source,
|
|
417
|
+
store: this._store,
|
|
418
|
+
updater,
|
|
419
|
+
operationTracker: this._operationTracker,
|
|
420
|
+
getDataID: this._getDataID,
|
|
421
|
+
treatMissingFieldsAsNull: this._treatMissingFieldsAsNull,
|
|
422
|
+
});
|
|
423
|
+
return () => executor.cancel();
|
|
424
|
+
}).do(logObserver);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Returns an Observable of GraphQLResponse resulting from executing the
|
|
429
|
+
* provided Query or Subscription operation responses, the result of which is
|
|
430
|
+
* then normalized and comitted to the publish queue.
|
|
431
|
+
*
|
|
432
|
+
* Note: Observables are lazy, so calling this method will do nothing until
|
|
433
|
+
* the result is subscribed to:
|
|
434
|
+
* environment.executeWithSource({...}).subscribe({...}).
|
|
435
|
+
*/
|
|
436
|
+
executeWithSource({
|
|
437
|
+
operation,
|
|
438
|
+
source,
|
|
439
|
+
}: {|
|
|
440
|
+
operation: OperationDescriptor,
|
|
441
|
+
source: RelayObservable<GraphQLResponse>,
|
|
442
|
+
|}): RelayObservable<GraphQLResponse> {
|
|
443
|
+
return RelayObservable.create(sink => {
|
|
444
|
+
const executor = RelayModernQueryExecutor.execute({
|
|
445
|
+
operation,
|
|
446
|
+
operationExecutions: this._operationExecutions,
|
|
447
|
+
operationLoader: this._operationLoader,
|
|
448
|
+
operationTracker: this._operationTracker,
|
|
449
|
+
optimisticConfig: null,
|
|
450
|
+
publishQueue: this._publishQueue,
|
|
451
|
+
scheduler: this._scheduler,
|
|
452
|
+
sink,
|
|
453
|
+
source,
|
|
454
|
+
store: this._store,
|
|
455
|
+
getDataID: this._getDataID,
|
|
456
|
+
treatMissingFieldsAsNull: this._treatMissingFieldsAsNull,
|
|
457
|
+
});
|
|
458
|
+
return () => executor.cancel();
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
toJSON(): mixed {
|
|
463
|
+
return `RelayModernEnvironment(${this.configName ?? ''})`;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
__createLogObserver(
|
|
467
|
+
params: RequestParameters,
|
|
468
|
+
variables: Variables,
|
|
469
|
+
): [Observer<GraphQLResponse>, LogRequestInfoFunction] {
|
|
470
|
+
const transactionID = generateID();
|
|
471
|
+
const log = this.__log;
|
|
472
|
+
const logObserver = {
|
|
473
|
+
start: subscription => {
|
|
474
|
+
log({
|
|
475
|
+
name: 'execute.start',
|
|
476
|
+
transactionID,
|
|
477
|
+
params,
|
|
478
|
+
variables,
|
|
479
|
+
});
|
|
480
|
+
},
|
|
481
|
+
next: response => {
|
|
482
|
+
log({
|
|
483
|
+
name: 'execute.next',
|
|
484
|
+
transactionID,
|
|
485
|
+
response,
|
|
486
|
+
});
|
|
487
|
+
},
|
|
488
|
+
error: error => {
|
|
489
|
+
log({
|
|
490
|
+
name: 'execute.error',
|
|
491
|
+
transactionID,
|
|
492
|
+
error,
|
|
493
|
+
});
|
|
494
|
+
},
|
|
495
|
+
complete: () => {
|
|
496
|
+
log({
|
|
497
|
+
name: 'execute.complete',
|
|
498
|
+
transactionID,
|
|
499
|
+
});
|
|
500
|
+
},
|
|
501
|
+
unsubscribe: () => {
|
|
502
|
+
log({
|
|
503
|
+
name: 'execute.unsubscribe',
|
|
504
|
+
transactionID,
|
|
505
|
+
});
|
|
506
|
+
},
|
|
507
|
+
};
|
|
508
|
+
const logRequestInfo = info => {
|
|
509
|
+
log({
|
|
510
|
+
name: 'execute.info',
|
|
511
|
+
transactionID,
|
|
512
|
+
info,
|
|
513
|
+
});
|
|
514
|
+
};
|
|
515
|
+
return [logObserver, logRequestInfo];
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// Add a sigil for detection by `isRelayModernEnvironment()` to avoid a
|
|
520
|
+
// realm-specific instanceof check, and to aid in module tree-shaking to
|
|
521
|
+
// avoid requiring all of RelayRuntime just to detect its environment.
|
|
522
|
+
(RelayModernEnvironment: any).prototype['@@RelayModernEnvironment'] = true;
|
|
523
|
+
|
|
524
|
+
function emptyFunction() {}
|
|
525
|
+
|
|
526
|
+
module.exports = RelayModernEnvironment;
|