relay-runtime 9.0.0 → 10.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.
Files changed (142) hide show
  1. package/handlers/RelayDefaultHandlerProvider.js.flow +47 -0
  2. package/handlers/connection/ConnectionHandler.js.flow +549 -0
  3. package/handlers/connection/ConnectionInterface.js.flow +92 -0
  4. package/handlers/connection/MutationHandlers.js.flow +199 -0
  5. package/index.js +1 -1
  6. package/index.js.flow +335 -0
  7. package/lib/handlers/RelayDefaultHandlerProvider.js +20 -0
  8. package/lib/handlers/connection/ConnectionHandler.js +1 -3
  9. package/lib/handlers/connection/MutationHandlers.js +212 -0
  10. package/lib/index.js +14 -2
  11. package/lib/mutations/RelayDeclarativeMutationConfig.js +22 -45
  12. package/lib/mutations/RelayRecordProxy.js +1 -3
  13. package/lib/mutations/RelayRecordSourceMutator.js +1 -3
  14. package/lib/mutations/RelayRecordSourceProxy.js +1 -3
  15. package/lib/mutations/RelayRecordSourceSelectorProxy.js +1 -3
  16. package/lib/mutations/commitMutation.js +2 -3
  17. package/lib/mutations/validateMutation.js +40 -9
  18. package/lib/network/RelayObservable.js +9 -9
  19. package/lib/network/RelayQueryResponseCache.js +8 -6
  20. package/lib/query/GraphQLTag.js +2 -1
  21. package/lib/query/PreloadableQueryRegistry.js +70 -0
  22. package/lib/query/fetchQuery.js +2 -3
  23. package/lib/query/fetchQueryInternal.js +5 -14
  24. package/lib/store/DataChecker.js +200 -71
  25. package/lib/store/RelayConcreteVariables.js +6 -2
  26. package/lib/store/RelayModernEnvironment.js +124 -65
  27. package/lib/store/RelayModernFragmentSpecResolver.js +19 -14
  28. package/lib/store/RelayModernOperationDescriptor.js +6 -5
  29. package/lib/store/RelayModernQueryExecutor.js +122 -73
  30. package/lib/store/RelayModernRecord.js +14 -9
  31. package/lib/store/RelayModernSelector.js +6 -2
  32. package/lib/store/RelayModernStore.js +281 -131
  33. package/lib/store/RelayOperationTracker.js +35 -78
  34. package/lib/store/RelayOptimisticRecordSource.js +7 -5
  35. package/lib/store/RelayPublishQueue.js +2 -4
  36. package/lib/store/RelayReader.js +304 -52
  37. package/lib/store/RelayRecordSource.js +1 -3
  38. package/lib/store/RelayRecordSourceMapImpl.js +13 -18
  39. package/lib/store/RelayReferenceMarker.js +125 -14
  40. package/lib/store/RelayResponseNormalizer.js +261 -66
  41. package/lib/store/RelayStoreReactFlightUtils.js +47 -0
  42. package/lib/store/RelayStoreUtils.js +1 -0
  43. package/lib/store/StoreInspector.js +8 -8
  44. package/lib/store/TypeID.js +28 -0
  45. package/lib/store/cloneRelayScalarHandleSourceField.js +44 -0
  46. package/lib/store/defaultRequiredFieldLogger.js +18 -0
  47. package/lib/store/normalizeRelayPayload.js +6 -2
  48. package/lib/store/readInlineData.js +1 -1
  49. package/lib/subscription/requestSubscription.js +4 -3
  50. package/lib/util/NormalizationNode.js +1 -5
  51. package/lib/util/RelayConcreteNode.js +11 -6
  52. package/lib/util/RelayError.js +39 -9
  53. package/lib/util/RelayFeatureFlags.js +6 -3
  54. package/lib/util/RelayReplaySubject.js +3 -3
  55. package/lib/util/createPayloadFor3DField.js +7 -2
  56. package/lib/util/getFragmentIdentifier.js +12 -3
  57. package/lib/util/getOperation.js +33 -0
  58. package/lib/util/getRequestIdentifier.js +2 -2
  59. package/lib/util/isEmptyObject.js +25 -0
  60. package/lib/util/recycleNodesInto.js +6 -7
  61. package/lib/util/reportMissingRequiredFields.js +48 -0
  62. package/mutations/RelayDeclarativeMutationConfig.js.flow +380 -0
  63. package/mutations/RelayRecordProxy.js.flow +165 -0
  64. package/mutations/RelayRecordSourceMutator.js.flow +238 -0
  65. package/mutations/RelayRecordSourceProxy.js.flow +164 -0
  66. package/mutations/RelayRecordSourceSelectorProxy.js.flow +119 -0
  67. package/mutations/applyOptimisticMutation.js.flow +76 -0
  68. package/mutations/commitLocalUpdate.js.flow +24 -0
  69. package/mutations/commitMutation.js.flow +181 -0
  70. package/mutations/validateMutation.js.flow +242 -0
  71. package/network/ConvertToExecuteFunction.js.flow +49 -0
  72. package/network/RelayNetwork.js.flow +84 -0
  73. package/network/RelayNetworkTypes.js.flow +145 -0
  74. package/network/RelayObservable.js.flow +634 -0
  75. package/network/RelayQueryResponseCache.js.flow +111 -0
  76. package/package.json +2 -2
  77. package/query/GraphQLTag.js.flow +168 -0
  78. package/query/PreloadableQueryRegistry.js.flow +65 -0
  79. package/query/fetchQuery.js.flow +47 -0
  80. package/query/fetchQueryInternal.js.flow +343 -0
  81. package/relay-runtime.js +2 -2
  82. package/relay-runtime.min.js +2 -2
  83. package/store/ClientID.js.flow +43 -0
  84. package/store/DataChecker.js.flow +568 -0
  85. package/store/RelayConcreteVariables.js.flow +96 -0
  86. package/store/RelayModernEnvironment.js.flow +571 -0
  87. package/store/RelayModernFragmentSpecResolver.js.flow +438 -0
  88. package/store/RelayModernOperationDescriptor.js.flow +92 -0
  89. package/store/RelayModernQueryExecutor.js.flow +1345 -0
  90. package/store/RelayModernRecord.js.flow +403 -0
  91. package/store/RelayModernSelector.js.flow +455 -0
  92. package/store/RelayModernStore.js.flow +858 -0
  93. package/store/RelayOperationTracker.js.flow +164 -0
  94. package/store/RelayOptimisticRecordSource.js.flow +119 -0
  95. package/store/RelayPublishQueue.js.flow +401 -0
  96. package/store/RelayReader.js.flow +638 -0
  97. package/store/RelayRecordSource.js.flow +29 -0
  98. package/store/RelayRecordSourceMapImpl.js.flow +87 -0
  99. package/store/RelayRecordState.js.flow +37 -0
  100. package/store/RelayReferenceMarker.js.flow +324 -0
  101. package/store/RelayResponseNormalizer.js.flow +791 -0
  102. package/store/RelayStoreReactFlightUtils.js.flow +64 -0
  103. package/store/RelayStoreTypes.js.flow +958 -0
  104. package/store/RelayStoreUtils.js.flow +219 -0
  105. package/store/StoreInspector.js.flow +171 -0
  106. package/store/TypeID.js.flow +28 -0
  107. package/store/ViewerPattern.js.flow +26 -0
  108. package/store/cloneRelayHandleSourceField.js.flow +66 -0
  109. package/store/cloneRelayScalarHandleSourceField.js.flow +62 -0
  110. package/store/createFragmentSpecResolver.js.flow +55 -0
  111. package/store/createRelayContext.js.flow +44 -0
  112. package/store/defaultGetDataID.js.flow +27 -0
  113. package/store/defaultRequiredFieldLogger.js.flow +23 -0
  114. package/store/hasOverlappingIDs.js.flow +34 -0
  115. package/store/isRelayModernEnvironment.js.flow +27 -0
  116. package/store/normalizeRelayPayload.js.flow +51 -0
  117. package/store/readInlineData.js.flow +75 -0
  118. package/subscription/requestSubscription.js.flow +103 -0
  119. package/util/JSResourceTypes.flow.js.flow +20 -0
  120. package/util/NormalizationNode.js.flow +213 -0
  121. package/util/ReaderNode.js.flow +227 -0
  122. package/util/RelayConcreteNode.js.flow +99 -0
  123. package/util/RelayDefaultHandleKey.js.flow +17 -0
  124. package/util/RelayError.js.flow +62 -0
  125. package/util/RelayFeatureFlags.js.flow +37 -0
  126. package/util/RelayProfiler.js.flow +284 -0
  127. package/util/RelayReplaySubject.js.flow +135 -0
  128. package/util/RelayRuntimeTypes.js.flow +72 -0
  129. package/util/createPayloadFor3DField.js.flow +43 -0
  130. package/util/deepFreeze.js.flow +36 -0
  131. package/util/generateID.js.flow +21 -0
  132. package/util/getFragmentIdentifier.js.flow +76 -0
  133. package/util/getOperation.js.flow +40 -0
  134. package/util/getRelayHandleKey.js.flow +41 -0
  135. package/util/getRequestIdentifier.js.flow +42 -0
  136. package/util/isEmptyObject.js.flow +25 -0
  137. package/util/isPromise.js.flow +21 -0
  138. package/util/isScalarAndEqual.js.flow +26 -0
  139. package/util/recycleNodesInto.js.flow +87 -0
  140. package/util/reportMissingRequiredFields.js.flow +51 -0
  141. package/util/resolveImmediate.js.flow +30 -0
  142. package/util/stableCopy.js.flow +35 -0
@@ -0,0 +1,284 @@
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
+ * @format
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ type Handler = (name: string, callback: () => void) => void;
16
+ type ProfileHandler = (name: string, state?: any) => (error?: Error) => void;
17
+
18
+ function emptyFunction() {}
19
+
20
+ const aggregateHandlersByName: {[name: string]: Array<Handler>, ...} = {
21
+ '*': [],
22
+ };
23
+ const profileHandlersByName: {[name: string]: Array<ProfileHandler>, ...} = {
24
+ '*': [],
25
+ };
26
+
27
+ const NOT_INVOKED = {};
28
+ const defaultProfiler = {stop: emptyFunction};
29
+ const shouldInstrument = name => {
30
+ if (__DEV__) {
31
+ return true;
32
+ }
33
+ return name.charAt(0) !== '@';
34
+ };
35
+
36
+ /**
37
+ * @public
38
+ *
39
+ * Instruments methods to allow profiling various parts of Relay. Profiling code
40
+ * in Relay consists of three steps:
41
+ *
42
+ * - Instrument the function to be profiled.
43
+ * - Attach handlers to the instrumented function.
44
+ * - Run the code which triggers the handlers.
45
+ *
46
+ * Handlers attached to instrumented methods are called with an instrumentation
47
+ * name and a callback that must be synchronously executed:
48
+ *
49
+ * instrumentedMethod.attachHandler(function(name, callback) {
50
+ * const start = performance.now();
51
+ * callback();
52
+ * console.log('Duration', performance.now() - start);
53
+ * });
54
+ *
55
+ * Handlers for profiles are callbacks that return a stop method:
56
+ *
57
+ * RelayProfiler.attachProfileHandler('profileName', (name, state) => {
58
+ * const start = performance.now();
59
+ * return function stop(name, state) {
60
+ * console.log(`Duration (${name})`, performance.now() - start);
61
+ * }
62
+ * });
63
+ *
64
+ * In order to reduce the impact on performance in production, instrumented
65
+ * methods and profilers with names that begin with `@` will only be measured
66
+ * if `__DEV__` is true. This should be used for very hot functions.
67
+ */
68
+ const RelayProfiler = {
69
+ /**
70
+ * Instruments methods on a class or object. This re-assigns the method in
71
+ * order to preserve function names in stack traces (which are detected by
72
+ * modern debuggers via heuristics). Example usage:
73
+ *
74
+ * const RelayStore = { primeCache: function() {...} };
75
+ * RelayProfiler.instrumentMethods(RelayStore, {
76
+ * primeCache: 'RelayStore.primeCache'
77
+ * });
78
+ *
79
+ * RelayStore.primeCache.attachHandler(...);
80
+ *
81
+ * As a result, the methods will be replaced by wrappers that provide the
82
+ * `attachHandler` and `detachHandler` methods.
83
+ */
84
+ instrumentMethods(
85
+ object: Function | Object,
86
+ names: {[key: string]: string, ...},
87
+ ): void {
88
+ for (const key in names) {
89
+ if (names.hasOwnProperty(key)) {
90
+ if (typeof object[key] === 'function') {
91
+ object[key] = RelayProfiler.instrument(names[key], object[key]);
92
+ }
93
+ }
94
+ }
95
+ },
96
+
97
+ /**
98
+ * Wraps the supplied function with one that provides the `attachHandler` and
99
+ * `detachHandler` methods. Example usage:
100
+ *
101
+ * const printRelayQuery =
102
+ * RelayProfiler.instrument('printRelayQuery', printRelayQuery);
103
+ *
104
+ * printRelayQuery.attachHandler(...);
105
+ *
106
+ * NOTE: The instrumentation assumes that no handlers are attached or detached
107
+ * in the course of executing another handler.
108
+ */
109
+ instrument<T: Function>(name: string, originalFunction: T): T {
110
+ if (!shouldInstrument(name)) {
111
+ originalFunction.attachHandler = emptyFunction;
112
+ originalFunction.detachHandler = emptyFunction;
113
+ return originalFunction;
114
+ }
115
+ if (!aggregateHandlersByName.hasOwnProperty(name)) {
116
+ aggregateHandlersByName[name] = [];
117
+ }
118
+ const catchallHandlers = aggregateHandlersByName['*'];
119
+ const aggregateHandlers = aggregateHandlersByName[name];
120
+ const handlers: Array<Handler> = [];
121
+ const contexts: Array<[number, number, number, any, any, any]> = [];
122
+ const invokeHandlers = function() {
123
+ const context = contexts[contexts.length - 1];
124
+ if (context[0]) {
125
+ context[0]--;
126
+ catchallHandlers[context[0]](name, invokeHandlers);
127
+ } else if (context[1]) {
128
+ context[1]--;
129
+ aggregateHandlers[context[1]](name, invokeHandlers);
130
+ } else if (context[2]) {
131
+ context[2]--;
132
+ handlers[context[2]](name, invokeHandlers);
133
+ } else {
134
+ context[5] = originalFunction.apply(context[3], context[4]);
135
+ }
136
+ };
137
+ const instrumentedCallback = function() {
138
+ let returnValue;
139
+ if (
140
+ aggregateHandlers.length === 0 &&
141
+ handlers.length === 0 &&
142
+ catchallHandlers.length === 0
143
+ ) {
144
+ returnValue = originalFunction.apply(this, arguments);
145
+ } else {
146
+ contexts.push([
147
+ catchallHandlers.length,
148
+ aggregateHandlers.length,
149
+ handlers.length,
150
+ this,
151
+ arguments,
152
+ NOT_INVOKED,
153
+ ]);
154
+ invokeHandlers();
155
+ const context = contexts.pop();
156
+ returnValue = context[5];
157
+ if (returnValue === NOT_INVOKED) {
158
+ throw new Error(
159
+ 'RelayProfiler: Handler did not invoke original function.',
160
+ );
161
+ }
162
+ }
163
+ return returnValue;
164
+ };
165
+ instrumentedCallback.attachHandler = function(handler: Handler): void {
166
+ handlers.push(handler);
167
+ };
168
+ instrumentedCallback.detachHandler = function(handler: Handler): void {
169
+ removeFromArray(handlers, handler);
170
+ };
171
+ instrumentedCallback.displayName = '(instrumented ' + name + ')';
172
+ return (instrumentedCallback: any);
173
+ },
174
+
175
+ /**
176
+ * Attaches a handler to all methods instrumented with the supplied name.
177
+ *
178
+ * function createRenderer() {
179
+ * return RelayProfiler.instrument('render', function() {...});
180
+ * }
181
+ * const renderA = createRenderer();
182
+ * const renderB = createRenderer();
183
+ *
184
+ * // Only profiles `renderA`.
185
+ * renderA.attachHandler(...);
186
+ *
187
+ * // Profiles both `renderA` and `renderB`.
188
+ * RelayProfiler.attachAggregateHandler('render', ...);
189
+ *
190
+ */
191
+ attachAggregateHandler(name: string, handler: Handler): void {
192
+ if (shouldInstrument(name)) {
193
+ if (!aggregateHandlersByName.hasOwnProperty(name)) {
194
+ aggregateHandlersByName[name] = [];
195
+ }
196
+ aggregateHandlersByName[name].push(handler);
197
+ }
198
+ },
199
+
200
+ /**
201
+ * Detaches a handler attached via `attachAggregateHandler`.
202
+ */
203
+ detachAggregateHandler(name: string, handler: Handler): void {
204
+ if (shouldInstrument(name)) {
205
+ if (aggregateHandlersByName.hasOwnProperty(name)) {
206
+ removeFromArray(aggregateHandlersByName[name], handler);
207
+ }
208
+ }
209
+ },
210
+
211
+ /**
212
+ * Instruments profiling for arbitrarily asynchronous code by a name.
213
+ *
214
+ * const timerProfiler = RelayProfiler.profile('timeout');
215
+ * setTimeout(function() {
216
+ * timerProfiler.stop();
217
+ * }, 1000);
218
+ *
219
+ * RelayProfiler.attachProfileHandler('timeout', ...);
220
+ *
221
+ * Arbitrary state can also be passed into `profile` as a second argument. The
222
+ * attached profile handlers will receive this as the second argument.
223
+ */
224
+ profile(name: string, state?: any): {stop: (error?: Error) => void, ...} {
225
+ const hasCatchAllHandlers = profileHandlersByName['*'].length > 0;
226
+ const hasNamedHandlers = profileHandlersByName.hasOwnProperty(name);
227
+ if (hasNamedHandlers || hasCatchAllHandlers) {
228
+ const profileHandlers =
229
+ hasNamedHandlers && hasCatchAllHandlers
230
+ ? profileHandlersByName[name].concat(profileHandlersByName['*'])
231
+ : hasNamedHandlers
232
+ ? profileHandlersByName[name]
233
+ : profileHandlersByName['*'];
234
+ let stopHandlers;
235
+ for (let ii = profileHandlers.length - 1; ii >= 0; ii--) {
236
+ const profileHandler = profileHandlers[ii];
237
+ const stopHandler = profileHandler(name, state);
238
+ stopHandlers = stopHandlers || [];
239
+ stopHandlers.unshift(stopHandler);
240
+ }
241
+ return {
242
+ stop(error?: Error): void {
243
+ if (stopHandlers) {
244
+ stopHandlers.forEach(stopHandler => stopHandler(error));
245
+ }
246
+ },
247
+ };
248
+ }
249
+ return defaultProfiler;
250
+ },
251
+
252
+ /**
253
+ * Attaches a handler to profiles with the supplied name. You can also
254
+ * attach to the special name '*' which is a catch all.
255
+ */
256
+ attachProfileHandler(name: string, handler: ProfileHandler): void {
257
+ if (shouldInstrument(name)) {
258
+ if (!profileHandlersByName.hasOwnProperty(name)) {
259
+ profileHandlersByName[name] = [];
260
+ }
261
+ profileHandlersByName[name].push(handler);
262
+ }
263
+ },
264
+
265
+ /**
266
+ * Detaches a handler attached via `attachProfileHandler`.
267
+ */
268
+ detachProfileHandler(name: string, handler: ProfileHandler): void {
269
+ if (shouldInstrument(name)) {
270
+ if (profileHandlersByName.hasOwnProperty(name)) {
271
+ removeFromArray(profileHandlersByName[name], handler);
272
+ }
273
+ }
274
+ },
275
+ };
276
+
277
+ function removeFromArray<T>(array: Array<T>, element: T): void {
278
+ var index = array.indexOf(element);
279
+ if (index !== -1) {
280
+ array.splice(index, 1);
281
+ }
282
+ }
283
+
284
+ module.exports = RelayProfiler;
@@ -0,0 +1,135 @@
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 strict-local
8
+ * @format
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ const RelayObservable = require('../network/RelayObservable');
16
+
17
+ const invariant = require('invariant');
18
+
19
+ import type {Observer, Sink, Subscription} from '../network/RelayObservable';
20
+
21
+ type Event<T> =
22
+ | {
23
+ kind: 'next',
24
+ data: T,
25
+ ...
26
+ }
27
+ | {
28
+ kind: 'error',
29
+ error: Error,
30
+ ...
31
+ }
32
+ | {kind: 'complete', ...};
33
+
34
+ /**
35
+ * An implementation of a `ReplaySubject` for Relay Observables.
36
+ *
37
+ * Records events provided and synchronously plays them back to new subscribers,
38
+ * as well as forwarding new asynchronous events.
39
+ */
40
+ class RelayReplaySubject<T> {
41
+ _complete: boolean = false;
42
+ _events: Array<Event<T>> = [];
43
+ _sinks: Set<Sink<T>> = new Set();
44
+ _observable: RelayObservable<T>;
45
+ _subscription: ?Subscription = null;
46
+
47
+ constructor() {
48
+ this._observable = RelayObservable.create(sink => {
49
+ this._sinks.add(sink);
50
+
51
+ const events = this._events;
52
+ for (let i = 0; i < events.length; i++) {
53
+ if (sink.closed) {
54
+ // Bail if an event made the observer unsubscribe.
55
+ break;
56
+ }
57
+ const event = events[i];
58
+ switch (event.kind) {
59
+ case 'complete':
60
+ sink.complete();
61
+ break;
62
+ case 'error':
63
+ sink.error(event.error);
64
+ break;
65
+ case 'next':
66
+ sink.next(event.data);
67
+ break;
68
+ default:
69
+ (event.kind: empty);
70
+ invariant(
71
+ false,
72
+ 'RelayReplaySubject: Unknown event kind `%s`.',
73
+ event.kind,
74
+ );
75
+ }
76
+ }
77
+
78
+ return () => {
79
+ this._sinks.delete(sink);
80
+ };
81
+ });
82
+ }
83
+
84
+ complete(): void {
85
+ if (this._complete === true) {
86
+ return;
87
+ }
88
+ this._complete = true;
89
+ this._events.push({
90
+ kind: 'complete',
91
+ });
92
+ this._sinks.forEach(sink => sink.complete());
93
+ }
94
+
95
+ error(error: Error): void {
96
+ if (this._complete === true) {
97
+ return;
98
+ }
99
+ this._complete = true;
100
+ this._events.push({
101
+ kind: 'error',
102
+ error,
103
+ });
104
+ this._sinks.forEach(sink => sink.error(error));
105
+ }
106
+
107
+ next(data: T): void {
108
+ if (this._complete === true) {
109
+ return;
110
+ }
111
+ this._events.push({
112
+ kind: 'next',
113
+ data,
114
+ });
115
+ this._sinks.forEach(sink => sink.next(data));
116
+ }
117
+
118
+ subscribe(observer: Observer<T> | Sink<T>): Subscription {
119
+ this._subscription = this._observable.subscribe(observer);
120
+ return this._subscription;
121
+ }
122
+
123
+ unsubscribe() {
124
+ if (this._subscription) {
125
+ this._subscription.unsubscribe();
126
+ this._subscription = null;
127
+ }
128
+ }
129
+
130
+ getObserverCount(): number {
131
+ return this._sinks.size;
132
+ }
133
+ }
134
+
135
+ module.exports = RelayReplaySubject;
@@ -0,0 +1,72 @@
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 strict
8
+ * @format
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ /**
16
+ * Basic types used throughout Relay.
17
+ */
18
+
19
+ /**
20
+ * Represents any resource that must be explicitly disposed of. The most common
21
+ * use-case is as a return value for subscriptions, where calling `dispose()`
22
+ * would cancel the subscription.
23
+ */
24
+ export type Disposable = {dispose(): void, ...};
25
+
26
+ export type DataID = string;
27
+
28
+ // Variables
29
+ export type Variables = {+[string]: $FlowFixMe, ...};
30
+
31
+ /**
32
+ * Generated operation flow types are subtypes of this.
33
+ */
34
+ export type OperationType = {|
35
+ // TODO(T33395812) Make this an open object type
36
+ +variables: Variables,
37
+ +response: mixed,
38
+ +rawResponse?: {...},
39
+ |};
40
+
41
+ export type VariablesOf<T: OperationType> = $ElementType<T, 'variables'>;
42
+
43
+ /**
44
+ * Settings for how a query response may be cached.
45
+ *
46
+ * - `force`: causes a query to be issued unconditionally, irrespective of the
47
+ * state of any configured response cache.
48
+ * - `poll`: causes a query to live update by polling at the specified interval
49
+ * in milliseconds. (This value will be passed to setTimeout.)
50
+ * - `liveConfigId`: causes a query to live update by calling GraphQLLiveQuery,
51
+ * it represents a configuration of gateway when doing live query
52
+ * - `metadata`: user-supplied metadata.
53
+ * - `transactionId`: a user-supplied value, intended for use as a unique id for
54
+ * a given instance of executing an operation.
55
+ */
56
+ export type CacheConfig = {|
57
+ force?: ?boolean,
58
+ poll?: ?number,
59
+ liveConfigId?: ?string,
60
+ metadata?: {[key: string]: mixed, ...},
61
+ transactionId?: ?string,
62
+ |};
63
+
64
+ /**
65
+ * Experimental
66
+ */
67
+ export type FetchQueryFetchPolicy = 'store-or-network' | 'network-only';
68
+ export type FetchPolicy =
69
+ | FetchQueryFetchPolicy
70
+ | 'store-and-network'
71
+ | 'store-only';
72
+ export type RenderPolicy = 'full' | 'partial';
@@ -0,0 +1,43 @@
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
+ * @emails oncall+relay
8
+ * @flow strict-local
9
+ * @format
10
+ */
11
+
12
+ // flowlint ambiguous-object-type:error
13
+
14
+ 'use strict';
15
+
16
+ const {
17
+ getModuleComponentKey,
18
+ getModuleOperationKey,
19
+ } = require('../store/RelayStoreUtils');
20
+
21
+ import type {NormalizationSplitOperation} from './NormalizationNode';
22
+ import type {JSResourceReference} from 'JSResourceReference';
23
+
24
+ export opaque type Local3DPayload<
25
+ +DocumentName: string,
26
+ +Response: {...},
27
+ > = Response;
28
+
29
+ function createPayloadFor3DField<+DocumentName: string, +Response: {...}>(
30
+ name: DocumentName,
31
+ operation: JSResourceReference<NormalizationSplitOperation>,
32
+ component: JSResourceReference<mixed>,
33
+ response: Response,
34
+ ): Local3DPayload<DocumentName, Response> {
35
+ const data = {
36
+ ...response,
37
+ };
38
+ data[getModuleComponentKey(name)] = component;
39
+ data[getModuleOperationKey(name)] = operation;
40
+ return data;
41
+ }
42
+
43
+ module.exports = createPayloadFor3DField;
@@ -0,0 +1,36 @@
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 strict
8
+ * @format
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ /**
16
+ * Recursively "deep" freezes the supplied object.
17
+ *
18
+ * For convenience, and for consistency with the behavior of `Object.freeze`,
19
+ * returns the now-frozen original object.
20
+ */
21
+ function deepFreeze<T: {...}>(object: T): T {
22
+ Object.freeze(object);
23
+ Object.getOwnPropertyNames(object).forEach(name => {
24
+ const property = object[name];
25
+ if (
26
+ property &&
27
+ typeof property === 'object' &&
28
+ !Object.isFrozen(property)
29
+ ) {
30
+ deepFreeze(property);
31
+ }
32
+ });
33
+ return object;
34
+ }
35
+
36
+ module.exports = deepFreeze;
@@ -0,0 +1,21 @@
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 strict
8
+ * @format
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ let id = 100000;
16
+
17
+ function generateID(): number {
18
+ return id++;
19
+ }
20
+
21
+ module.exports = generateID;
@@ -0,0 +1,76 @@
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 strict-local
8
+ * @format
9
+ * @emails oncall+relay
10
+ */
11
+
12
+ // flowlint ambiguous-object-type:error
13
+
14
+ 'use strict';
15
+
16
+ const RelayFeatureFlags = require('./RelayFeatureFlags');
17
+
18
+ const isEmptyObject = require('./isEmptyObject');
19
+ const stableCopy = require('./stableCopy');
20
+
21
+ const {
22
+ getDataIDsFromFragment,
23
+ getVariablesFromFragment,
24
+ getSelector,
25
+ } = require('../store/RelayModernSelector');
26
+
27
+ import type {ReaderFragment} from './ReaderNode';
28
+
29
+ function getFragmentIdentifier(
30
+ fragmentNode: ReaderFragment,
31
+ fragmentRef: mixed,
32
+ ): string {
33
+ const selector = getSelector(fragmentNode, fragmentRef);
34
+ const fragmentOwnerIdentifier =
35
+ selector == null
36
+ ? 'null'
37
+ : selector.kind === 'SingularReaderSelector'
38
+ ? selector.owner.identifier
39
+ : '[' +
40
+ selector.selectors.map(sel => sel.owner.identifier).join(',') +
41
+ ']';
42
+ const fragmentVariables = getVariablesFromFragment(fragmentNode, fragmentRef);
43
+ const dataIDs = getDataIDsFromFragment(fragmentNode, fragmentRef);
44
+
45
+ if (RelayFeatureFlags.ENABLE_GETFRAGMENTIDENTIFIER_OPTIMIZATION) {
46
+ return (
47
+ fragmentOwnerIdentifier +
48
+ '/' +
49
+ fragmentNode.name +
50
+ '/' +
51
+ (fragmentVariables == null || isEmptyObject(fragmentVariables)
52
+ ? '{}'
53
+ : JSON.stringify(stableCopy(fragmentVariables))) +
54
+ '/' +
55
+ (typeof dataIDs === 'undefined'
56
+ ? 'missing'
57
+ : dataIDs == null
58
+ ? 'null'
59
+ : Array.isArray(dataIDs)
60
+ ? '[' + dataIDs.join(',') + ']'
61
+ : dataIDs)
62
+ );
63
+ } else {
64
+ return (
65
+ fragmentOwnerIdentifier +
66
+ '/' +
67
+ fragmentNode.name +
68
+ '/' +
69
+ JSON.stringify(stableCopy(fragmentVariables)) +
70
+ '/' +
71
+ (JSON.stringify(dataIDs) ?? 'missing')
72
+ );
73
+ }
74
+ }
75
+
76
+ module.exports = getFragmentIdentifier;
@@ -0,0 +1,40 @@
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 strict-local
8
+ * @format
9
+ * @emails oncall+relay
10
+ */
11
+
12
+ // flowlint ambiguous-object-type:error
13
+
14
+ 'use strict';
15
+
16
+ const {REQUEST, SPLIT_OPERATION} = require('./RelayConcreteNode');
17
+
18
+ import type {
19
+ NormalizationOperation,
20
+ NormalizationRootNode,
21
+ NormalizationSplitOperation,
22
+ } from './NormalizationNode';
23
+
24
+ /**
25
+ * OperationLoaders can return either a NormalizationSplitOperation or
26
+ * ConcreteRequest.
27
+ */
28
+ function getOperation(
29
+ node: NormalizationRootNode,
30
+ ): NormalizationSplitOperation | NormalizationOperation {
31
+ switch (node.kind) {
32
+ case REQUEST:
33
+ return node.operation;
34
+ case SPLIT_OPERATION:
35
+ default:
36
+ return node;
37
+ }
38
+ }
39
+
40
+ module.exports = getOperation;