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,41 @@
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
+ const invariant = require('invariant');
16
+
17
+ const {DEFAULT_HANDLE_KEY} = require('./RelayDefaultHandleKey');
18
+
19
+ /**
20
+ * @internal
21
+ *
22
+ * Helper to create a unique name for a handle field based on the handle name, handle key and
23
+ * source field.
24
+ */
25
+ function getRelayHandleKey(
26
+ handleName: string,
27
+ key: ?string,
28
+ fieldName: ?string,
29
+ ): string {
30
+ if (key && key !== DEFAULT_HANDLE_KEY) {
31
+ return `__${key}_${handleName}`;
32
+ }
33
+
34
+ invariant(
35
+ fieldName != null,
36
+ 'getRelayHandleKey: Expected either `fieldName` or `key` in `handle` to be provided',
37
+ );
38
+ return `__${fieldName}_${handleName}`;
39
+ }
40
+
41
+ module.exports = getRelayHandleKey;
@@ -0,0 +1,42 @@
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 invariant = require('invariant');
16
+ const stableCopy = require('./stableCopy');
17
+
18
+ import type {RequestParameters} from './RelayConcreteNode';
19
+ import type {Variables} from './RelayRuntimeTypes';
20
+
21
+ export opaque type RequestIdentifier: string = string;
22
+
23
+ /**
24
+ * Returns a stable identifier for the given pair of `RequestParameters` +
25
+ * variables.
26
+ */
27
+ function getRequestIdentifier(
28
+ parameters: RequestParameters,
29
+ variables: Variables,
30
+ ): RequestIdentifier {
31
+ const requestID =
32
+ parameters.cacheID != null ? parameters.cacheID : parameters.id;
33
+ invariant(
34
+ requestID != null,
35
+ 'getRequestIdentifier: Expected request `%s` to have either a ' +
36
+ 'valid `id` or `cacheID` property',
37
+ parameters.name,
38
+ );
39
+ return requestID + JSON.stringify(stableCopy(variables));
40
+ }
41
+
42
+ module.exports = getRequestIdentifier;
@@ -0,0 +1,25 @@
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
+ * @emails oncall+relay
10
+ */
11
+
12
+ 'use strict';
13
+
14
+ const hasOwnProperty = Object.prototype.hasOwnProperty;
15
+
16
+ function isEmptyObject(obj: {+[key: string]: mixed}): boolean {
17
+ for (const key in obj) {
18
+ if (hasOwnProperty.call(obj, key)) {
19
+ return false;
20
+ }
21
+ }
22
+ return true;
23
+ }
24
+
25
+ module.exports = isEmptyObject;
@@ -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
+ declare function isPromise(p: mixed): boolean %checks(p instanceof Promise);
16
+
17
+ function isPromise(p: $FlowFixMe): boolean {
18
+ return !!p && typeof p.then === 'function';
19
+ }
20
+
21
+ module.exports = isPromise;
@@ -0,0 +1,26 @@
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
+ * A fast test to determine if two values are equal scalars:
17
+ * - compares scalars such as booleans, strings, numbers by value
18
+ * - compares functions by identity
19
+ * - returns false for complex values, since these cannot be cheaply tested for
20
+ * equality (use `areEquals` instead)
21
+ */
22
+ function isScalarAndEqual(valueA: mixed, valueB: mixed): boolean {
23
+ return valueA === valueB && (valueA === null || typeof valueA !== 'object');
24
+ }
25
+
26
+ module.exports = isScalarAndEqual;
@@ -0,0 +1,87 @@
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 hasWeakSetDefined = typeof WeakSet !== 'undefined';
16
+ const hasWeakMapDefined = typeof WeakMap !== 'undefined';
17
+
18
+ /**
19
+ * Recycles subtrees from `prevData` by replacing equal subtrees in `nextData`.
20
+ */
21
+ function recycleNodesInto<T>(prevData: T, nextData: T): T {
22
+ if (
23
+ prevData === nextData ||
24
+ typeof prevData !== 'object' ||
25
+ prevData instanceof Set ||
26
+ prevData instanceof Map ||
27
+ (hasWeakSetDefined && prevData instanceof WeakSet) ||
28
+ (hasWeakMapDefined && prevData instanceof WeakMap) ||
29
+ !prevData ||
30
+ typeof nextData !== 'object' ||
31
+ nextData instanceof Set ||
32
+ nextData instanceof Map ||
33
+ (hasWeakSetDefined && nextData instanceof WeakSet) ||
34
+ (hasWeakMapDefined && nextData instanceof WeakMap) ||
35
+ !nextData
36
+ ) {
37
+ return nextData;
38
+ }
39
+ let canRecycle = false;
40
+
41
+ // Assign local variables to preserve Flow type refinement.
42
+ const prevArray = Array.isArray(prevData) ? prevData : null;
43
+ const nextArray = Array.isArray(nextData) ? nextData : null;
44
+ if (prevArray && nextArray) {
45
+ canRecycle =
46
+ nextArray.reduce((wasEqual, nextItem, ii) => {
47
+ const prevValue = prevArray[ii];
48
+ const nextValue = recycleNodesInto(prevValue, nextItem);
49
+ if (nextValue !== nextArray[ii]) {
50
+ if (__DEV__) {
51
+ if (!Object.isFrozen(nextArray)) {
52
+ nextArray[ii] = nextValue;
53
+ }
54
+ } else {
55
+ nextArray[ii] = nextValue;
56
+ }
57
+ }
58
+ return wasEqual && nextValue === prevArray[ii];
59
+ }, true) && prevArray.length === nextArray.length;
60
+ } else if (!prevArray && !nextArray) {
61
+ // Assign local variables to preserve Flow type refinement.
62
+ const prevObject = prevData;
63
+ const nextObject = nextData;
64
+ const prevKeys = Object.keys(prevObject);
65
+ const nextKeys = Object.keys(nextObject);
66
+ canRecycle =
67
+ nextKeys.reduce((wasEqual, key) => {
68
+ const prevValue = prevObject[key];
69
+ const nextValue = recycleNodesInto(prevValue, nextObject[key]);
70
+ if (nextValue !== nextObject[key]) {
71
+ if (__DEV__) {
72
+ if (!Object.isFrozen(nextObject)) {
73
+ // $FlowFixMe[cannot-write]
74
+ nextObject[key] = nextValue;
75
+ }
76
+ } else {
77
+ // $FlowFixMe[cannot-write]
78
+ nextObject[key] = nextValue;
79
+ }
80
+ }
81
+ return wasEqual && nextValue === prevObject[key];
82
+ }, true) && prevKeys.length === nextKeys.length;
83
+ }
84
+ return canRecycle ? prevData : nextData;
85
+ }
86
+
87
+ module.exports = recycleNodesInto;
@@ -0,0 +1,51 @@
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
+ * @emails oncall+relay
9
+ * @format
10
+ */
11
+
12
+ 'use strict';
13
+
14
+ import type {
15
+ IEnvironment,
16
+ MissingRequiredFields,
17
+ } from '../store/RelayStoreTypes';
18
+
19
+ function reportMissingRequiredFields(
20
+ environment: IEnvironment,
21
+ missingRequiredFields: MissingRequiredFields,
22
+ ) {
23
+ switch (missingRequiredFields.action) {
24
+ case 'THROW': {
25
+ const {path, owner} = missingRequiredFields.field;
26
+ // This gives the consumer the chance to throw their own error if they so wish.
27
+ environment.requiredFieldLogger({
28
+ kind: 'missing_field.throw',
29
+ owner,
30
+ fieldPath: path,
31
+ });
32
+ throw new Error(
33
+ `Relay: Missing @required value at path '${path}' in '${owner}'.`,
34
+ );
35
+ }
36
+ case 'LOG':
37
+ missingRequiredFields.fields.forEach(({path, owner}) => {
38
+ environment.requiredFieldLogger({
39
+ kind: 'missing_field.log',
40
+ owner,
41
+ fieldPath: path,
42
+ });
43
+ });
44
+ break;
45
+ default: {
46
+ (missingRequiredFields.action: empty);
47
+ }
48
+ }
49
+ }
50
+
51
+ module.exports = reportMissingRequiredFields;
@@ -0,0 +1,30 @@
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
+ var resolvedPromise = Promise.resolve();
16
+
17
+ /**
18
+ * An alternative to setImmediate based on Promise.
19
+ */
20
+ function resolveImmediate(callback: () => void) {
21
+ resolvedPromise.then(callback).catch(throwNext);
22
+ }
23
+
24
+ function throwNext(error) {
25
+ setTimeout(() => {
26
+ throw error;
27
+ }, 0);
28
+ }
29
+
30
+ module.exports = resolveImmediate;
@@ -0,0 +1,35 @@
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
+ /**
16
+ * Creates a copy of the provided value, ensuring any nested objects have their
17
+ * keys sorted such that equivalent values would have identical JSON.stringify
18
+ * results.
19
+ */
20
+ function stableCopy<T: mixed>(value: T): T {
21
+ if (!value || typeof value !== 'object') {
22
+ return value;
23
+ }
24
+ if (Array.isArray(value)) {
25
+ return value.map(stableCopy);
26
+ }
27
+ const keys = Object.keys(value).sort();
28
+ const stable = {};
29
+ for (let i = 0; i < keys.length; i++) {
30
+ stable[keys[i]] = stableCopy(value[keys[i]]);
31
+ }
32
+ return (stable: any);
33
+ }
34
+
35
+ module.exports = stableCopy;