-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
RelayEnvironment.js
275 lines (250 loc) · 7.6 KB
/
RelayEnvironment.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
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule RelayEnvironment
* @typechecks
* @flow
*/
'use strict';
const GraphQLStoreQueryResolver = require('GraphQLStoreQueryResolver');
import type {ChangeSubscription} from 'RelayInternalTypes';
import type RelayMutation from 'RelayMutation';
import type RelayMutationTransaction from 'RelayMutationTransaction';
import type {MutationCallback, QueryCallback} from 'RelayNetworkLayer';
import type RelayQuery from 'RelayQuery';
const RelayQueryResultObservable = require('RelayQueryResultObservable');
const RelayStoreData = require('RelayStoreData');
import type {TaskScheduler} from 'RelayTaskQueue';
import type {NetworkLayer} from 'RelayTypes';
const forEachRootCallArg = require('forEachRootCallArg');
const readRelayQueryData = require('readRelayQueryData');
const relayUnstableBatchedUpdates = require('relayUnstableBatchedUpdates');
const warning = require('warning');
import type {
Abortable,
Observable,
RelayMutationTransactionCommitCallbacks,
ReadyStateChangeCallback,
StoreReaderData,
StoreReaderOptions,
} from 'RelayTypes';
import type {
DataID,
RelayQuerySet,
} from 'RelayInternalTypes';
export type FragmentResolver = {
dispose: () => void;
resolve: (
fragment: RelayQuery.Fragment,
dataIDs: DataID | Array<DataID>
) => ?(StoreReaderData | Array<?StoreReaderData>);
};
export type RelayEnvironmentInterface = {
forceFetch: (
querySet: RelayQuerySet,
onReadyStateChange: ReadyStateChangeCallback
) => Abortable;
getFragmentResolver: (
fragment: RelayQuery.Fragment,
onNext: () => void
) => FragmentResolver;
getStoreData: () => RelayStoreData;
primeCache: (
querySet: RelayQuerySet,
onReadyStateChange: ReadyStateChangeCallback
) => Abortable;
read: (
node: RelayQuery.Node,
dataID: DataID,
options?: StoreReaderOptions
) => ?StoreReaderData;
};
/**
* @public
*
* `RelayEnvironment` is the public API for Relay core. Each instance provides
* an isolated environment with:
* - Methods for fetchng and updating data.
* - An in-memory cache of fetched data.
* - A configurable network layer for resolving queries/mutations.
* - A configurable task scheduler to control when internal tasks are executed.
* - A configurable cache manager for persisting data between sessions.
*
* No data or configuration is shared between instances. We recommend creating
* one `RelayEnvironment` instance per user: client apps may share a single
* instance, server apps may create one instance per HTTP request.
*/
class RelayEnvironment {
_storeData: RelayStoreData;
constructor() {
this._storeData = new RelayStoreData();
this._storeData.getChangeEmitter().injectBatchingStrategy(
relayUnstableBatchedUpdates
);
}
/**
* @internal
*/
getStoreData(): RelayStoreData {
return this._storeData;
}
/**
* @internal
*/
injectDefaultNetworkLayer(networkLayer: ?NetworkLayer) {
this._storeData.getNetworkLayer().injectDefaultImplementation(networkLayer);
}
injectNetworkLayer(networkLayer: ?NetworkLayer) {
this._storeData.getNetworkLayer().injectImplementation(networkLayer);
}
addNetworkSubscriber(
queryCallback?: ?QueryCallback,
mutationCallback?: ?MutationCallback
): ChangeSubscription {
return this._storeData.getNetworkLayer().addNetworkSubscriber(
queryCallback,
mutationCallback
);
}
injectTaskScheduler(scheduler: ?TaskScheduler): void {
this._storeData.injectTaskScheduler(scheduler);
}
/**
* Primes the store by sending requests for any missing data that would be
* required to satisfy the supplied set of queries.
*/
primeCache(
querySet: RelayQuerySet,
callback: ReadyStateChangeCallback
): Abortable {
return this._storeData.getQueryRunner().run(querySet, callback);
}
/**
* Forces the supplied set of queries to be fetched and written to the store.
* Any data that previously satisfied the queries will be overwritten.
*/
forceFetch(
querySet: RelayQuerySet,
callback: ReadyStateChangeCallback
): Abortable {
return this._storeData.getQueryRunner().forceFetch(querySet, callback);
}
/**
* Reads query data anchored at the supplied data ID.
*/
read(
node: RelayQuery.Node,
dataID: DataID,
options?: StoreReaderOptions
): ?StoreReaderData {
return readRelayQueryData(this._storeData, node, dataID, options).data;
}
/**
* Reads query data anchored at the supplied data IDs.
*/
readAll(
node: RelayQuery.Node,
dataIDs: Array<DataID>,
options?: StoreReaderOptions
): Array<?StoreReaderData> {
return dataIDs.map(
dataID => readRelayQueryData(this._storeData, node, dataID, options).data
);
}
/**
* Reads query data, where each element in the result array corresponds to a
* root call argument. If the root call has no arguments, the result array
* will contain exactly one element.
*/
readQuery(
root: RelayQuery.Root,
options?: StoreReaderOptions
): Array<?StoreReaderData> {
const queuedStore = this._storeData.getQueuedStore();
const storageKey = root.getStorageKey();
const results = [];
forEachRootCallArg(root, ({identifyingArgKey}) => {
let data;
const dataID = queuedStore.getDataID(storageKey, identifyingArgKey);
if (dataID != null) {
data = this.read(root, dataID, options);
}
results.push(data);
});
return results;
}
/**
* Reads and subscribes to query data anchored at the supplied data ID. The
* returned observable emits updates as the data changes over time.
*/
observe(
fragment: RelayQuery.Fragment,
dataID: DataID
): Observable<?StoreReaderData> {
return new RelayQueryResultObservable(this._storeData, fragment, dataID);
}
/**
* @internal
*
* Returns a fragment "resolver" - a subscription to the results of a fragment
* and a means to access the latest results. This is a transitional API and
* not recommended for general use.
*/
getFragmentResolver(
fragment: RelayQuery.Fragment,
onNext: () => void
): FragmentResolver {
return new GraphQLStoreQueryResolver(
this._storeData,
fragment,
onNext
);
}
/**
* Adds an update to the store without committing it. The returned
* RelayMutationTransaction can be committed or rolled back at a later time.
*/
applyUpdate(
mutation: RelayMutation,
callbacks?: RelayMutationTransactionCommitCallbacks
): RelayMutationTransaction {
mutation.bindEnvironment(this);
return this._storeData.getMutationQueue()
.createTransaction(mutation, callbacks)
.applyOptimistic();
}
/**
* Adds an update to the store and commits it immediately. Returns
* the RelayMutationTransaction.
*/
commitUpdate(
mutation: RelayMutation,
callbacks?: RelayMutationTransactionCommitCallbacks
): RelayMutationTransaction {
return this
.applyUpdate(mutation, callbacks)
.commit();
}
/**
* @deprecated
*
* Method renamed to commitUpdate
*/
update(
mutation: RelayMutation,
callbacks?: RelayMutationTransactionCommitCallbacks
): void {
warning(
false,
'`Relay.Store.update` is deprecated. Please use' +
' `Relay.Store.commitUpdate` or `Relay.Store.applyUpdate` instead.'
);
this.commitUpdate(mutation, callbacks);
}
}
module.exports = RelayEnvironment;