relay-runtime 11.0.2 → 13.0.0-rc.2

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 (219) hide show
  1. package/README.md +67 -0
  2. package/handlers/RelayDefaultHandlerProvider.js.flow +3 -3
  3. package/handlers/connection/ConnectionHandler.js.flow +9 -18
  4. package/handlers/connection/ConnectionInterface.js.flow +1 -1
  5. package/handlers/connection/MutationHandlers.js.flow +8 -12
  6. package/index.js +1 -1
  7. package/index.js.flow +57 -35
  8. package/lib/handlers/RelayDefaultHandlerProvider.js +1 -1
  9. package/lib/handlers/connection/ConnectionHandler.js +13 -19
  10. package/lib/handlers/connection/ConnectionInterface.js +1 -1
  11. package/lib/handlers/connection/MutationHandlers.js +4 -7
  12. package/lib/index.js +59 -44
  13. package/lib/multi-actor-environment/ActorIdentifier.js +12 -2
  14. package/lib/multi-actor-environment/ActorSpecificEnvironment.js +64 -20
  15. package/lib/multi-actor-environment/ActorUtils.js +27 -0
  16. package/lib/multi-actor-environment/MultiActorEnvironment.js +324 -61
  17. package/lib/multi-actor-environment/MultiActorEnvironmentTypes.js +1 -1
  18. package/lib/multi-actor-environment/index.js +6 -2
  19. package/lib/mutations/RelayDeclarativeMutationConfig.js +5 -2
  20. package/lib/mutations/RelayRecordProxy.js +4 -3
  21. package/lib/mutations/RelayRecordSourceMutator.js +4 -3
  22. package/lib/mutations/RelayRecordSourceProxy.js +13 -5
  23. package/lib/mutations/RelayRecordSourceSelectorProxy.js +19 -6
  24. package/lib/mutations/applyOptimisticMutation.js +7 -7
  25. package/lib/mutations/commitLocalUpdate.js +1 -1
  26. package/lib/mutations/commitMutation.js +15 -11
  27. package/lib/mutations/readUpdatableQuery_EXPERIMENTAL.js +242 -0
  28. package/lib/mutations/validateMutation.js +11 -6
  29. package/lib/network/ConvertToExecuteFunction.js +3 -2
  30. package/lib/network/RelayNetwork.js +4 -3
  31. package/lib/network/RelayNetworkTypes.js +1 -1
  32. package/lib/network/RelayObservable.js +1 -1
  33. package/lib/network/RelayQueryResponseCache.js +22 -6
  34. package/lib/network/wrapNetworkWithLogObserver.js +79 -0
  35. package/lib/query/GraphQLTag.js +3 -2
  36. package/lib/query/PreloadableQueryRegistry.js +1 -1
  37. package/lib/query/fetchQuery.js +7 -6
  38. package/lib/query/fetchQueryInternal.js +1 -1
  39. package/lib/query/fetchQuery_DEPRECATED.js +3 -2
  40. package/lib/store/ClientID.js +8 -2
  41. package/lib/store/DataChecker.js +124 -55
  42. package/lib/store/OperationExecutor.js +489 -215
  43. package/lib/store/RelayConcreteVariables.js +27 -9
  44. package/lib/store/RelayExperimentalGraphResponseHandler.js +153 -0
  45. package/lib/store/RelayExperimentalGraphResponseTransform.js +391 -0
  46. package/lib/store/RelayModernEnvironment.js +100 -120
  47. package/lib/store/RelayModernFragmentSpecResolver.js +53 -27
  48. package/lib/store/RelayModernOperationDescriptor.js +3 -2
  49. package/lib/store/RelayModernRecord.js +48 -13
  50. package/lib/store/RelayModernSelector.js +15 -9
  51. package/lib/store/RelayModernStore.js +56 -23
  52. package/lib/store/RelayOperationTracker.js +34 -24
  53. package/lib/store/RelayOptimisticRecordSource.js +1 -1
  54. package/lib/store/RelayPublishQueue.js +35 -11
  55. package/lib/store/RelayReader.js +257 -72
  56. package/lib/store/RelayRecordSource.js +88 -4
  57. package/lib/store/RelayRecordState.js +1 -1
  58. package/lib/store/RelayReferenceMarker.js +34 -22
  59. package/lib/store/RelayResponseNormalizer.js +172 -96
  60. package/lib/store/RelayStoreReactFlightUtils.js +5 -11
  61. package/lib/store/RelayStoreSubscriptions.js +15 -10
  62. package/lib/store/RelayStoreTypes.js +1 -1
  63. package/lib/store/RelayStoreUtils.js +13 -8
  64. package/lib/store/ResolverCache.js +213 -0
  65. package/lib/store/ResolverFragments.js +10 -6
  66. package/lib/store/StoreInspector.js +1 -1
  67. package/lib/store/TypeID.js +1 -1
  68. package/lib/store/ViewerPattern.js +1 -1
  69. package/lib/store/cloneRelayHandleSourceField.js +6 -5
  70. package/lib/store/cloneRelayScalarHandleSourceField.js +6 -5
  71. package/lib/store/createFragmentSpecResolver.js +1 -1
  72. package/lib/store/createRelayContext.js +5 -3
  73. package/lib/store/defaultGetDataID.js +1 -1
  74. package/lib/store/defaultRequiredFieldLogger.js +1 -1
  75. package/lib/store/hasOverlappingIDs.js +1 -1
  76. package/lib/store/isRelayModernEnvironment.js +1 -1
  77. package/lib/store/normalizeRelayPayload.js +1 -1
  78. package/lib/store/readInlineData.js +7 -3
  79. package/lib/subscription/requestSubscription.js +32 -34
  80. package/lib/util/JSResourceTypes.flow.js +1 -1
  81. package/lib/util/NormalizationNode.js +1 -1
  82. package/lib/util/ReaderNode.js +1 -1
  83. package/lib/util/RelayConcreteNode.js +3 -1
  84. package/lib/util/RelayDefaultHandleKey.js +1 -1
  85. package/lib/util/RelayError.js +1 -1
  86. package/lib/util/RelayFeatureFlags.js +10 -7
  87. package/lib/util/RelayProfiler.js +1 -1
  88. package/lib/util/RelayReplaySubject.js +22 -7
  89. package/lib/util/RelayRuntimeTypes.js +1 -7
  90. package/lib/util/StringInterner.js +71 -0
  91. package/lib/util/createPayloadFor3DField.js +1 -1
  92. package/lib/util/deepFreeze.js +1 -1
  93. package/lib/util/generateID.js +1 -1
  94. package/lib/util/getAllRootVariables.js +29 -0
  95. package/lib/util/getFragmentIdentifier.js +16 -8
  96. package/lib/util/getOperation.js +3 -2
  97. package/lib/util/getPaginationMetadata.js +41 -0
  98. package/lib/util/getPaginationVariables.js +66 -0
  99. package/lib/util/getPendingOperationsForFragment.js +55 -0
  100. package/lib/util/getRefetchMetadata.js +36 -0
  101. package/lib/util/getRelayHandleKey.js +3 -3
  102. package/lib/util/getRequestIdentifier.js +3 -3
  103. package/lib/util/getValueAtPath.js +51 -0
  104. package/lib/util/isEmptyObject.js +2 -2
  105. package/lib/util/isPromise.js +1 -1
  106. package/lib/util/isScalarAndEqual.js +1 -1
  107. package/lib/util/recycleNodesInto.js +1 -1
  108. package/lib/util/registerEnvironmentWithDevTools.js +26 -0
  109. package/lib/util/reportMissingRequiredFields.js +1 -1
  110. package/lib/util/resolveImmediate.js +1 -1
  111. package/lib/util/stableCopy.js +1 -1
  112. package/lib/util/withDuration.js +31 -0
  113. package/multi-actor-environment/ActorIdentifier.js.flow +18 -2
  114. package/multi-actor-environment/ActorSpecificEnvironment.js.flow +94 -58
  115. package/multi-actor-environment/ActorUtils.js.flow +33 -0
  116. package/multi-actor-environment/MultiActorEnvironment.js.flow +366 -93
  117. package/multi-actor-environment/MultiActorEnvironmentTypes.js.flow +88 -23
  118. package/multi-actor-environment/index.js.flow +3 -1
  119. package/mutations/RelayDeclarativeMutationConfig.js.flow +33 -27
  120. package/mutations/RelayRecordProxy.js.flow +5 -6
  121. package/mutations/RelayRecordSourceMutator.js.flow +5 -7
  122. package/mutations/RelayRecordSourceProxy.js.flow +20 -11
  123. package/mutations/RelayRecordSourceSelectorProxy.js.flow +23 -8
  124. package/mutations/applyOptimisticMutation.js.flow +14 -15
  125. package/mutations/commitLocalUpdate.js.flow +2 -2
  126. package/mutations/commitMutation.js.flow +36 -47
  127. package/mutations/readUpdatableQuery_EXPERIMENTAL.js.flow +318 -0
  128. package/mutations/validateMutation.js.flow +27 -17
  129. package/network/ConvertToExecuteFunction.js.flow +3 -3
  130. package/network/RelayNetwork.js.flow +5 -6
  131. package/network/RelayNetworkTypes.js.flow +1 -1
  132. package/network/RelayObservable.js.flow +2 -2
  133. package/network/RelayQueryResponseCache.js.flow +35 -22
  134. package/network/wrapNetworkWithLogObserver.js.flow +99 -0
  135. package/package.json +2 -2
  136. package/query/GraphQLTag.js.flow +11 -11
  137. package/query/PreloadableQueryRegistry.js.flow +5 -3
  138. package/query/fetchQuery.js.flow +19 -19
  139. package/query/fetchQueryInternal.js.flow +7 -10
  140. package/query/fetchQuery_DEPRECATED.js.flow +7 -7
  141. package/relay-runtime.js +2 -2
  142. package/relay-runtime.min.js +2 -2
  143. package/store/ClientID.js.flow +15 -4
  144. package/store/DataChecker.js.flow +142 -60
  145. package/store/OperationExecutor.js.flow +575 -320
  146. package/store/RelayConcreteVariables.js.flow +28 -9
  147. package/store/RelayExperimentalGraphResponseHandler.js.flow +121 -0
  148. package/store/RelayExperimentalGraphResponseTransform.js.flow +470 -0
  149. package/store/RelayModernEnvironment.js.flow +91 -115
  150. package/store/RelayModernFragmentSpecResolver.js.flow +56 -32
  151. package/store/RelayModernOperationDescriptor.js.flow +13 -8
  152. package/store/RelayModernRecord.js.flow +68 -12
  153. package/store/RelayModernSelector.js.flow +25 -15
  154. package/store/RelayModernStore.js.flow +67 -32
  155. package/store/RelayOperationTracker.js.flow +60 -44
  156. package/store/RelayOptimisticRecordSource.js.flow +3 -3
  157. package/store/RelayPublishQueue.js.flow +74 -32
  158. package/store/RelayReader.js.flow +319 -100
  159. package/store/RelayRecordSource.js.flow +73 -7
  160. package/store/RelayRecordState.js.flow +1 -1
  161. package/store/RelayReferenceMarker.js.flow +41 -27
  162. package/store/RelayResponseNormalizer.js.flow +204 -86
  163. package/store/RelayStoreReactFlightUtils.js.flow +5 -12
  164. package/store/RelayStoreSubscriptions.js.flow +20 -12
  165. package/store/RelayStoreTypes.js.flow +200 -41
  166. package/store/RelayStoreUtils.js.flow +25 -12
  167. package/store/ResolverCache.js.flow +249 -0
  168. package/store/ResolverFragments.js.flow +16 -20
  169. package/store/StoreInspector.js.flow +3 -3
  170. package/store/TypeID.js.flow +2 -2
  171. package/store/ViewerPattern.js.flow +3 -3
  172. package/store/cloneRelayHandleSourceField.js.flow +6 -7
  173. package/store/cloneRelayScalarHandleSourceField.js.flow +6 -7
  174. package/store/createFragmentSpecResolver.js.flow +4 -5
  175. package/store/createRelayContext.js.flow +4 -4
  176. package/store/defaultGetDataID.js.flow +1 -1
  177. package/store/defaultRequiredFieldLogger.js.flow +1 -1
  178. package/store/hasOverlappingIDs.js.flow +1 -1
  179. package/store/isRelayModernEnvironment.js.flow +1 -1
  180. package/store/normalizeRelayPayload.js.flow +7 -8
  181. package/store/readInlineData.js.flow +8 -9
  182. package/subscription/requestSubscription.js.flow +55 -51
  183. package/util/JSResourceTypes.flow.js.flow +1 -1
  184. package/util/NormalizationNode.js.flow +11 -4
  185. package/util/ReaderNode.js.flow +25 -2
  186. package/util/RelayConcreteNode.js.flow +5 -1
  187. package/util/RelayDefaultHandleKey.js.flow +1 -1
  188. package/util/RelayError.js.flow +1 -1
  189. package/util/RelayFeatureFlags.js.flow +23 -15
  190. package/util/RelayProfiler.js.flow +1 -1
  191. package/util/RelayReplaySubject.js.flow +10 -10
  192. package/util/RelayRuntimeTypes.js.flow +70 -3
  193. package/util/StringInterner.js.flow +69 -0
  194. package/util/createPayloadFor3DField.js.flow +4 -4
  195. package/util/deepFreeze.js.flow +1 -1
  196. package/util/generateID.js.flow +1 -1
  197. package/util/getAllRootVariables.js.flow +36 -0
  198. package/util/getFragmentIdentifier.js.flow +28 -16
  199. package/util/getOperation.js.flow +3 -3
  200. package/util/getPaginationMetadata.js.flow +69 -0
  201. package/util/getPaginationVariables.js.flow +108 -0
  202. package/util/getPendingOperationsForFragment.js.flow +62 -0
  203. package/util/getRefetchMetadata.js.flow +76 -0
  204. package/util/getRelayHandleKey.js.flow +2 -3
  205. package/util/getRequestIdentifier.js.flow +4 -4
  206. package/util/getValueAtPath.js.flow +46 -0
  207. package/util/isEmptyObject.js.flow +2 -1
  208. package/util/isPromise.js.flow +1 -1
  209. package/util/isScalarAndEqual.js.flow +1 -1
  210. package/util/recycleNodesInto.js.flow +1 -1
  211. package/util/registerEnvironmentWithDevTools.js.flow +33 -0
  212. package/util/reportMissingRequiredFields.js.flow +1 -1
  213. package/util/resolveImmediate.js.flow +2 -2
  214. package/util/stableCopy.js.flow +1 -1
  215. package/util/withDuration.js.flow +32 -0
  216. package/lib/store/RelayRecordSourceMapImpl.js +0 -107
  217. package/lib/store/RelayStoreSubscriptionsUsingMapByID.js +0 -318
  218. package/store/RelayRecordSourceMapImpl.js.flow +0 -91
  219. package/store/RelayStoreSubscriptionsUsingMapByID.js.flow +0 -283
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ import type {RequestParameters} from './RelayConcreteNode';
17
+ import type {Variables} from './RelayRuntimeTypes';
18
+
19
+ function getAllRootVariables(
20
+ userSuppliedVariables: Variables,
21
+ parameters: RequestParameters,
22
+ ): Variables {
23
+ const providedVariables = parameters.providedVariables;
24
+ if (providedVariables != null) {
25
+ const allVariables = {};
26
+ Object.assign(allVariables, userSuppliedVariables);
27
+ Object.keys(providedVariables).forEach((varName: string) => {
28
+ allVariables[varName] = providedVariables[varName].get();
29
+ });
30
+ return allVariables;
31
+ } else {
32
+ return userSuppliedVariables;
33
+ }
34
+ }
35
+
36
+ module.exports = getAllRootVariables;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -13,18 +13,17 @@
13
13
 
14
14
  'use strict';
15
15
 
16
- const RelayFeatureFlags = require('./RelayFeatureFlags');
17
-
18
- const isEmptyObject = require('./isEmptyObject');
19
- const stableCopy = require('./stableCopy');
16
+ import type {ReaderFragment} from './ReaderNode';
20
17
 
21
18
  const {
22
19
  getDataIDsFromFragment,
23
- getVariablesFromFragment,
24
20
  getSelector,
21
+ getVariablesFromFragment,
25
22
  } = require('../store/RelayModernSelector');
26
-
27
- import type {ReaderFragment} from './ReaderNode';
23
+ const isEmptyObject = require('./isEmptyObject');
24
+ const RelayFeatureFlags = require('./RelayFeatureFlags');
25
+ const stableCopy = require('./stableCopy');
26
+ const {intern} = require('./StringInterner');
28
27
 
29
28
  function getFragmentIdentifier(
30
29
  fragmentNode: ReaderFragment,
@@ -43,6 +42,19 @@ function getFragmentIdentifier(
43
42
  const dataIDs = getDataIDsFromFragment(fragmentNode, fragmentRef);
44
43
 
45
44
  if (RelayFeatureFlags.ENABLE_GETFRAGMENTIDENTIFIER_OPTIMIZATION) {
45
+ let ids =
46
+ typeof dataIDs === 'undefined'
47
+ ? 'missing'
48
+ : dataIDs == null
49
+ ? 'null'
50
+ : Array.isArray(dataIDs)
51
+ ? '[' + dataIDs.join(',') + ']'
52
+ : dataIDs;
53
+ ids =
54
+ RelayFeatureFlags.STRING_INTERN_LEVEL <= 1
55
+ ? ids
56
+ : intern(ids, RelayFeatureFlags.MAX_DATA_ID_LENGTH);
57
+
46
58
  return (
47
59
  fragmentOwnerIdentifier +
48
60
  '/' +
@@ -52,15 +64,15 @@ function getFragmentIdentifier(
52
64
  ? '{}'
53
65
  : JSON.stringify(stableCopy(fragmentVariables))) +
54
66
  '/' +
55
- (typeof dataIDs === 'undefined'
56
- ? 'missing'
57
- : dataIDs == null
58
- ? 'null'
59
- : Array.isArray(dataIDs)
60
- ? '[' + dataIDs.join(',') + ']'
61
- : dataIDs)
67
+ ids
62
68
  );
63
69
  } else {
70
+ let ids = JSON.stringify(dataIDs) ?? 'missing';
71
+ ids =
72
+ RelayFeatureFlags.STRING_INTERN_LEVEL <= 1
73
+ ? ids
74
+ : intern(ids, RelayFeatureFlags.MAX_DATA_ID_LENGTH);
75
+
64
76
  return (
65
77
  fragmentOwnerIdentifier +
66
78
  '/' +
@@ -68,7 +80,7 @@ function getFragmentIdentifier(
68
80
  '/' +
69
81
  JSON.stringify(stableCopy(fragmentVariables)) +
70
82
  '/' +
71
- (JSON.stringify(dataIDs) ?? 'missing')
83
+ ids
72
84
  );
73
85
  }
74
86
  }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -13,14 +13,14 @@
13
13
 
14
14
  'use strict';
15
15
 
16
- const {REQUEST, SPLIT_OPERATION} = require('./RelayConcreteNode');
17
-
18
16
  import type {
19
17
  NormalizationOperation,
20
18
  NormalizationRootNode,
21
19
  NormalizationSplitOperation,
22
20
  } from './NormalizationNode';
23
21
 
22
+ const {REQUEST, SPLIT_OPERATION} = require('./RelayConcreteNode');
23
+
24
24
  /**
25
25
  * OperationLoaders can return either a NormalizationSplitOperation or
26
26
  * ConcreteRequest.
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ import type {ReaderFragment, ReaderPaginationMetadata} from './ReaderNode';
17
+ import type {ConcreteRequest} from './RelayConcreteNode';
18
+
19
+ const getRefetchMetadata = require('./getRefetchMetadata');
20
+ const invariant = require('invariant');
21
+
22
+ function getPaginationMetadata(
23
+ fragmentNode: ReaderFragment,
24
+ componentDisplayName: string,
25
+ ): {|
26
+ connectionPathInFragmentData: $ReadOnlyArray<string | number>,
27
+ identifierField: ?string,
28
+ paginationRequest: ConcreteRequest,
29
+ paginationMetadata: ReaderPaginationMetadata,
30
+ stream: boolean,
31
+ |} {
32
+ const {refetchableRequest: paginationRequest, refetchMetadata} =
33
+ getRefetchMetadata(fragmentNode, componentDisplayName);
34
+
35
+ const paginationMetadata = refetchMetadata.connection;
36
+ invariant(
37
+ paginationMetadata != null,
38
+ 'Relay: getPaginationMetadata(): Expected fragment `%s` to include a ' +
39
+ 'connection when using `%s`. Did you forget to add a @connection ' +
40
+ 'directive to the connection field in the fragment?',
41
+ componentDisplayName,
42
+ fragmentNode.name,
43
+ );
44
+ const connectionPathInFragmentData = paginationMetadata.path;
45
+
46
+ const connectionMetadata = (fragmentNode.metadata?.connection ?? [])[0];
47
+ invariant(
48
+ connectionMetadata != null,
49
+ 'Relay: getPaginationMetadata(): Expected fragment `%s` to include a ' +
50
+ 'connection when using `%s`. Did you forget to add a @connection ' +
51
+ 'directive to the connection field in the fragment?',
52
+ componentDisplayName,
53
+ fragmentNode.name,
54
+ );
55
+ const identifierField = refetchMetadata.identifierField;
56
+ invariant(
57
+ identifierField == null || typeof identifierField === 'string',
58
+ 'Relay: getRefetchMetadata(): Expected `identifierField` to be a string.',
59
+ );
60
+ return {
61
+ connectionPathInFragmentData,
62
+ identifierField,
63
+ paginationRequest,
64
+ paginationMetadata,
65
+ stream: connectionMetadata.stream === true,
66
+ };
67
+ }
68
+
69
+ module.exports = getPaginationMetadata;
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ import type {ReaderPaginationMetadata} from './ReaderNode';
17
+ import type {Variables} from './RelayRuntimeTypes';
18
+
19
+ const invariant = require('invariant');
20
+ const warning = require('warning');
21
+
22
+ export type Direction = 'forward' | 'backward';
23
+
24
+ function getPaginationVariables(
25
+ direction: Direction,
26
+ count: number,
27
+ cursor: ?string,
28
+ baseVariables: Variables,
29
+ extraVariables: Variables,
30
+ paginationMetadata: ReaderPaginationMetadata,
31
+ ): {[string]: mixed, ...} {
32
+ const {backward: backwardMetadata, forward: forwardMetadata} =
33
+ paginationMetadata;
34
+
35
+ if (direction === 'backward') {
36
+ invariant(
37
+ backwardMetadata != null &&
38
+ backwardMetadata.count != null &&
39
+ backwardMetadata.cursor != null,
40
+ 'Relay: Expected backward pagination metadata to be available. ' +
41
+ "If you're seeing this, this is likely a bug in Relay.",
42
+ );
43
+ warning(
44
+ !extraVariables.hasOwnProperty(backwardMetadata.cursor),
45
+ 'Relay: `UNSTABLE_extraVariables` provided by caller should not ' +
46
+ 'contain cursor variable `%s`. This variable is automatically ' +
47
+ 'determined by Relay.',
48
+ backwardMetadata.cursor,
49
+ );
50
+ warning(
51
+ !extraVariables.hasOwnProperty(backwardMetadata.count),
52
+ 'Relay: `UNSTABLE_extraVariables` provided by caller should not ' +
53
+ 'contain count variable `%s`. This variable is automatically ' +
54
+ 'determined by Relay.',
55
+ backwardMetadata.count,
56
+ );
57
+ const paginationVariables = {
58
+ ...baseVariables,
59
+ ...extraVariables,
60
+ [backwardMetadata.cursor]: cursor,
61
+ [backwardMetadata.count]: count,
62
+ };
63
+ if (forwardMetadata && forwardMetadata.cursor) {
64
+ paginationVariables[forwardMetadata.cursor] = null;
65
+ }
66
+ if (forwardMetadata && forwardMetadata.count) {
67
+ paginationVariables[forwardMetadata.count] = null;
68
+ }
69
+ return paginationVariables;
70
+ }
71
+
72
+ invariant(
73
+ forwardMetadata != null &&
74
+ forwardMetadata.count != null &&
75
+ forwardMetadata.cursor != null,
76
+ 'Relay: Expected forward pagination metadata to be available. ' +
77
+ "If you're seeing this, this is likely a bug in Relay.",
78
+ );
79
+ warning(
80
+ !extraVariables.hasOwnProperty(forwardMetadata.cursor),
81
+ 'Relay: `UNSTABLE_extraVariables` provided by caller should not ' +
82
+ 'contain cursor variable `%s`. This variable is automatically ' +
83
+ 'determined by Relay.',
84
+ forwardMetadata.cursor,
85
+ );
86
+ warning(
87
+ !extraVariables.hasOwnProperty(forwardMetadata.count),
88
+ 'Relay: `UNSTABLE_extraVariables` provided by caller should not ' +
89
+ 'contain count variable `%s`. This variable is automatically ' +
90
+ 'determined by Relay.',
91
+ forwardMetadata.count,
92
+ );
93
+ const paginationVariables = {
94
+ ...baseVariables,
95
+ ...extraVariables,
96
+ [forwardMetadata.cursor]: cursor,
97
+ [forwardMetadata.count]: count,
98
+ };
99
+ if (backwardMetadata && backwardMetadata.cursor) {
100
+ paginationVariables[backwardMetadata.cursor] = null;
101
+ }
102
+ if (backwardMetadata && backwardMetadata.count) {
103
+ paginationVariables[backwardMetadata.count] = null;
104
+ }
105
+ return paginationVariables;
106
+ }
107
+
108
+ module.exports = getPaginationVariables;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ import type {IEnvironment, RequestDescriptor} from '../store/RelayStoreTypes';
17
+ import type {ReaderFragment} from './ReaderNode';
18
+
19
+ const {getPromiseForActiveRequest} = require('../query/fetchQueryInternal');
20
+
21
+ function getPendingOperationsForFragment(
22
+ environment: IEnvironment,
23
+ fragmentNode: ReaderFragment,
24
+ fragmentOwner: RequestDescriptor,
25
+ ): {|
26
+ promise: Promise<void>,
27
+ pendingOperations: $ReadOnlyArray<RequestDescriptor>,
28
+ |} | null {
29
+ let pendingOperations: $ReadOnlyArray<RequestDescriptor> = [];
30
+ let promise = getPromiseForActiveRequest(environment, fragmentOwner);
31
+
32
+ if (promise != null) {
33
+ pendingOperations = [fragmentOwner];
34
+ } else {
35
+ const result = environment
36
+ .getOperationTracker()
37
+ .getPendingOperationsAffectingOwner(fragmentOwner);
38
+
39
+ pendingOperations = result?.pendingOperations ?? [];
40
+ promise = result?.promise ?? null;
41
+ }
42
+
43
+ if (!promise) {
44
+ return null;
45
+ }
46
+
47
+ let pendingOperationName =
48
+ pendingOperations?.map(op => op.node.params.name).join(',') ?? null;
49
+ if (pendingOperationName == null || pendingOperationName.length === 0) {
50
+ pendingOperationName = 'Unknown pending operation';
51
+ }
52
+ const fragmentName = fragmentNode.name;
53
+ const promiseDisplayName =
54
+ pendingOperationName === fragmentName
55
+ ? `Relay(${pendingOperationName})`
56
+ : `Relay(${pendingOperationName}:${fragmentName})`;
57
+ // $FlowExpectedError[prop-missing] Expando to annotate Promises.
58
+ promise.displayName = promiseDisplayName;
59
+ return {promise, pendingOperations};
60
+ }
61
+
62
+ module.exports = getPendingOperationsForFragment;
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ import type {ReaderFragment, ReaderRefetchMetadata} from './ReaderNode';
17
+ import type {ConcreteRequest} from './RelayConcreteNode';
18
+
19
+ const invariant = require('invariant');
20
+
21
+ function getRefetchMetadata(
22
+ fragmentNode: ReaderFragment,
23
+ componentDisplayName: string,
24
+ ): {|
25
+ fragmentRefPathInResponse: $ReadOnlyArray<string | number>,
26
+ identifierField: ?string,
27
+ refetchableRequest: ConcreteRequest,
28
+ refetchMetadata: ReaderRefetchMetadata,
29
+ |} {
30
+ invariant(
31
+ fragmentNode.metadata?.plural !== true,
32
+ 'Relay: getRefetchMetadata(): Expected fragment `%s` not to be plural when using ' +
33
+ '`%s`. Remove `@relay(plural: true)` from fragment `%s` ' +
34
+ 'in order to use it with `%s`.',
35
+ fragmentNode.name,
36
+ componentDisplayName,
37
+ fragmentNode.name,
38
+ componentDisplayName,
39
+ );
40
+
41
+ const refetchMetadata = fragmentNode.metadata?.refetch;
42
+ invariant(
43
+ refetchMetadata != null,
44
+ 'Relay: getRefetchMetadata(): Expected fragment `%s` to be refetchable when using `%s`. ' +
45
+ 'Did you forget to add a @refetchable directive to the fragment?',
46
+ componentDisplayName,
47
+ fragmentNode.name,
48
+ );
49
+
50
+ // handle both commonjs and es modules
51
+ const refetchableRequest: ConcreteRequest | string =
52
+ (refetchMetadata: $FlowFixMe).operation.default
53
+ ? (refetchMetadata: $FlowFixMe).operation.default
54
+ : refetchMetadata.operation;
55
+ const fragmentRefPathInResponse = refetchMetadata.fragmentPathInResult;
56
+ invariant(
57
+ typeof refetchableRequest !== 'string',
58
+ 'Relay: getRefetchMetadata(): Expected refetch query to be an ' +
59
+ "operation and not a string when using `%s`. If you're seeing this, " +
60
+ 'this is likely a bug in Relay.',
61
+ componentDisplayName,
62
+ );
63
+ const identifierField = refetchMetadata.identifierField;
64
+ invariant(
65
+ identifierField == null || typeof identifierField === 'string',
66
+ 'Relay: getRefetchMetadata(): Expected `identifierField` to be a string.',
67
+ );
68
+ return {
69
+ fragmentRefPathInResponse,
70
+ identifierField,
71
+ refetchableRequest,
72
+ refetchMetadata,
73
+ };
74
+ }
75
+
76
+ module.exports = getRefetchMetadata;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -12,9 +12,8 @@
12
12
 
13
13
  'use strict';
14
14
 
15
- const invariant = require('invariant');
16
-
17
15
  const {DEFAULT_HANDLE_KEY} = require('./RelayDefaultHandleKey');
16
+ const invariant = require('invariant');
18
17
 
19
18
  /**
20
19
  * @internal
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -12,12 +12,12 @@
12
12
 
13
13
  'use strict';
14
14
 
15
- const invariant = require('invariant');
16
- const stableCopy = require('./stableCopy');
17
-
18
15
  import type {RequestParameters} from './RelayConcreteNode';
19
16
  import type {Variables} from './RelayRuntimeTypes';
20
17
 
18
+ const stableCopy = require('./stableCopy');
19
+ const invariant = require('invariant');
20
+
21
21
  export type RequestIdentifier = string;
22
22
 
23
23
  /**
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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 invariant = require('invariant');
17
+
18
+ function getValueAtPath(
19
+ data: mixed,
20
+ path: $ReadOnlyArray<string | number>,
21
+ ): mixed {
22
+ let result = data;
23
+ for (const key of path) {
24
+ if (result == null) {
25
+ return null;
26
+ }
27
+ if (typeof key === 'number') {
28
+ invariant(
29
+ Array.isArray(result),
30
+ 'Relay: Expected an array when extracting value at path. ' +
31
+ "If you're seeing this, this is likely a bug in Relay.",
32
+ );
33
+ result = result[key];
34
+ } else {
35
+ invariant(
36
+ typeof result === 'object' && !Array.isArray(result),
37
+ 'Relay: Expected an object when extracting value at path. ' +
38
+ "If you're seeing this, this is likely a bug in Relay.",
39
+ );
40
+ result = result[key];
41
+ }
42
+ }
43
+ return result;
44
+ }
45
+
46
+ module.exports = getValueAtPath;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -11,6 +11,7 @@
11
11
 
12
12
  'use strict';
13
13
 
14
+ // $FlowFixMe[method-unbinding] added when improving typing for this parameters
14
15
  const hasOwnProperty = Object.prototype.hasOwnProperty;
15
16
 
16
17
  function isEmptyObject(obj: interface {+[key: string]: mixed}): boolean {
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ import type {IEnvironment} from '../store/RelayStoreTypes';
17
+
18
+ function registerEnvironmentWithDevTools(environment: IEnvironment): void {
19
+ // Register this Relay Environment with Relay DevTools if it exists.
20
+ // Note: this must always be the last step in the constructor.
21
+ const _global =
22
+ typeof global !== 'undefined'
23
+ ? global
24
+ : typeof window !== 'undefined'
25
+ ? window
26
+ : undefined;
27
+ const devToolsHook = _global && _global.__RELAY_DEVTOOLS_HOOK__;
28
+ if (devToolsHook) {
29
+ devToolsHook.registerEnvironment(environment);
30
+ }
31
+ }
32
+
33
+ module.exports = registerEnvironmentWithDevTools;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -21,7 +21,7 @@ function resolveImmediate(callback: () => void) {
21
21
  resolvedPromise.then(callback).catch(throwNext);
22
22
  }
23
23
 
24
- function throwNext(error) {
24
+ function throwNext(error: $FlowFixMe) {
25
25
  setTimeout(() => {
26
26
  throw error;
27
27
  }, 0);
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and 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
+ const isPerformanceNowAvailable =
16
+ typeof window !== 'undefined' &&
17
+ typeof window?.performance?.now === 'function';
18
+
19
+ function currentTimestamp(): number {
20
+ if (isPerformanceNowAvailable) {
21
+ return window.performance.now();
22
+ }
23
+ return Date.now();
24
+ }
25
+
26
+ function withDuration<T>(cb: () => T): [number, T] {
27
+ const startTime = currentTimestamp();
28
+ const result = cb();
29
+ return [currentTimestamp() - startTime, result];
30
+ }
31
+
32
+ module.exports = withDuration;