relay-runtime 2.0.0-rc.2 → 2.0.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/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Relay v2.0.0-rc.2
2
+ * Relay v2.0.0
3
3
  *
4
4
  * Copyright (c) 2013-present, Facebook, Inc.
5
5
  *
@@ -265,6 +265,12 @@ function () {
265
265
 
266
266
  break;
267
267
 
268
+ case require("./RelayConcreteNode").DEFER:
269
+ case require("./RelayConcreteNode").STREAM:
270
+ _this2._traverseSelections(selection.selections, dataID);
271
+
272
+ break;
273
+
268
274
  case require("./RelayConcreteNode").SCALAR_HANDLE:
269
275
  case require("./RelayConcreteNode").FRAGMENT_SPREAD:
270
276
  !false ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'RelayAsyncLoader(): Unexpected ast kind `%s`.', selection.kind) : require("fbjs/lib/invariant")(false) : void 0; // $FlowExpectedError - we need the break; for OSS linter
@@ -22,6 +22,7 @@
22
22
  */
23
23
  var RelayConcreteNode = {
24
24
  CONDITION: 'Condition',
25
+ DEFER: 'Defer',
25
26
  FRAGMENT: 'Fragment',
26
27
  FRAGMENT_SPREAD: 'FragmentSpread',
27
28
  INLINE_FRAGMENT: 'InlineFragment',
@@ -36,6 +37,7 @@ var RelayConcreteNode = {
36
37
  SCALAR_FIELD: 'ScalarField',
37
38
  SCALAR_HANDLE: 'ScalarHandle',
38
39
  SPLIT_OPERATION: 'SplitOperation',
40
+ STREAM: 'Stream',
39
41
  VARIABLE: 'Variable'
40
42
  };
41
43
  module.exports = RelayConcreteNode;
@@ -163,117 +163,16 @@ function () {
163
163
 
164
164
 
165
165
  _proto.execute = function execute(_ref2) {
166
- var _this3 = this;
167
-
168
166
  var operation = _ref2.operation,
169
167
  cacheConfig = _ref2.cacheConfig,
170
168
  updater = _ref2.updater;
171
- return require("./RelayObservable").create(function (sink) {
172
- var optimisticResponse = null;
173
- var subscriptions = new Set();
174
-
175
- function start(subscription) {
176
- // NOTE: store the subscription object on the observer so that it
177
- // can be cleaned up in complete() or the dispose function.
178
- this._subscription = subscription;
179
- subscriptions.add(subscription);
180
- }
181
-
182
- function complete() {
183
- subscriptions["delete"](this._subscription);
184
-
185
- if (subscriptions.size === 0) {
186
- sink.complete();
187
- }
188
- } // Convert each GraphQLResponse from the network to a RelayResponsePayload
189
- // and process it
190
-
191
-
192
- function next(response) {
193
- var _response$extensions;
194
-
195
- var payload = require("./normalizePayload")(operation, response);
196
-
197
- var isOptimistic = ((_response$extensions = response.extensions) === null || _response$extensions === void 0 ? void 0 : _response$extensions.isOptimistic) === true;
198
- processRelayPayload(payload, operation, updater, isOptimistic);
199
- sink.next(response);
200
- } // Each RelayResponsePayload contains both data to publish to the store
201
- // immediately, but may also contain matchPayloads that need to be
202
- // asynchronously normalized into RelayResponsePayloads, which may
203
- // themselves have matchPayloads: this function is recursive and relies
204
- // on GraphQL queries *disallowing* recursion to ensure termination.
205
-
206
-
207
- var processRelayPayload = function processRelayPayload(payload) {
208
- var operationDescriptor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
209
- var payloadUpdater = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
210
- var isOptimistic = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
211
- var matchPayloads = payload.matchPayloads;
212
-
213
- if (matchPayloads && matchPayloads.length) {
214
- var operationLoader = _this3._operationLoader;
215
- !operationLoader ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'RelayModernEnvironment: Expected an operationLoader to be ' + 'configured when using `@match`.') : require("fbjs/lib/invariant")(false) : void 0;
216
- matchPayloads.forEach(function (matchPayload) {
217
- processMatchPayload(processRelayPayload, operationLoader, matchPayload).subscribe({
218
- complete: complete,
219
- error: sink.error,
220
- start: start
221
- });
222
- });
223
- }
224
-
225
- if (isOptimistic) {
226
- !(optimisticResponse === null) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'environment.execute: only support one optimistic response per ' + 'execute.') : require("fbjs/lib/invariant")(false) : void 0;
227
- optimisticResponse = {
228
- source: payload.source,
229
- fieldPayloads: payload.fieldPayloads
230
- };
231
-
232
- _this3._publishQueue.applyUpdate(optimisticResponse);
233
-
234
- _this3._publishQueue.run();
235
- } else {
236
- if (optimisticResponse !== null) {
237
- _this3._publishQueue.revertUpdate(optimisticResponse);
238
-
239
- optimisticResponse = null;
240
- }
241
-
242
- if (operationDescriptor && payloadUpdater) {
243
- _this3._publishQueue.commitPayload(operationDescriptor, payload, payloadUpdater);
244
- } else {
245
- _this3._publishQueue.commitRelayPayload(payload);
246
- }
247
-
248
- _this3._publishQueue.run();
249
- }
250
- };
251
-
252
- var node = operation.node;
253
-
254
- _this3._network.execute(node.params, operation.variables, cacheConfig || {}).subscribe({
255
- complete: complete,
256
- next: next,
257
- error: sink.error,
258
- start: start
259
- });
260
-
261
- return function () {
262
- if (subscriptions.size !== 0) {
263
- subscriptions.forEach(function (sub) {
264
- return sub.unsubscribe();
265
- });
266
- subscriptions.clear();
267
- }
268
-
269
- if (optimisticResponse !== null) {
270
- _this3._publishQueue.revertUpdate(optimisticResponse);
271
-
272
- optimisticResponse = null;
273
-
274
- _this3._publishQueue.run();
275
- }
276
- };
169
+ return require("./RelayModernQueryExecutor").execute({
170
+ operation: operation,
171
+ cacheConfig: cacheConfig,
172
+ updater: updater,
173
+ network: this._network,
174
+ operationLoader: this._operationLoader,
175
+ publishQueue: this._publishQueue
277
176
  });
278
177
  };
279
178
  /**
@@ -289,7 +188,7 @@ function () {
289
188
 
290
189
 
291
190
  _proto.executeMutation = function executeMutation(_ref3) {
292
- var _this4 = this;
191
+ var _this3 = this;
293
192
 
294
193
  var operation = _ref3.operation,
295
194
  optimisticResponse = _ref3.optimisticResponse,
@@ -312,21 +211,21 @@ function () {
312
211
  }, uploadables)["do"]({
313
212
  start: function start() {
314
213
  if (optimisticUpdate) {
315
- _this4._publishQueue.applyUpdate(optimisticUpdate);
214
+ _this3._publishQueue.applyUpdate(optimisticUpdate);
316
215
 
317
- _this4._publishQueue.run();
216
+ _this3._publishQueue.run();
318
217
  }
319
218
  },
320
219
  next: function next(payload) {
321
220
  if (optimisticUpdate) {
322
- _this4._publishQueue.revertUpdate(optimisticUpdate);
221
+ _this3._publishQueue.revertUpdate(optimisticUpdate);
323
222
 
324
223
  optimisticUpdate = undefined;
325
224
  }
326
225
 
327
- _this4._publishQueue.commitPayload(operation, require("./normalizePayload")(operation, payload), updater);
226
+ _this3._publishQueue.commitPayload(operation, require("./normalizePayload")(operation, payload), updater);
328
227
 
329
- _this4._publishQueue.run();
228
+ _this3._publishQueue.run();
330
229
  },
331
230
  error: function (_error) {
332
231
  function error(_x) {
@@ -340,20 +239,20 @@ function () {
340
239
  return error;
341
240
  }(function (error) {
342
241
  if (optimisticUpdate) {
343
- _this4._publishQueue.revertUpdate(optimisticUpdate);
242
+ _this3._publishQueue.revertUpdate(optimisticUpdate);
344
243
 
345
244
  optimisticUpdate = undefined;
346
245
 
347
- _this4._publishQueue.run();
246
+ _this3._publishQueue.run();
348
247
  }
349
248
  }),
350
249
  unsubscribe: function unsubscribe() {
351
250
  if (optimisticUpdate) {
352
- _this4._publishQueue.revertUpdate(optimisticUpdate);
251
+ _this3._publishQueue.revertUpdate(optimisticUpdate);
353
252
 
354
253
  optimisticUpdate = undefined;
355
254
 
356
- _this4._publishQueue.run();
255
+ _this3._publishQueue.run();
357
256
  }
358
257
  }
359
258
  });
@@ -418,51 +317,7 @@ function () {
418
317
  };
419
318
 
420
319
  return RelayModernEnvironment;
421
- }();
422
- /**
423
- * Processes a MatchFieldPayload, asynchronously resolving the fragment,
424
- * using it to normalize the field data into a RelayResponsePayload.
425
- * Because @match fields may contain other @match fields, the result of
426
- * normalizing `matchPayload` may contain *other* MatchFieldPayloads:
427
- * the processRelayPayload() callback is responsible for publishing
428
- * both the normalize payload's source as well as recursively calling
429
- * this function for any matchPayloads it contains.
430
- *
431
- * @private
432
- */
433
-
434
-
435
- function processMatchPayload(processRelayPayload, operationLoader, matchPayload) {
436
- return require("./RelayObservable").from(new Promise(function (resolve, reject) {
437
- operationLoader.load(matchPayload.operationReference).then(resolve, reject);
438
- })).map(function (operation) {
439
- if (operation == null) {
440
- return;
441
- }
442
-
443
- var selector = {
444
- dataID: matchPayload.dataID,
445
- variables: matchPayload.variables,
446
- node: operation
447
- };
448
- var source = new (require("./RelayInMemoryRecordSource"))();
449
-
450
- var matchRecord = require("./RelayModernRecord").create(matchPayload.dataID, matchPayload.typeName);
451
-
452
- source.set(matchPayload.dataID, matchRecord);
453
-
454
- var normalizeResult = require("./RelayResponseNormalizer").normalize(source, selector, matchPayload.data);
455
-
456
- var relayPayload = {
457
- errors: null,
458
- // Errors are handled as part of the parent GraphQLResponse
459
- fieldPayloads: normalizeResult.fieldPayloads,
460
- matchPayloads: normalizeResult.matchPayloads,
461
- source: source
462
- };
463
- processRelayPayload(relayPayload);
464
- });
465
- } // Add a sigil for detection by `isRelayModernEnvironment()` to avoid a
320
+ }(); // Add a sigil for detection by `isRelayModernEnvironment()` to avoid a
466
321
  // realm-specific instanceof check, and to aid in module tree-shaking to
467
322
  // avoid requiring all of RelayRuntime just to detect its environment.
468
323
 
@@ -0,0 +1,172 @@
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
+ * strict-local
8
+ * @format
9
+ * @emails oncall+relay
10
+ */
11
+ 'use strict';
12
+
13
+ /**
14
+ * Coordinates the execution of a query, handling network callbacks
15
+ * including optimistic payloads, standard payloads, resolution of match
16
+ * dependencies, etc.
17
+ */
18
+ function execute(_ref) {
19
+ var network = _ref.network,
20
+ publishQueue = _ref.publishQueue,
21
+ operation = _ref.operation,
22
+ operationLoader = _ref.operationLoader,
23
+ cacheConfig = _ref.cacheConfig,
24
+ updater = _ref.updater;
25
+ return require("./RelayObservable").create(function (sink) {
26
+ var optimisticResponse = null;
27
+ var subscriptions = new Set();
28
+
29
+ function start(subscription) {
30
+ // NOTE: store the subscription object on the observer so that it
31
+ // can be cleaned up in complete() or the dispose function.
32
+ this._subscription = subscription;
33
+ subscriptions.add(subscription);
34
+ }
35
+
36
+ function complete() {
37
+ subscriptions["delete"](this._subscription);
38
+
39
+ if (subscriptions.size === 0) {
40
+ sink.complete();
41
+ }
42
+ } // Convert each GraphQLResponse from the network to a RelayResponsePayload
43
+ // and process it
44
+
45
+
46
+ function next(response) {
47
+ var _response$extensions;
48
+
49
+ var payload = require("./normalizePayload")(operation, response);
50
+
51
+ var isOptimistic = ((_response$extensions = response.extensions) === null || _response$extensions === void 0 ? void 0 : _response$extensions.isOptimistic) === true;
52
+ processRelayPayload(payload, operation, updater, isOptimistic);
53
+ sink.next(response);
54
+ } // Each RelayResponsePayload contains both data to publish to the store
55
+ // immediately, but may also contain matchPayloads that need to be
56
+ // asynchronously normalized into RelayResponsePayloads, which may
57
+ // themselves have matchPayloads: this function is recursive and relies
58
+ // on GraphQL queries *disallowing* recursion to ensure termination.
59
+
60
+
61
+ var processRelayPayload = function processRelayPayload(payload) {
62
+ var operationDescriptor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
63
+ var payloadUpdater = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
64
+ var isOptimistic = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
65
+ var matchPayloads = payload.matchPayloads;
66
+
67
+ if (matchPayloads && matchPayloads.length) {
68
+ !operationLoader ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'RelayModernEnvironment: Expected an operationLoader to be ' + 'configured when using `@match`.') : require("fbjs/lib/invariant")(false) : void 0;
69
+ matchPayloads.forEach(function (matchPayload) {
70
+ processMatchPayload(processRelayPayload, operationLoader, matchPayload).subscribe({
71
+ complete: complete,
72
+ error: sink.error,
73
+ start: start
74
+ });
75
+ });
76
+ }
77
+
78
+ if (isOptimistic) {
79
+ !(optimisticResponse === null) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'environment.execute: only support one optimistic response per ' + 'execute.') : require("fbjs/lib/invariant")(false) : void 0;
80
+ optimisticResponse = {
81
+ source: payload.source,
82
+ fieldPayloads: payload.fieldPayloads
83
+ };
84
+ publishQueue.applyUpdate(optimisticResponse);
85
+ publishQueue.run();
86
+ } else {
87
+ if (optimisticResponse !== null) {
88
+ publishQueue.revertUpdate(optimisticResponse);
89
+ optimisticResponse = null;
90
+ }
91
+
92
+ if (operationDescriptor && payloadUpdater) {
93
+ publishQueue.commitPayload(operationDescriptor, payload, payloadUpdater);
94
+ } else {
95
+ publishQueue.commitRelayPayload(payload);
96
+ }
97
+
98
+ publishQueue.run();
99
+ }
100
+ };
101
+
102
+ var node = operation.node;
103
+ network.execute(node.params, operation.variables, cacheConfig || {}).subscribe({
104
+ complete: complete,
105
+ next: next,
106
+ error: sink.error,
107
+ start: start
108
+ });
109
+ return function () {
110
+ if (subscriptions.size !== 0) {
111
+ subscriptions.forEach(function (sub) {
112
+ return sub.unsubscribe();
113
+ });
114
+ subscriptions.clear();
115
+ }
116
+
117
+ if (optimisticResponse !== null) {
118
+ publishQueue.revertUpdate(optimisticResponse);
119
+ optimisticResponse = null;
120
+ publishQueue.run();
121
+ }
122
+ };
123
+ });
124
+ }
125
+ /**
126
+ * Processes a MatchFieldPayload, asynchronously resolving the fragment,
127
+ * using it to normalize the field data into a RelayResponsePayload.
128
+ * Because @match fields may contain other @match fields, the result of
129
+ * normalizing `matchPayload` may contain *other* MatchFieldPayloads:
130
+ * the processRelayPayload() callback is responsible for publishing
131
+ * both the normalize payload's source as well as recursively calling
132
+ * this function for any matchPayloads it contains.
133
+ *
134
+ * @private
135
+ */
136
+
137
+
138
+ function processMatchPayload(processRelayPayload, operationLoader, matchPayload) {
139
+ return require("./RelayObservable").from(new Promise(function (resolve, reject) {
140
+ operationLoader.load(matchPayload.operationReference).then(resolve, reject);
141
+ })).map(function (operation) {
142
+ if (operation == null) {
143
+ return;
144
+ }
145
+
146
+ var selector = {
147
+ dataID: matchPayload.dataID,
148
+ variables: matchPayload.variables,
149
+ node: operation
150
+ };
151
+ var source = new (require("./RelayInMemoryRecordSource"))();
152
+
153
+ var matchRecord = require("./RelayModernRecord").create(matchPayload.dataID, matchPayload.typeName);
154
+
155
+ source.set(matchPayload.dataID, matchRecord);
156
+
157
+ var normalizeResult = require("./RelayResponseNormalizer").normalize(source, selector, matchPayload.data);
158
+
159
+ var relayPayload = {
160
+ errors: null,
161
+ // Errors are handled as part of the parent GraphQLResponse
162
+ fieldPayloads: normalizeResult.fieldPayloads,
163
+ matchPayloads: normalizeResult.matchPayloads,
164
+ source: source
165
+ };
166
+ processRelayPayload(relayPayload);
167
+ });
168
+ }
169
+
170
+ module.exports = {
171
+ execute: execute
172
+ };
@@ -165,7 +165,7 @@ function () {
165
165
  return;
166
166
  }
167
167
 
168
- var _RelayReader$read = require("./RelayReader").read(this._recordSource, snapshot),
168
+ var _RelayReader$read = require("./RelayReader").read(this._recordSource, snapshot, snapshot.owner),
169
169
  data = _RelayReader$read.data,
170
170
  seenRecords = _RelayReader$read.seenRecords;
171
171
 
@@ -98,7 +98,7 @@ function () {
98
98
  kind: 'payload',
99
99
  payload: {
100
100
  fieldPayloads: fieldPayloads,
101
- selector: operation.fragment,
101
+ operation: operation,
102
102
  source: source,
103
103
  updater: updater
104
104
  }
@@ -114,7 +114,7 @@ function () {
114
114
  kind: 'payload',
115
115
  payload: {
116
116
  fieldPayloads: fieldPayloads,
117
- selector: null,
117
+ operation: null,
118
118
  source: source,
119
119
  updater: null
120
120
  }
@@ -185,7 +185,7 @@ function () {
185
185
  var _this = this;
186
186
 
187
187
  var fieldPayloads = payload.fieldPayloads,
188
- selector = payload.selector,
188
+ operation = payload.operation,
189
189
  source = payload.source,
190
190
  updater = payload.updater;
191
191
  var mutator = new (require("./RelayRecordSourceMutator"))(this._store.getSource(), source);
@@ -201,9 +201,10 @@ function () {
201
201
  }
202
202
 
203
203
  if (updater) {
204
+ var selector = operation === null || operation === void 0 ? void 0 : operation.fragment;
204
205
  !(selector != null) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'RelayModernEnvironment: Expected a selector to be provided with updater function.') : require("fbjs/lib/invariant")(false) : void 0;
205
206
  var selectorStore = new (require("./RelayRecordSourceSelectorProxy"))(store, selector);
206
- var selectorData = lookupSelector(source, selector);
207
+ var selectorData = lookupSelector(source, selector, operation);
207
208
  updater(selectorStore, selectorData);
208
209
  }
209
210
 
@@ -275,7 +276,7 @@ function () {
275
276
  var _normalizeRelayPayloa = require("./normalizeRelayPayload")(operation.root, response);
276
277
 
277
278
  source = _normalizeRelayPayloa.source;
278
- selectorData = lookupSelector(source, operation.fragment);
279
+ selectorData = lookupSelector(source, operation.fragment, operation);
279
280
  }
280
281
 
281
282
  selectorStoreUpdater && require("fbjs/lib/ErrorUtils").applyWithGuard(selectorStoreUpdater, null, [selectorStore, selectorData], null, 'RelayPublishQueue:applyUpdates');
@@ -306,7 +307,7 @@ function () {
306
307
  var _normalizeRelayPayloa2 = require("./normalizeRelayPayload")(operation.root, response);
307
308
 
308
309
  source = _normalizeRelayPayloa2.source;
309
- selectorData = lookupSelector(source, operation.fragment);
310
+ selectorData = lookupSelector(source, operation.fragment, operation);
310
311
  }
311
312
 
312
313
  selectorStoreUpdater && require("fbjs/lib/ErrorUtils").applyWithGuard(selectorStoreUpdater, null, [selectorStore, selectorData], null, 'RelayPublishQueue:applyUpdates');
@@ -333,8 +334,8 @@ function () {
333
334
  return RelayPublishQueue;
334
335
  }();
335
336
 
336
- function lookupSelector(source, selector) {
337
- var selectorData = require("./RelayReader").read(source, selector).data;
337
+ function lookupSelector(source, selector, owner) {
338
+ var selectorData = require("./RelayReader").read(source, selector, owner).data;
338
339
 
339
340
  if (process.env.NODE_ENV !== "production") {
340
341
  var deepFreeze = require("./deepFreeze");
@@ -45,7 +45,8 @@ function () {
45
45
  node: node,
46
46
  seenRecords: this._seenRecords,
47
47
  variables: this._variables,
48
- isMissingData: this._isMissingData
48
+ isMissingData: this._isMissingData,
49
+ owner: this._owner
49
50
  };
50
51
  };
51
52
 
@@ -279,8 +280,6 @@ function () {
279
280
  };
280
281
 
281
282
  _proto._createFragmentPointer = function _createFragmentPointer(fragmentSpread, record, data, variables) {
282
- var _data$ID_KEY;
283
-
284
283
  var fragmentPointers = data[require("./RelayStoreUtils").FRAGMENTS_KEY];
285
284
 
286
285
  if (fragmentPointers == null) {
@@ -288,7 +287,11 @@ function () {
288
287
  }
289
288
 
290
289
  !(typeof fragmentPointers === 'object' && fragmentPointers) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'RelayReader: Expected fragment spread data to be an object, got `%s`.', fragmentPointers) : require("fbjs/lib/invariant")(false) : void 0;
291
- data[require("./RelayStoreUtils").ID_KEY] = (_data$ID_KEY = data[require("./RelayStoreUtils").ID_KEY]) !== null && _data$ID_KEY !== void 0 ? _data$ID_KEY : require("./RelayModernRecord").getDataID(record);
290
+
291
+ if (data[require("./RelayStoreUtils").ID_KEY] == null) {
292
+ data[require("./RelayStoreUtils").ID_KEY] = require("./RelayModernRecord").getDataID(record);
293
+ }
294
+
292
295
  fragmentPointers[fragmentSpread.name] = fragmentSpread.args ? require("./RelayStoreUtils").getArgumentValues(fragmentSpread.args, variables) : {};
293
296
  data[require("./RelayStoreUtils").FRAGMENT_OWNER_KEY] = this._owner;
294
297
  };
@@ -112,6 +112,12 @@ function () {
112
112
 
113
113
  break;
114
114
 
115
+ case require("./RelayConcreteNode").DEFER:
116
+ case require("./RelayConcreteNode").STREAM:
117
+ _this._traverseSelections(selection.selections, record);
118
+
119
+ break;
120
+
115
121
  case require("./RelayConcreteNode").SCALAR_FIELD:
116
122
  case require("./RelayConcreteNode").SCALAR_HANDLE:
117
123
  break;