relay-runtime 11.0.0 → 13.0.0-rc.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 (169) hide show
  1. package/handlers/RelayDefaultHandlerProvider.js.flow +2 -2
  2. package/handlers/connection/ConnectionHandler.js.flow +8 -10
  3. package/handlers/connection/MutationHandlers.js.flow +31 -7
  4. package/index.js +1 -1
  5. package/index.js.flow +60 -36
  6. package/lib/handlers/RelayDefaultHandlerProvider.js +1 -1
  7. package/lib/handlers/connection/ConnectionHandler.js +8 -8
  8. package/lib/handlers/connection/MutationHandlers.js +61 -5
  9. package/lib/index.js +58 -43
  10. package/lib/multi-actor-environment/ActorIdentifier.js +33 -0
  11. package/lib/multi-actor-environment/ActorSpecificEnvironment.js +152 -0
  12. package/lib/multi-actor-environment/ActorUtils.js +27 -0
  13. package/lib/multi-actor-environment/MultiActorEnvironment.js +419 -0
  14. package/lib/multi-actor-environment/MultiActorEnvironmentTypes.js +11 -0
  15. package/lib/multi-actor-environment/index.js +21 -0
  16. package/lib/mutations/RelayDeclarativeMutationConfig.js +4 -1
  17. package/lib/mutations/RelayRecordProxy.js +3 -2
  18. package/lib/mutations/RelayRecordSourceMutator.js +3 -2
  19. package/lib/mutations/RelayRecordSourceProxy.js +12 -4
  20. package/lib/mutations/RelayRecordSourceSelectorProxy.js +18 -5
  21. package/lib/mutations/applyOptimisticMutation.js +6 -6
  22. package/lib/mutations/commitMutation.js +14 -10
  23. package/lib/mutations/readUpdatableQuery_EXPERIMENTAL.js +238 -0
  24. package/lib/mutations/validateMutation.js +12 -5
  25. package/lib/network/ConvertToExecuteFunction.js +2 -1
  26. package/lib/network/RelayNetwork.js +3 -2
  27. package/lib/network/RelayQueryResponseCache.js +21 -4
  28. package/lib/network/wrapNetworkWithLogObserver.js +79 -0
  29. package/lib/query/GraphQLTag.js +3 -2
  30. package/lib/query/fetchQuery.js +6 -5
  31. package/lib/query/fetchQueryInternal.js +1 -1
  32. package/lib/query/fetchQuery_DEPRECATED.js +2 -1
  33. package/lib/store/ClientID.js +7 -1
  34. package/lib/store/DataChecker.js +141 -60
  35. package/lib/store/{RelayModernQueryExecutor.js → OperationExecutor.js} +532 -195
  36. package/lib/store/RelayConcreteVariables.js +24 -4
  37. package/lib/store/RelayModernEnvironment.js +175 -234
  38. package/lib/store/RelayModernFragmentSpecResolver.js +52 -26
  39. package/lib/store/RelayModernOperationDescriptor.js +2 -1
  40. package/lib/store/RelayModernRecord.js +47 -12
  41. package/lib/store/RelayModernSelector.js +14 -8
  42. package/lib/store/RelayModernStore.js +58 -29
  43. package/lib/store/RelayOperationTracker.js +34 -24
  44. package/lib/store/RelayPublishQueue.js +41 -13
  45. package/lib/store/RelayReader.js +287 -46
  46. package/lib/store/RelayRecordSource.js +87 -3
  47. package/lib/store/RelayReferenceMarker.js +55 -31
  48. package/lib/store/RelayResponseNormalizer.js +250 -108
  49. package/lib/store/RelayStoreReactFlightUtils.js +8 -12
  50. package/lib/store/RelayStoreSubscriptions.js +14 -9
  51. package/lib/store/RelayStoreUtils.js +11 -5
  52. package/lib/store/ResolverCache.js +213 -0
  53. package/lib/store/ResolverFragments.js +61 -0
  54. package/lib/store/cloneRelayHandleSourceField.js +5 -4
  55. package/lib/store/cloneRelayScalarHandleSourceField.js +5 -4
  56. package/lib/store/createRelayContext.js +4 -2
  57. package/lib/store/defaultGetDataID.js +3 -1
  58. package/lib/store/readInlineData.js +6 -2
  59. package/lib/subscription/requestSubscription.js +35 -9
  60. package/lib/util/RelayConcreteNode.js +4 -0
  61. package/lib/util/RelayFeatureFlags.js +11 -4
  62. package/lib/util/RelayProfiler.js +17 -187
  63. package/lib/util/RelayReplaySubject.js +22 -7
  64. package/lib/util/RelayRuntimeTypes.js +0 -6
  65. package/lib/util/StringInterner.js +71 -0
  66. package/lib/util/deepFreeze.js +1 -0
  67. package/lib/util/getFragmentIdentifier.js +15 -7
  68. package/lib/util/getOperation.js +2 -1
  69. package/lib/util/getPaginationMetadata.js +41 -0
  70. package/lib/util/getPaginationVariables.js +66 -0
  71. package/lib/util/getPendingOperationsForFragment.js +55 -0
  72. package/lib/util/getRefetchMetadata.js +36 -0
  73. package/lib/util/getRelayHandleKey.js +2 -2
  74. package/lib/util/getRequestIdentifier.js +2 -2
  75. package/lib/util/getValueAtPath.js +51 -0
  76. package/lib/util/isEmptyObject.js +1 -1
  77. package/lib/util/registerEnvironmentWithDevTools.js +26 -0
  78. package/lib/util/withDuration.js +31 -0
  79. package/multi-actor-environment/ActorIdentifier.js.flow +43 -0
  80. package/multi-actor-environment/ActorSpecificEnvironment.js.flow +225 -0
  81. package/multi-actor-environment/ActorUtils.js.flow +33 -0
  82. package/multi-actor-environment/MultiActorEnvironment.js.flow +506 -0
  83. package/multi-actor-environment/MultiActorEnvironmentTypes.js.flow +261 -0
  84. package/multi-actor-environment/index.js.flow +26 -0
  85. package/mutations/RelayDeclarativeMutationConfig.js.flow +32 -26
  86. package/mutations/RelayRecordProxy.js.flow +4 -5
  87. package/mutations/RelayRecordSourceMutator.js.flow +4 -6
  88. package/mutations/RelayRecordSourceProxy.js.flow +19 -10
  89. package/mutations/RelayRecordSourceSelectorProxy.js.flow +22 -7
  90. package/mutations/applyOptimisticMutation.js.flow +13 -14
  91. package/mutations/commitLocalUpdate.js.flow +1 -1
  92. package/mutations/commitMutation.js.flow +35 -46
  93. package/mutations/readUpdatableQuery_EXPERIMENTAL.js.flow +309 -0
  94. package/mutations/validateMutation.js.flow +28 -16
  95. package/network/ConvertToExecuteFunction.js.flow +2 -2
  96. package/network/RelayNetwork.js.flow +4 -5
  97. package/network/RelayNetworkTypes.js.flow +17 -8
  98. package/network/RelayObservable.js.flow +1 -1
  99. package/network/RelayQueryResponseCache.js.flow +34 -20
  100. package/network/wrapNetworkWithLogObserver.js.flow +100 -0
  101. package/package.json +3 -2
  102. package/query/GraphQLTag.js.flow +9 -9
  103. package/query/PreloadableQueryRegistry.js.flow +2 -1
  104. package/query/fetchQuery.js.flow +11 -13
  105. package/query/fetchQueryInternal.js.flow +6 -9
  106. package/query/fetchQuery_DEPRECATED.js.flow +6 -6
  107. package/relay-runtime.js +2 -2
  108. package/relay-runtime.min.js +2 -2
  109. package/store/ClientID.js.flow +14 -3
  110. package/store/DataChecker.js.flow +162 -67
  111. package/store/{RelayModernQueryExecutor.js.flow → OperationExecutor.js.flow} +616 -283
  112. package/store/RelayConcreteVariables.js.flow +27 -5
  113. package/store/RelayModernEnvironment.js.flow +176 -235
  114. package/store/RelayModernFragmentSpecResolver.js.flow +55 -31
  115. package/store/RelayModernOperationDescriptor.js.flow +12 -7
  116. package/store/RelayModernRecord.js.flow +67 -11
  117. package/store/RelayModernSelector.js.flow +24 -14
  118. package/store/RelayModernStore.js.flow +72 -36
  119. package/store/RelayOperationTracker.js.flow +59 -43
  120. package/store/RelayOptimisticRecordSource.js.flow +2 -2
  121. package/store/RelayPublishQueue.js.flow +79 -34
  122. package/store/RelayReader.js.flow +351 -72
  123. package/store/RelayRecordSource.js.flow +72 -6
  124. package/store/RelayReferenceMarker.js.flow +60 -33
  125. package/store/RelayResponseNormalizer.js.flow +288 -102
  126. package/store/RelayStoreReactFlightUtils.js.flow +9 -13
  127. package/store/RelayStoreSubscriptions.js.flow +19 -11
  128. package/store/RelayStoreTypes.js.flow +210 -44
  129. package/store/RelayStoreUtils.js.flow +25 -11
  130. package/store/ResolverCache.js.flow +249 -0
  131. package/store/ResolverFragments.js.flow +121 -0
  132. package/store/StoreInspector.js.flow +2 -2
  133. package/store/TypeID.js.flow +1 -1
  134. package/store/ViewerPattern.js.flow +2 -2
  135. package/store/cloneRelayHandleSourceField.js.flow +5 -6
  136. package/store/cloneRelayScalarHandleSourceField.js.flow +5 -6
  137. package/store/createFragmentSpecResolver.js.flow +3 -4
  138. package/store/createRelayContext.js.flow +3 -3
  139. package/store/defaultGetDataID.js.flow +3 -1
  140. package/store/normalizeRelayPayload.js.flow +6 -7
  141. package/store/readInlineData.js.flow +7 -8
  142. package/subscription/requestSubscription.js.flow +54 -27
  143. package/util/NormalizationNode.js.flow +16 -3
  144. package/util/ReaderNode.js.flow +38 -2
  145. package/util/RelayConcreteNode.js.flow +4 -0
  146. package/util/RelayFeatureFlags.js.flow +24 -8
  147. package/util/RelayProfiler.js.flow +22 -194
  148. package/util/RelayReplaySubject.js.flow +9 -9
  149. package/util/RelayRuntimeTypes.js.flow +73 -4
  150. package/util/StringInterner.js.flow +69 -0
  151. package/util/createPayloadFor3DField.js.flow +3 -3
  152. package/util/deepFreeze.js.flow +2 -1
  153. package/util/getFragmentIdentifier.js.flow +27 -15
  154. package/util/getOperation.js.flow +2 -2
  155. package/util/getPaginationMetadata.js.flow +72 -0
  156. package/util/getPaginationVariables.js.flow +108 -0
  157. package/util/getPendingOperationsForFragment.js.flow +62 -0
  158. package/util/getRefetchMetadata.js.flow +79 -0
  159. package/util/getRelayHandleKey.js.flow +1 -2
  160. package/util/getRequestIdentifier.js.flow +3 -3
  161. package/util/getValueAtPath.js.flow +46 -0
  162. package/util/isEmptyObject.js.flow +2 -1
  163. package/util/registerEnvironmentWithDevTools.js.flow +33 -0
  164. package/util/resolveImmediate.js.flow +1 -1
  165. package/util/withDuration.js.flow +32 -0
  166. package/lib/store/RelayRecordSourceMapImpl.js +0 -107
  167. package/lib/store/RelayStoreSubscriptionsUsingMapByID.js +0 -318
  168. package/store/RelayRecordSourceMapImpl.js.flow +0 -91
  169. package/store/RelayStoreSubscriptionsUsingMapByID.js.flow +0 -283
@@ -4,7 +4,7 @@
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.
6
6
  *
7
- * @flow strict
7
+ * @flow strict-local
8
8
  * @format
9
9
  */
10
10
 
@@ -16,17 +16,20 @@
16
16
  * Basic types used throughout Relay.
17
17
  */
18
18
 
19
+ import type {ReaderFragment, ReaderInlineDataFragment} from './ReaderNode';
20
+ import type {ConcreteRequest} from './RelayConcreteNode';
21
+
19
22
  /**
20
23
  * Represents any resource that must be explicitly disposed of. The most common
21
24
  * use-case is as a return value for subscriptions, where calling `dispose()`
22
25
  * would cancel the subscription.
23
26
  */
24
- export type Disposable = {dispose(): void, ...};
27
+ export type Disposable = interface {dispose(): void};
25
28
 
26
29
  export type DataID = string;
27
30
 
28
31
  // Variables
29
- export type Variables = {+[string]: $FlowFixMe, ...};
32
+ export type Variables = {+[string]: $FlowFixMe};
30
33
 
31
34
  /**
32
35
  * Generated operation flow types are subtypes of this.
@@ -38,7 +41,7 @@ export type OperationType = {|
38
41
  +rawResponse?: {...},
39
42
  |};
40
43
 
41
- export type VariablesOf<T: OperationType> = $ElementType<T, 'variables'>;
44
+ export type VariablesOf<T: OperationType> = T['variables'];
42
45
 
43
46
  /**
44
47
  * Settings for how a query response may be cached.
@@ -49,6 +52,7 @@ export type VariablesOf<T: OperationType> = $ElementType<T, 'variables'>;
49
52
  * in milliseconds. (This value will be passed to setTimeout.)
50
53
  * - `liveConfigId`: causes a query to live update by calling GraphQLLiveQuery,
51
54
  * it represents a configuration of gateway when doing live query
55
+ * - `onSubscribe`: Not in use.
52
56
  * - `metadata`: user-supplied metadata.
53
57
  * - `transactionId`: a user-supplied value, intended for use as a unique id for
54
58
  * a given instance of executing an operation.
@@ -57,6 +61,7 @@ export type CacheConfig = {|
57
61
  force?: ?boolean,
58
62
  poll?: ?number,
59
63
  liveConfigId?: ?string,
64
+ onSubscribe?: () => void,
60
65
  metadata?: {[key: string]: mixed, ...},
61
66
  transactionId?: ?string,
62
67
  |};
@@ -67,3 +72,67 @@ export type FetchPolicy =
67
72
  | 'store-and-network'
68
73
  | 'store-only';
69
74
  export type RenderPolicy = 'full' | 'partial';
75
+
76
+ /* eslint-disable no-undef */
77
+
78
+ /**
79
+ * Return type of graphql tag literals for all operations.
80
+ */
81
+ declare export opaque type Operation<
82
+ -TVariables: Variables,
83
+ +TData,
84
+ TRawResponse,
85
+ >: ConcreteRequest;
86
+
87
+ /**
88
+ * Return type of graphql tag literals for queries.
89
+ */
90
+ declare export opaque type Query<
91
+ -TVariables: Variables,
92
+ +TData,
93
+ TRawResponse = void,
94
+ >: Operation<TVariables, TData, TRawResponse>;
95
+
96
+ /**
97
+ * Return type of graphql tag literals for mutations.
98
+ */
99
+ declare export opaque type Mutation<
100
+ -TVariables: Variables,
101
+ +TData,
102
+ TRawResponse = void,
103
+ >: Operation<TVariables, TData, TRawResponse>;
104
+
105
+ /**
106
+ * Return type of graphql tag literals for subscriptions.
107
+ *
108
+ * NOTE: Using the GraphQL prefix here because of a naming conflict with
109
+ * `RelayObservable`'s `Subscription` type.
110
+ */
111
+ declare export opaque type GraphQLSubscription<
112
+ -TVariables: Variables,
113
+ +TData,
114
+ TRawResponse = void,
115
+ >: Operation<TVariables, TData, TRawResponse>;
116
+
117
+ /**
118
+ * Return type of graphql tag literals for `@inline` fragments.
119
+ */
120
+ declare export opaque type InlineFragment<
121
+ TFragmentType,
122
+ +TData,
123
+ >: ReaderInlineDataFragment;
124
+
125
+ /**
126
+ * Return type of graphql tag literals for fragments, except `@inline`
127
+ * fragments.
128
+ */
129
+ declare export opaque type Fragment<TFragmentType, +TData>: ReaderFragment;
130
+
131
+ /**
132
+ * Return type of graphql tag literals for `@refetchable` fragments.
133
+ */
134
+ declare export opaque type RefetchableFragment<
135
+ TFragmentType,
136
+ +TData,
137
+ -TVariables: Variables,
138
+ >: Fragment<TFragmentType, TData>;
@@ -0,0 +1,69 @@
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
+ const internTable = new Map();
16
+ let nextIndex = 1;
17
+ const digits = initDigitTable();
18
+
19
+ // Character used as the prefix for interned strings. The specific character is
20
+ // chosen to reduce the likelihood that non-interned input strings need to be
21
+ // escaped (choosing eg a-Z would increase the likelihood we need to escape)
22
+ const INTERN_PREFIX = '\t';
23
+ // Character used as the prefix of escaped strings. As above, this is also
24
+ // chosen to be unlikely in normal input strings.
25
+ const ESCAPE_PREFIX = '\v';
26
+
27
+ function initDigitTable() {
28
+ // disable lint because digits isn't defined when this function is called
29
+ // eslint-disable-next-line no-shadow
30
+ const digits = new Set();
31
+ for (let i = 0; i < 10; ++i) {
32
+ digits.add(i.toString());
33
+ }
34
+ return digits;
35
+ }
36
+
37
+ // Escape a string so that it cannot conflict with an interned string
38
+ function escape(str: string): string {
39
+ if (
40
+ // "\t<digit>..." -> "\v\t<digit>..."
41
+ (str[0] === INTERN_PREFIX && digits.has(str[1])) ||
42
+ // "\v..." -> "\v\v..."
43
+ str[0] === ESCAPE_PREFIX
44
+ ) {
45
+ return ESCAPE_PREFIX + str;
46
+ }
47
+ return str;
48
+ }
49
+
50
+ // Interns the input string if its length equals or exceeds the given `limit`,
51
+ // returning a shorter replacement string that is uniquely associated with the
52
+ // input: multiple calls to intern() for the equivalent input strings (and limit)
53
+ // will always return the exact same string.
54
+ // Strings shorter than the limit are not interned but are escaped if they
55
+ // could conflict with interned strings.
56
+ function intern(str: string, limit: ?number): string {
57
+ if (limit == null || str.length < limit) {
58
+ return escape(str);
59
+ }
60
+ let internedString = internTable.get(str);
61
+ if (internedString != null) {
62
+ return internedString;
63
+ }
64
+ internedString = INTERN_PREFIX + nextIndex++;
65
+ internTable.set(str, internedString);
66
+ return internedString;
67
+ }
68
+
69
+ module.exports = {intern};
@@ -13,14 +13,14 @@
13
13
 
14
14
  'use strict';
15
15
 
16
+ import type {JSResourceReference} from 'JSResourceReference';
17
+ import type {NormalizationSplitOperation} from './NormalizationNode';
18
+
16
19
  const {
17
20
  getModuleComponentKey,
18
21
  getModuleOperationKey,
19
22
  } = require('../store/RelayStoreUtils');
20
23
 
21
- import type {NormalizationSplitOperation} from './NormalizationNode';
22
- import type {JSResourceReference} from 'JSResourceReference';
23
-
24
24
  export opaque type Local3DPayload<
25
25
  +DocumentName: string,
26
26
  +Response: {...},
@@ -18,9 +18,10 @@
18
18
  * For convenience, and for consistency with the behavior of `Object.freeze`,
19
19
  * returns the now-frozen original object.
20
20
  */
21
- function deepFreeze<T: {...}>(object: T): T {
21
+ function deepFreeze<T: interface {}>(object: T): T {
22
22
  Object.freeze(object);
23
23
  Object.getOwnPropertyNames(object).forEach(name => {
24
+ // $FlowFixMe[prop-missing]
24
25
  const property = object[name];
25
26
  if (
26
27
  property &&
@@ -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
  }
@@ -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,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
+ * @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 {
17
+ ConcreteRequest,
18
+ ReaderFragment,
19
+ ReaderPaginationMetadata,
20
+ } from 'relay-runtime';
21
+
22
+ const getRefetchMetadata = require('./getRefetchMetadata');
23
+ const invariant = require('invariant');
24
+
25
+ function getPaginationMetadata(
26
+ fragmentNode: ReaderFragment,
27
+ componentDisplayName: string,
28
+ ): {|
29
+ connectionPathInFragmentData: $ReadOnlyArray<string | number>,
30
+ identifierField: ?string,
31
+ paginationRequest: ConcreteRequest,
32
+ paginationMetadata: ReaderPaginationMetadata,
33
+ stream: boolean,
34
+ |} {
35
+ const {refetchableRequest: paginationRequest, refetchMetadata} =
36
+ getRefetchMetadata(fragmentNode, componentDisplayName);
37
+
38
+ const paginationMetadata = refetchMetadata.connection;
39
+ invariant(
40
+ paginationMetadata != null,
41
+ 'Relay: getPaginationMetadata(): Expected fragment `%s` to include a ' +
42
+ 'connection when using `%s`. Did you forget to add a @connection ' +
43
+ 'directive to the connection field in the fragment?',
44
+ componentDisplayName,
45
+ fragmentNode.name,
46
+ );
47
+ const connectionPathInFragmentData = paginationMetadata.path;
48
+
49
+ const connectionMetadata = (fragmentNode.metadata?.connection ?? [])[0];
50
+ invariant(
51
+ connectionMetadata != null,
52
+ 'Relay: getPaginationMetadata(): Expected fragment `%s` to include a ' +
53
+ 'connection when using `%s`. Did you forget to add a @connection ' +
54
+ 'directive to the connection field in the fragment?',
55
+ componentDisplayName,
56
+ fragmentNode.name,
57
+ );
58
+ const identifierField = refetchMetadata.identifierField;
59
+ invariant(
60
+ identifierField == null || typeof identifierField === 'string',
61
+ 'Relay: getRefetchMetadata(): Expected `identifierField` to be a string.',
62
+ );
63
+ return {
64
+ connectionPathInFragmentData,
65
+ identifierField,
66
+ paginationRequest,
67
+ paginationMetadata,
68
+ stream: connectionMetadata.stream === true,
69
+ };
70
+ }
71
+
72
+ module.exports = getPaginationMetadata;
@@ -0,0 +1,108 @@
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
+ 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) 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
+ 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,79 @@
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
+ import type {
17
+ ConcreteRequest,
18
+ ReaderFragment,
19
+ ReaderRefetchMetadata,
20
+ } from 'relay-runtime';
21
+
22
+ const invariant = require('invariant');
23
+
24
+ function getRefetchMetadata(
25
+ fragmentNode: ReaderFragment,
26
+ componentDisplayName: string,
27
+ ): {|
28
+ fragmentRefPathInResponse: $ReadOnlyArray<string | number>,
29
+ identifierField: ?string,
30
+ refetchableRequest: ConcreteRequest,
31
+ refetchMetadata: ReaderRefetchMetadata,
32
+ |} {
33
+ invariant(
34
+ fragmentNode.metadata?.plural !== true,
35
+ 'Relay: getRefetchMetadata(): Expected fragment `%s` not to be plural when using ' +
36
+ '`%s`. Remove `@relay(plural: true)` from fragment `%s` ' +
37
+ 'in order to use it with `%s`.',
38
+ fragmentNode.name,
39
+ componentDisplayName,
40
+ fragmentNode.name,
41
+ componentDisplayName,
42
+ );
43
+
44
+ const refetchMetadata = fragmentNode.metadata?.refetch;
45
+ invariant(
46
+ refetchMetadata != null,
47
+ 'Relay: getRefetchMetadata(): Expected fragment `%s` to be refetchable when using `%s`. ' +
48
+ 'Did you forget to add a @refetchable directive to the fragment?',
49
+ componentDisplayName,
50
+ fragmentNode.name,
51
+ );
52
+
53
+ // handle both commonjs and es modules
54
+ const refetchableRequest: ConcreteRequest | string =
55
+ (refetchMetadata: $FlowFixMe).operation.default
56
+ ? (refetchMetadata: $FlowFixMe).operation.default
57
+ : refetchMetadata.operation;
58
+ const fragmentRefPathInResponse = refetchMetadata.fragmentPathInResult;
59
+ invariant(
60
+ typeof refetchableRequest !== 'string',
61
+ 'Relay: getRefetchMetadata(): Expected refetch query to be an ' +
62
+ "operation and not a string when using `%s`. If you're seeing this, " +
63
+ 'this is likely a bug in Relay.',
64
+ componentDisplayName,
65
+ );
66
+ const identifierField = refetchMetadata.identifierField;
67
+ invariant(
68
+ identifierField == null || typeof identifierField === 'string',
69
+ 'Relay: getRefetchMetadata(): Expected `identifierField` to be a string.',
70
+ );
71
+ return {
72
+ fragmentRefPathInResponse,
73
+ identifierField,
74
+ refetchableRequest,
75
+ refetchMetadata,
76
+ };
77
+ }
78
+
79
+ module.exports = getRefetchMetadata;
@@ -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
@@ -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
  /**