react-relay 13.1.1 → 14.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (178) hide show
  1. package/ReactRelayContainerUtils.js.flow +0 -2
  2. package/ReactRelayContext.js +1 -1
  3. package/ReactRelayContext.js.flow +0 -2
  4. package/ReactRelayFragmentContainer.js.flow +7 -6
  5. package/ReactRelayFragmentMockRenderer.js.flow +0 -2
  6. package/ReactRelayLocalQueryRenderer.js.flow +1 -3
  7. package/ReactRelayPaginationContainer.js.flow +13 -10
  8. package/ReactRelayQueryFetcher.js.flow +10 -11
  9. package/ReactRelayQueryRenderer.js.flow +15 -16
  10. package/ReactRelayQueryRendererContext.js.flow +1 -3
  11. package/ReactRelayRefetchContainer.js.flow +10 -7
  12. package/ReactRelayTestMocker.js.flow +0 -2
  13. package/ReactRelayTypes.js.flow +6 -8
  14. package/RelayContext.js.flow +0 -2
  15. package/__flowtests__/ReactRelayFragmentContainer-flowtest.js.flow +2 -4
  16. package/__flowtests__/ReactRelayPaginationContainer-flowtest.js.flow +3 -5
  17. package/__flowtests__/ReactRelayRefetchContainer-flowtest.js.flow +3 -5
  18. package/__flowtests__/RelayModern-flowtest.js.flow +2 -4
  19. package/__flowtests__/RelayModernFlowtest_badref.graphql.js.flow +2 -4
  20. package/__flowtests__/RelayModernFlowtest_notref.graphql.js.flow +2 -4
  21. package/__flowtests__/RelayModernFlowtest_user.graphql.js.flow +2 -4
  22. package/__flowtests__/RelayModernFlowtest_users.graphql.js.flow +2 -4
  23. package/__flowtests__/__generated__/ReactRelayFragmentContainerFlowtest_viewer.graphql.js.flow +2 -2
  24. package/__flowtests__/__generated__/ReactRelayFragmentContainerFlowtest_viewer2.graphql.js.flow +2 -2
  25. package/__flowtests__/__generated__/ReactRelayPaginationContainerFlowtestQuery.graphql.js.flow +3 -3
  26. package/__flowtests__/__generated__/ReactRelayPaginationContainerFlowtest_viewer.graphql.js.flow +3 -3
  27. package/__flowtests__/__generated__/ReactRelayRefetchContainerFlowtestQuery.graphql.js.flow +3 -3
  28. package/__flowtests__/__generated__/ReactRelayRefetchContainerFlowtest_viewer.graphql.js.flow +3 -3
  29. package/__flowtests__/__generated__/RelayModernFlowtest_badref.graphql.js.flow +2 -2
  30. package/__flowtests__/__generated__/RelayModernFlowtest_notref.graphql.js.flow +2 -2
  31. package/__flowtests__/__generated__/RelayModernFlowtest_user.graphql.js.flow +2 -2
  32. package/__flowtests__/__generated__/RelayModernFlowtest_users.graphql.js.flow +2 -2
  33. package/assertFragmentMap.js.flow +0 -2
  34. package/buildReactRelayContainer.js.flow +2 -4
  35. package/getRootVariablesForFragments.js.flow +0 -2
  36. package/hooks.js +1 -1
  37. package/hooks.js.flow +0 -2
  38. package/index.js +1 -1
  39. package/index.js.flow +2 -2
  40. package/isRelayEnvironment.js.flow +0 -2
  41. package/jest-react/internalAct.js.flow +25 -9
  42. package/legacy.js +1 -1
  43. package/legacy.js.flow +0 -2
  44. package/lib/ReactRelayContainerUtils.js +0 -1
  45. package/lib/ReactRelayContext.js +0 -1
  46. package/lib/ReactRelayFragmentContainer.js +10 -9
  47. package/lib/ReactRelayFragmentMockRenderer.js +0 -1
  48. package/lib/ReactRelayLocalQueryRenderer.js +0 -1
  49. package/lib/ReactRelayPaginationContainer.js +14 -11
  50. package/lib/ReactRelayQueryFetcher.js +2 -2
  51. package/lib/ReactRelayQueryRenderer.js +2 -4
  52. package/lib/ReactRelayQueryRendererContext.js +0 -1
  53. package/lib/ReactRelayRefetchContainer.js +11 -14
  54. package/lib/ReactRelayTestMocker.js +1 -2
  55. package/lib/ReactRelayTypes.js +0 -1
  56. package/lib/RelayContext.js +0 -1
  57. package/lib/assertFragmentMap.js +0 -1
  58. package/lib/buildReactRelayContainer.js +1 -2
  59. package/lib/getRootVariablesForFragments.js +1 -2
  60. package/lib/hooks.js +0 -1
  61. package/lib/index.js +3 -1
  62. package/lib/isRelayEnvironment.js +0 -1
  63. package/lib/jest-react/internalAct.js +24 -4
  64. package/lib/legacy.js +0 -1
  65. package/lib/multi-actor/useRelayActorEnvironment.js +0 -1
  66. package/lib/readContext.js +2 -2
  67. package/lib/relay-hooks/EntryPointContainer.react.js +0 -1
  68. package/lib/relay-hooks/EntryPointTypes.flow.js +0 -1
  69. package/lib/relay-hooks/FragmentResource.js +68 -29
  70. package/lib/relay-hooks/HooksImplementation.js +29 -0
  71. package/lib/relay-hooks/InternalLogger.js +0 -1
  72. package/lib/relay-hooks/LRUCache.js +0 -1
  73. package/lib/relay-hooks/LazyLoadEntryPointContainer_DEPRECATED.react.js +0 -1
  74. package/lib/relay-hooks/MatchContainer.js +2 -2
  75. package/lib/relay-hooks/ProfilerContext.js +0 -1
  76. package/lib/relay-hooks/QueryResource.js +5 -168
  77. package/lib/relay-hooks/RelayEnvironmentProvider.js +0 -1
  78. package/lib/relay-hooks/SuspenseResource.js +1 -2
  79. package/lib/relay-hooks/loadQuery.js +1 -1
  80. package/lib/relay-hooks/preloadQuery_DEPRECATED.js +8 -13
  81. package/lib/relay-hooks/prepareEntryPoint_DEPRECATED.js +0 -1
  82. package/lib/relay-hooks/react-cache/RelayReactCache.js +36 -0
  83. package/lib/relay-hooks/react-cache/getQueryResultOrFetchQuery_REACT_CACHE.js +344 -0
  84. package/lib/relay-hooks/react-cache/readFragmentInternal_REACT_CACHE.js +239 -0
  85. package/lib/relay-hooks/react-cache/useFragmentInternal_REACT_CACHE.js +598 -0
  86. package/lib/relay-hooks/react-cache/useFragment_REACT_CACHE.js +50 -0
  87. package/lib/relay-hooks/react-cache/useLazyLoadQuery_REACT_CACHE.js +55 -0
  88. package/lib/relay-hooks/react-cache/usePaginationFragment_REACT_CACHE.js +150 -0
  89. package/lib/relay-hooks/react-cache/usePreloadedQuery_REACT_CACHE.js +124 -0
  90. package/lib/relay-hooks/react-cache/useRefetchableFragmentInternal_REACT_CACHE.js +367 -0
  91. package/lib/relay-hooks/react-cache/useRefetchableFragment_REACT_CACHE.js +45 -0
  92. package/lib/relay-hooks/useBlockingPaginationFragment.js +4 -3
  93. package/lib/relay-hooks/useClientQuery.js +33 -0
  94. package/lib/relay-hooks/useEntryPointLoader.js +1 -2
  95. package/lib/relay-hooks/useFetchTrackingRef.js +0 -1
  96. package/lib/relay-hooks/useFragment.js +15 -2
  97. package/lib/relay-hooks/useFragmentNode.js +0 -1
  98. package/lib/relay-hooks/useIsMountedRef.js +0 -1
  99. package/lib/relay-hooks/useLazyLoadQuery.js +4 -2
  100. package/lib/relay-hooks/useLazyLoadQueryNode.js +0 -1
  101. package/lib/relay-hooks/useLoadMoreFunction.js +1 -2
  102. package/lib/relay-hooks/useMemoOperationDescriptor.js +0 -1
  103. package/lib/relay-hooks/useMemoVariables.js +0 -1
  104. package/lib/relay-hooks/useMutation.js +5 -7
  105. package/lib/relay-hooks/usePaginationFragment.js +15 -3
  106. package/lib/relay-hooks/usePreloadedQuery.js +4 -2
  107. package/lib/relay-hooks/useQueryLoader.js +1 -2
  108. package/lib/relay-hooks/useRefetchableFragment.js +14 -2
  109. package/lib/relay-hooks/useRefetchableFragmentNode.js +1 -2
  110. package/lib/relay-hooks/useRelayEnvironment.js +0 -1
  111. package/lib/relay-hooks/useStaticFragmentNodeWarning.js +0 -1
  112. package/lib/relay-hooks/useSubscribeToInvalidationState.js +0 -1
  113. package/lib/relay-hooks/useSubscription.js +0 -1
  114. package/multi-actor/useRelayActorEnvironment.js.flow +0 -2
  115. package/package.json +3 -3
  116. package/react-relay-hooks.js +2 -2
  117. package/react-relay-hooks.min.js +2 -2
  118. package/react-relay-legacy.js +2 -2
  119. package/react-relay-legacy.min.js +2 -2
  120. package/react-relay.js +2 -2
  121. package/react-relay.min.js +2 -2
  122. package/readContext.js.flow +1 -2
  123. package/relay-hooks/EntryPointContainer.react.js.flow +2 -4
  124. package/relay-hooks/EntryPointTypes.flow.js.flow +30 -32
  125. package/relay-hooks/FragmentResource.js.flow +80 -37
  126. package/relay-hooks/HooksImplementation.js.flow +43 -0
  127. package/relay-hooks/InternalLogger.js.flow +0 -2
  128. package/relay-hooks/LRUCache.js.flow +0 -2
  129. package/relay-hooks/LazyLoadEntryPointContainer_DEPRECATED.react.js.flow +4 -6
  130. package/relay-hooks/MatchContainer.js.flow +11 -6
  131. package/relay-hooks/ProfilerContext.js.flow +0 -2
  132. package/relay-hooks/QueryResource.js.flow +12 -209
  133. package/relay-hooks/RelayEnvironmentProvider.js.flow +2 -4
  134. package/relay-hooks/SuspenseResource.js.flow +0 -2
  135. package/relay-hooks/__flowtests__/EntryPointTypes/EntryPointElementConfig-flowtest.js.flow +3 -3
  136. package/relay-hooks/__flowtests__/EntryPointTypes/NestedEntrypoints-flowtest.js.flow +2 -2
  137. package/relay-hooks/__flowtests__/__generated__/useFragmentFlowtest_user.graphql.js.flow +2 -2
  138. package/relay-hooks/__flowtests__/__generated__/useFragmentFlowtest_users.graphql.js.flow +2 -2
  139. package/relay-hooks/__flowtests__/useBlockingPaginationFragment-flowtest.js.flow +4 -6
  140. package/relay-hooks/__flowtests__/useFragment-flowtest.js.flow +0 -2
  141. package/relay-hooks/__flowtests__/usePaginationFragment-flowtest.js.flow +4 -6
  142. package/relay-hooks/__flowtests__/useRefetchableFragment-flowtest.js.flow +0 -2
  143. package/relay-hooks/__flowtests__/utils.js.flow +8 -10
  144. package/relay-hooks/loadQuery.js.flow +2 -1
  145. package/relay-hooks/preloadQuery_DEPRECATED.js.flow +11 -20
  146. package/relay-hooks/prepareEntryPoint_DEPRECATED.js.flow +0 -2
  147. package/relay-hooks/react-cache/RelayReactCache.js.flow +40 -0
  148. package/relay-hooks/react-cache/getQueryResultOrFetchQuery_REACT_CACHE.js.flow +430 -0
  149. package/relay-hooks/react-cache/readFragmentInternal_REACT_CACHE.js.flow +297 -0
  150. package/relay-hooks/react-cache/useFragmentInternal_REACT_CACHE.js.flow +599 -0
  151. package/relay-hooks/react-cache/useFragment_REACT_CACHE.js.flow +72 -0
  152. package/relay-hooks/react-cache/useLazyLoadQuery_REACT_CACHE.js.flow +70 -0
  153. package/relay-hooks/react-cache/usePaginationFragment_REACT_CACHE.js.flow +171 -0
  154. package/relay-hooks/react-cache/usePreloadedQuery_REACT_CACHE.js.flow +151 -0
  155. package/relay-hooks/react-cache/useRefetchableFragmentInternal_REACT_CACHE.js.flow +595 -0
  156. package/relay-hooks/react-cache/useRefetchableFragment_REACT_CACHE.js.flow +65 -0
  157. package/relay-hooks/useBlockingPaginationFragment.js.flow +4 -6
  158. package/relay-hooks/useClientQuery.js.flow +39 -0
  159. package/relay-hooks/useEntryPointLoader.js.flow +6 -8
  160. package/relay-hooks/useFetchTrackingRef.js.flow +2 -4
  161. package/relay-hooks/useFragment.js.flow +17 -12
  162. package/relay-hooks/useFragmentNode.js.flow +2 -4
  163. package/relay-hooks/useIsMountedRef.js.flow +1 -3
  164. package/relay-hooks/useLazyLoadQuery.js.flow +17 -5
  165. package/relay-hooks/useLazyLoadQueryNode.js.flow +2 -4
  166. package/relay-hooks/useLoadMoreFunction.js.flow +6 -8
  167. package/relay-hooks/useMemoOperationDescriptor.js.flow +0 -2
  168. package/relay-hooks/useMemoVariables.js.flow +0 -2
  169. package/relay-hooks/useMutation.js.flow +5 -7
  170. package/relay-hooks/usePaginationFragment.js.flow +44 -19
  171. package/relay-hooks/usePreloadedQuery.js.flow +14 -5
  172. package/relay-hooks/useQueryLoader.js.flow +4 -6
  173. package/relay-hooks/useRefetchableFragment.js.flow +32 -3
  174. package/relay-hooks/useRefetchableFragmentNode.js.flow +38 -25
  175. package/relay-hooks/useRelayEnvironment.js.flow +0 -2
  176. package/relay-hooks/useStaticFragmentNodeWarning.js.flow +0 -2
  177. package/relay-hooks/useSubscribeToInvalidationState.js.flow +0 -2
  178. package/relay-hooks/useSubscription.js.flow +14 -10
@@ -0,0 +1,367 @@
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
+ *
9
+ * @format
10
+ */
11
+ 'use strict';
12
+
13
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
14
+
15
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
16
+
17
+ var ProfilerContext = require('../ProfilerContext');
18
+
19
+ var _require = require('../QueryResource'),
20
+ getQueryResourceForEnvironment = _require.getQueryResourceForEnvironment;
21
+
22
+ var useIsMountedRef = require('../useIsMountedRef');
23
+
24
+ var useQueryLoader = require('../useQueryLoader');
25
+
26
+ var useRelayEnvironment = require('../useRelayEnvironment');
27
+
28
+ var readFragmentInternal = require('./readFragmentInternal_REACT_CACHE');
29
+
30
+ var useFragmentInternal = require('./useFragmentInternal_REACT_CACHE');
31
+
32
+ var invariant = require('invariant');
33
+
34
+ var _require2 = require('react'),
35
+ useCallback = _require2.useCallback,
36
+ useContext = _require2.useContext,
37
+ useReducer = _require2.useReducer;
38
+
39
+ var _require3 = require('relay-runtime'),
40
+ fetchQuery = _require3.__internal.fetchQuery,
41
+ createOperationDescriptor = _require3.createOperationDescriptor,
42
+ getFragmentIdentifier = _require3.getFragmentIdentifier,
43
+ getRefetchMetadata = _require3.getRefetchMetadata,
44
+ getSelector = _require3.getSelector,
45
+ getValueAtPath = _require3.getValueAtPath;
46
+
47
+ var warning = require("fbjs/lib/warning");
48
+
49
+ function reducer(state, action) {
50
+ switch (action.type) {
51
+ case 'refetch':
52
+ {
53
+ var _action$refetchEnviro;
54
+
55
+ return (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, state), {}, {
56
+ fetchPolicy: action.fetchPolicy,
57
+ mirroredEnvironment: (_action$refetchEnviro = action.refetchEnvironment) !== null && _action$refetchEnviro !== void 0 ? _action$refetchEnviro : state.mirroredEnvironment,
58
+ onComplete: action.onComplete,
59
+ refetchEnvironment: action.refetchEnvironment,
60
+ refetchQuery: action.refetchQuery,
61
+ renderPolicy: action.renderPolicy
62
+ });
63
+ }
64
+
65
+ case 'reset':
66
+ {
67
+ return {
68
+ fetchPolicy: undefined,
69
+ mirroredEnvironment: action.environment,
70
+ mirroredFragmentIdentifier: action.fragmentIdentifier,
71
+ onComplete: undefined,
72
+ refetchQuery: null,
73
+ renderPolicy: undefined
74
+ };
75
+ }
76
+
77
+ default:
78
+ {
79
+ action.type;
80
+ throw new Error('useRefetchableFragmentNode: Unexpected action type');
81
+ }
82
+ }
83
+ }
84
+
85
+ function useRefetchableFragmentNode(fragmentNode, parentFragmentRef, componentDisplayName) {
86
+ var parentEnvironment = useRelayEnvironment();
87
+
88
+ var _getRefetchMetadata = getRefetchMetadata(fragmentNode, componentDisplayName),
89
+ refetchableRequest = _getRefetchMetadata.refetchableRequest,
90
+ fragmentRefPathInResponse = _getRefetchMetadata.fragmentRefPathInResponse,
91
+ identifierField = _getRefetchMetadata.identifierField;
92
+
93
+ var fragmentIdentifier = getFragmentIdentifier(fragmentNode, parentFragmentRef);
94
+
95
+ var _useReducer = useReducer(reducer, {
96
+ fetchPolicy: undefined,
97
+ mirroredEnvironment: parentEnvironment,
98
+ mirroredFragmentIdentifier: fragmentIdentifier,
99
+ onComplete: undefined,
100
+ refetchEnvironment: null,
101
+ refetchQuery: null,
102
+ renderPolicy: undefined
103
+ }),
104
+ refetchState = _useReducer[0],
105
+ dispatch = _useReducer[1];
106
+
107
+ var fetchPolicy = refetchState.fetchPolicy,
108
+ mirroredEnvironment = refetchState.mirroredEnvironment,
109
+ mirroredFragmentIdentifier = refetchState.mirroredFragmentIdentifier,
110
+ onComplete = refetchState.onComplete,
111
+ refetchEnvironment = refetchState.refetchEnvironment,
112
+ refetchQuery = refetchState.refetchQuery,
113
+ renderPolicy = refetchState.renderPolicy;
114
+ var environment = refetchEnvironment !== null && refetchEnvironment !== void 0 ? refetchEnvironment : parentEnvironment;
115
+ var QueryResource = getQueryResourceForEnvironment(environment);
116
+ var profilerContext = useContext(ProfilerContext);
117
+ var shouldReset = environment !== mirroredEnvironment || fragmentIdentifier !== mirroredFragmentIdentifier;
118
+
119
+ var _useQueryLoader = useQueryLoader(refetchableRequest),
120
+ queryRef = _useQueryLoader[0],
121
+ loadQuery = _useQueryLoader[1],
122
+ disposeQuery = _useQueryLoader[2];
123
+
124
+ var fragmentRef = parentFragmentRef;
125
+
126
+ if (shouldReset) {
127
+ dispatch({
128
+ type: 'reset',
129
+ environment: environment,
130
+ fragmentIdentifier: fragmentIdentifier
131
+ });
132
+ disposeQuery();
133
+ } else if (refetchQuery != null && queryRef != null) {
134
+ // If refetch was called, we expect to have a refetchQuery and queryRef
135
+ // in state, since both state updates to set the refetchQuery and the
136
+ // queryRef occur simultaneously.
137
+ // In this case, we need to read the refetched query data (potentially
138
+ // suspending if it's in flight), and extract the new fragment ref
139
+ // from the query in order read the current @refetchable fragment
140
+ // with the updated fragment owner as the new refetchQuery.
141
+ // Before observing the refetch, record the current ID and typename
142
+ // so that, if we are refetching existing data on
143
+ // a field that implements Node, after refetching we
144
+ // can validate that the received data is consistent
145
+ var debugPreviousIDAndTypename;
146
+
147
+ if (process.env.NODE_ENV !== "production") {
148
+ debugPreviousIDAndTypename = debugFunctions.getInitialIDAndType(refetchQuery.request.variables, fragmentRefPathInResponse, environment);
149
+ }
150
+
151
+ var handleQueryCompleted = function handleQueryCompleted(maybeError) {
152
+ onComplete && onComplete(maybeError !== null && maybeError !== void 0 ? maybeError : null);
153
+ }; // The queryRef.source obtained from useQueryLoader will be
154
+ // an observable we can consume /if/ a network request was
155
+ // started. Otherwise, given that QueryResource.prepare
156
+ // always expects an observable we fall back to a new network
157
+ // observable. Note however that if loadQuery did not make a network
158
+ // request, we don't expect to make one here, unless the state of
159
+ // the cache has changed between the call to refetch and this
160
+ // render.
161
+
162
+
163
+ var fetchObservable = queryRef.source != null ? queryRef.source : fetchQuery(environment, refetchQuery); // Now wwe can we read the refetch query here using the
164
+ // queryRef provided from useQueryLoader. Note that the
165
+ // network request is started during the call to refetch,
166
+ // but if the refetch query is still in flight, we will suspend
167
+ // at this point:
168
+
169
+ var queryResult = profilerContext.wrapPrepareQueryResource(function () {
170
+ return QueryResource.prepare(refetchQuery, fetchObservable, fetchPolicy, renderPolicy, {
171
+ error: handleQueryCompleted,
172
+ complete: function complete() {
173
+ // Validate that the type of the object we got back matches the type
174
+ // of the object already in the store
175
+ if (process.env.NODE_ENV !== "production") {
176
+ debugFunctions.checkSameTypeAfterRefetch(debugPreviousIDAndTypename, environment, fragmentNode, componentDisplayName);
177
+ }
178
+
179
+ handleQueryCompleted();
180
+ }
181
+ }, queryRef.fetchKey, profilerContext);
182
+ });
183
+ var queryData = readFragmentInternal(environment, queryResult.fragmentNode, queryResult.fragmentRef, componentDisplayName).data;
184
+ !(queryData != null) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: Expected to be able to read refetch query response. ' + "If you're seeing this, this is likely a bug in Relay.") : invariant(false) : void 0; // After reading/fetching the refetch query, we extract from the
185
+ // refetch query response the new fragment ref we need to use to read
186
+ // the fragment. The new fragment ref will point to the refetch query
187
+ // as its fragment owner.
188
+
189
+ var refetchedFragmentRef = getValueAtPath(queryData, fragmentRefPathInResponse);
190
+ fragmentRef = refetchedFragmentRef;
191
+
192
+ if (process.env.NODE_ENV !== "production") {
193
+ // Validate that the id of the object we got back matches the id
194
+ // we queried for in the variables.
195
+ // We do this during render instead of onComplete to make sure we are
196
+ // only validating the most recent refetch.
197
+ debugFunctions.checkSameIDAfterRefetch(debugPreviousIDAndTypename, fragmentRef, fragmentNode, componentDisplayName);
198
+ }
199
+ } // We read and subscribe to the fragment using useFragmentNode.
200
+ // If refetch was called, we read the fragment using the new computed
201
+ // fragment ref from the refetch query response; otherwise, we use the
202
+ // fragment ref passed by the caller as normal.
203
+
204
+
205
+ var fragmentData = useFragmentInternal(fragmentNode, fragmentRef, componentDisplayName);
206
+ var refetch = useRefetchFunction(componentDisplayName, dispatch, disposeQuery, fragmentData, fragmentIdentifier, fragmentNode, fragmentRefPathInResponse, identifierField, loadQuery, parentFragmentRef, refetchableRequest);
207
+ return {
208
+ fragmentData: fragmentData,
209
+ fragmentRef: fragmentRef,
210
+ refetch: refetch
211
+ };
212
+ }
213
+
214
+ function useRefetchFunction(componentDisplayName, dispatch, disposeQuery, fragmentData, fragmentIdentifier, fragmentNode, fragmentRefPathInResponse, identifierField, loadQuery, parentFragmentRef, refetchableRequest) {
215
+ var isMountedRef = useIsMountedRef();
216
+ var identifierValue = identifierField != null && fragmentData != null && typeof fragmentData === 'object' ? fragmentData[identifierField] : null;
217
+ return useCallback(function (providedRefetchVariables, options) {
218
+ // Bail out and warn if we're trying to refetch after the component
219
+ // has unmounted
220
+ if (isMountedRef.current !== true) {
221
+ process.env.NODE_ENV !== "production" ? warning(false, 'Relay: Unexpected call to `refetch` on unmounted component for fragment ' + '`%s` in `%s`. It looks like some instances of your component are ' + 'still trying to fetch data but they already unmounted. ' + 'Please make sure you clear all timers, intervals, ' + 'async calls, etc that may trigger a fetch.', fragmentNode.name, componentDisplayName) : void 0;
222
+ return {
223
+ dispose: function dispose() {}
224
+ };
225
+ }
226
+
227
+ if (parentFragmentRef == null) {
228
+ process.env.NODE_ENV !== "production" ? warning(false, 'Relay: Unexpected call to `refetch` while using a null fragment ref ' + 'for fragment `%s` in `%s`. When calling `refetch`, we expect ' + "initial fragment data to be non-null. Please make sure you're " + 'passing a valid fragment ref to `%s` before calling ' + '`refetch`, or make sure you pass all required variables to `refetch`.', fragmentNode.name, componentDisplayName, componentDisplayName) : void 0;
229
+ }
230
+
231
+ var refetchEnvironment = options === null || options === void 0 ? void 0 : options.__environment;
232
+ var fetchPolicy = options === null || options === void 0 ? void 0 : options.fetchPolicy;
233
+ var renderPolicy = options === null || options === void 0 ? void 0 : options.UNSTABLE_renderPolicy;
234
+ var onComplete = options === null || options === void 0 ? void 0 : options.onComplete;
235
+ var fragmentSelector = getSelector(fragmentNode, parentFragmentRef);
236
+ var parentVariables;
237
+ var fragmentVariables;
238
+
239
+ if (fragmentSelector == null) {
240
+ parentVariables = {};
241
+ fragmentVariables = {};
242
+ } else if (fragmentSelector.kind === 'PluralReaderSelector') {
243
+ var _fragmentSelector$sel, _fragmentSelector$sel2, _fragmentSelector$sel3, _fragmentSelector$sel4;
244
+
245
+ parentVariables = (_fragmentSelector$sel = (_fragmentSelector$sel2 = fragmentSelector.selectors[0]) === null || _fragmentSelector$sel2 === void 0 ? void 0 : _fragmentSelector$sel2.owner.variables) !== null && _fragmentSelector$sel !== void 0 ? _fragmentSelector$sel : {};
246
+ fragmentVariables = (_fragmentSelector$sel3 = (_fragmentSelector$sel4 = fragmentSelector.selectors[0]) === null || _fragmentSelector$sel4 === void 0 ? void 0 : _fragmentSelector$sel4.variables) !== null && _fragmentSelector$sel3 !== void 0 ? _fragmentSelector$sel3 : {};
247
+ } else {
248
+ parentVariables = fragmentSelector.owner.variables;
249
+ fragmentVariables = fragmentSelector.variables;
250
+ } // A user of `useRefetchableFragment()` may pass a subset of
251
+ // all variables required by the fragment when calling `refetch()`.
252
+ // We fill in any variables not passed by the call to `refetch()` with the
253
+ // variables from the original parent fragment owner.
254
+
255
+
256
+ var refetchVariables = (0, _objectSpread2["default"])((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, parentVariables), fragmentVariables), providedRefetchVariables); // If the query needs an identifier value ('id' or similar) and one
257
+ // was not explicitly provided, read it from the fragment data.
258
+
259
+ if (identifierField != null && !providedRefetchVariables.hasOwnProperty('id')) {
260
+ // @refetchable fragments are guaranteed to have an `id` selection
261
+ // if the type is Node, implements Node, or is @fetchable. Double-check
262
+ // that there actually is a value at runtime.
263
+ if (typeof identifierValue !== 'string') {
264
+ process.env.NODE_ENV !== "production" ? warning(false, 'Relay: Expected result to have a string ' + '`%s` in order to refetch, got `%s`.', identifierField, identifierValue) : void 0;
265
+ }
266
+
267
+ refetchVariables.id = identifierValue;
268
+ }
269
+
270
+ var refetchQuery = createOperationDescriptor(refetchableRequest, refetchVariables, {
271
+ force: true
272
+ }); // We call loadQuery which will start a network request if necessary
273
+ // and update the querRef from useQueryLoader.
274
+ // Note the following:
275
+ // - loadQuery will dispose of any previously refetched queries.
276
+ // - We use the variables extracted off the OperationDescriptor
277
+ // so that they have been filtered out to include only the
278
+ // variables actually declared in the query.
279
+
280
+ loadQuery(refetchQuery.request.variables, {
281
+ fetchPolicy: fetchPolicy,
282
+ __environment: refetchEnvironment,
283
+ __nameForWarning: 'refetch'
284
+ });
285
+ dispatch({
286
+ type: 'refetch',
287
+ fetchPolicy: fetchPolicy,
288
+ onComplete: onComplete,
289
+ refetchEnvironment: refetchEnvironment,
290
+ refetchQuery: refetchQuery,
291
+ renderPolicy: renderPolicy
292
+ });
293
+ return {
294
+ dispose: disposeQuery
295
+ };
296
+ }, // NOTE: We disable react-hooks-deps warning because:
297
+ // - We know fragmentRefPathInResponse is static, so it can be omitted from
298
+ // deps
299
+ // - We know fragmentNode is static, so it can be omitted from deps.
300
+ // - fragmentNode and parentFragmentRef are also captured by including
301
+ // fragmentIdentifier
302
+ // eslint-disable-next-line react-hooks/exhaustive-deps
303
+ [fragmentIdentifier, dispatch, disposeQuery, identifierValue, loadQuery]);
304
+ }
305
+
306
+ var debugFunctions;
307
+
308
+ if (process.env.NODE_ENV !== "production") {
309
+ debugFunctions = {
310
+ getInitialIDAndType: function getInitialIDAndType(memoRefetchVariables, fragmentRefPathInResponse, environment) {
311
+ var _require4 = require('relay-runtime'),
312
+ Record = _require4.Record;
313
+
314
+ var id = memoRefetchVariables === null || memoRefetchVariables === void 0 ? void 0 : memoRefetchVariables.id;
315
+
316
+ if (fragmentRefPathInResponse.length !== 1 || fragmentRefPathInResponse[0] !== 'node' || id == null) {
317
+ return null;
318
+ }
319
+
320
+ var recordSource = environment.getStore().getSource();
321
+ var record = recordSource.get(id);
322
+ var typename = record && Record.getType(record);
323
+
324
+ if (typename == null) {
325
+ return null;
326
+ }
327
+
328
+ return {
329
+ id: id,
330
+ typename: typename
331
+ };
332
+ },
333
+ checkSameTypeAfterRefetch: function checkSameTypeAfterRefetch(previousIDAndType, environment, fragmentNode, componentDisplayName) {
334
+ var _require5 = require('relay-runtime'),
335
+ Record = _require5.Record;
336
+
337
+ if (!previousIDAndType) {
338
+ return;
339
+ }
340
+
341
+ var recordSource = environment.getStore().getSource();
342
+ var record = recordSource.get(previousIDAndType.id);
343
+ var typename = record && Record.getType(record);
344
+
345
+ if (typename !== previousIDAndType.typename) {
346
+ process.env.NODE_ENV !== "production" ? warning(false, 'Relay: Call to `refetch` returned data with a different ' + '__typename: was `%s`, now `%s`, on `%s` in `%s`. ' + 'Please make sure the server correctly implements' + 'unique id requirement.', previousIDAndType.typename, typename, fragmentNode.name, componentDisplayName) : void 0;
347
+ }
348
+ },
349
+ checkSameIDAfterRefetch: function checkSameIDAfterRefetch(previousIDAndTypename, refetchedFragmentRef, fragmentNode, componentDisplayName) {
350
+ if (previousIDAndTypename == null) {
351
+ return;
352
+ }
353
+
354
+ var _require6 = require('relay-runtime'),
355
+ ID_KEY = _require6.ID_KEY; // $FlowExpectedError[incompatible-use]
356
+
357
+
358
+ var resultID = refetchedFragmentRef[ID_KEY];
359
+
360
+ if (resultID != null && resultID !== previousIDAndTypename.id) {
361
+ process.env.NODE_ENV !== "production" ? warning(false, 'Relay: Call to `refetch` returned a different id, expected ' + '`%s`, got `%s`, on `%s` in `%s`. ' + 'Please make sure the server correctly implements ' + 'unique id requirement.', resultID, previousIDAndTypename.id, fragmentNode.name, componentDisplayName) : void 0;
362
+ }
363
+ }
364
+ };
365
+ }
366
+
367
+ module.exports = useRefetchableFragmentNode;
@@ -0,0 +1,45 @@
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
+ *
9
+ * @format
10
+ */
11
+ 'use strict';
12
+
13
+ var useRefetchableFragmentInternal = require('./useRefetchableFragmentInternal_REACT_CACHE');
14
+
15
+ var useStaticFragmentNodeWarning = require('../useStaticFragmentNodeWarning');
16
+
17
+ var _require = require('react'),
18
+ useDebugValue = _require.useDebugValue;
19
+
20
+ var _require2 = require('relay-runtime'),
21
+ getFragment = _require2.getFragment;
22
+
23
+ function useRefetchableFragment(fragmentInput, fragmentRef) {
24
+ var fragmentNode = getFragment(fragmentInput);
25
+ useStaticFragmentNodeWarning(fragmentNode, 'first argument of useRefetchableFragment()');
26
+
27
+ var _useRefetchableFragme = useRefetchableFragmentInternal(fragmentNode, fragmentRef, 'useRefetchableFragment()'),
28
+ fragmentData = _useRefetchableFragme.fragmentData,
29
+ refetch = _useRefetchableFragme.refetch;
30
+
31
+ if (process.env.NODE_ENV !== "production") {
32
+ // eslint-disable-next-line react-hooks/rules-of-hooks
33
+ useDebugValue({
34
+ fragment: fragmentNode.name,
35
+ data: fragmentData
36
+ });
37
+ }
38
+ /* $FlowExpectedError[prop-missing] : Exposed options is a subset of internal
39
+ * options */
40
+
41
+
42
+ return [fragmentData, refetch];
43
+ }
44
+
45
+ module.exports = useRefetchableFragment;
@@ -8,15 +8,16 @@
8
8
  *
9
9
  * @format
10
10
  */
11
- // flowlint ambiguous-object-type:error
12
11
  'use strict';
13
12
 
14
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
13
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
15
14
 
16
15
  var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
17
16
 
18
17
  var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
19
18
 
19
+ var _excluded = ["disableStoreUpdates", "enableStoreUpdates"];
20
+
20
21
  var useLoadMoreFunction = require('./useLoadMoreFunction');
21
22
 
22
23
  var useRefetchableFragmentNode = require('./useRefetchableFragmentNode');
@@ -116,7 +117,7 @@ function useBlockingPaginationFragment(fragmentInput, parentFragmentRef) {
116
117
  function useLoadMore(args) {
117
118
  var disableStoreUpdates = args.disableStoreUpdates,
118
119
  enableStoreUpdates = args.enableStoreUpdates,
119
- loadMoreArgs = (0, _objectWithoutPropertiesLoose2["default"])(args, ["disableStoreUpdates", "enableStoreUpdates"]);
120
+ loadMoreArgs = (0, _objectWithoutPropertiesLoose2["default"])(args, _excluded);
120
121
 
121
122
  var _useState = useState(null),
122
123
  requestPromise = _useState[0],
@@ -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
+ * @emails oncall+relay
8
+ *
9
+ * @format
10
+ */
11
+ 'use strict';
12
+
13
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
14
+
15
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
16
+
17
+ var useLazyLoadQuery = require('./useLazyLoadQuery');
18
+ /**
19
+ * This hook can be used to render client-only queries.
20
+ * These queries are consist of queries for client-only data,
21
+ * schematized via local schema extensions and/or Relay resolvers.
22
+ */
23
+
24
+
25
+ function useClientQuery(gqlQuery, variables, options) {
26
+ // $FlowFixMe[incompatible-type] client queries can be used with useLazyLoadQuery, but only with `store-only` policy.
27
+ var query = gqlQuery;
28
+ return useLazyLoadQuery(query, variables, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, options), {}, {
29
+ fetchPolicy: 'store-only'
30
+ }));
31
+ }
32
+
33
+ module.exports = useClientQuery;
@@ -8,10 +8,9 @@
8
8
  *
9
9
  * @format
10
10
  */
11
- // flowlint ambiguous-object-type:error
12
11
  'use strict';
13
12
 
14
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
13
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
15
14
 
16
15
  var _createForOfIteratorHelper2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelper"));
17
16
 
@@ -8,7 +8,6 @@
8
8
  *
9
9
  * @format
10
10
  */
11
- // flowlint ambiguous-object-type:error
12
11
  'use strict';
13
12
 
14
13
  var _require = require('react'),
@@ -8,9 +8,10 @@
8
8
  *
9
9
  * @format
10
10
  */
11
- // flowlint ambiguous-object-type:error
12
11
  'use strict';
13
12
 
13
+ var HooksImplementation = require('./HooksImplementation');
14
+
14
15
  var _require = require('./loadQuery'),
15
16
  useTrackLoadQueryInRender = _require.useTrackLoadQueryInRender;
16
17
 
@@ -24,7 +25,7 @@ var _require2 = require('react'),
24
25
  var _require3 = require('relay-runtime'),
25
26
  getFragment = _require3.getFragment;
26
27
 
27
- function useFragment(fragment, key) {
28
+ function useFragment_LEGACY(fragment, key) {
28
29
  // We need to use this hook in order to be able to track if
29
30
  // loadQuery was called during render
30
31
  useTrackLoadQueryInRender();
@@ -45,4 +46,16 @@ function useFragment(fragment, key) {
45
46
  return data;
46
47
  }
47
48
 
49
+ function useFragment(fragment, key) {
50
+ var impl = HooksImplementation.get();
51
+
52
+ if (impl) {
53
+ // $FlowFixMe This is safe because impl.useFragment has the type of useFragment...
54
+ return impl.useFragment(fragment, key); // (i.e. type declared above, but not the supertype used in this function definition)
55
+ } else {
56
+ // eslint-disable-next-line react-hooks/rules-of-hooks
57
+ return useFragment_LEGACY(fragment, key);
58
+ }
59
+ }
60
+
48
61
  module.exports = useFragment;
@@ -8,7 +8,6 @@
8
8
  *
9
9
  * @format
10
10
  */
11
- // flowlint ambiguous-object-type:error
12
11
  'use strict';
13
12
 
14
13
  var _require = require('./FragmentResource'),
@@ -8,7 +8,6 @@
8
8
  *
9
9
  * @format
10
10
  */
11
- // flowlint ambiguous-object-type:error
12
11
  'use strict';
13
12
 
14
13
  var _require = require('react'),
@@ -8,7 +8,6 @@
8
8
  *
9
9
  * @format
10
10
  */
11
- // flowlint ambiguous-object-type:error
12
11
  'use strict';
13
12
 
14
13
  var _require = require('./loadQuery'),
@@ -21,7 +20,10 @@ var useMemoOperationDescriptor = require('./useMemoOperationDescriptor');
21
20
  var useRelayEnvironment = require('./useRelayEnvironment');
22
21
 
23
22
  var _require2 = require('relay-runtime'),
24
- fetchQuery = _require2.__internal.fetchQuery;
23
+ fetchQuery = _require2.__internal.fetchQuery; // This separate type export is only needed as long as we are injecting
24
+ // a separate hooks implementation in ./HooksImplementation -- it can
25
+ // be removed after we stop doing that.
26
+
25
27
 
26
28
  function useLazyLoadQuery(gqlQuery, variables, options) {
27
29
  // We need to use this hook in order to be able to track if
@@ -8,7 +8,6 @@
8
8
  *
9
9
  * @format
10
10
  */
11
- // flowlint ambiguous-object-type:error
12
11
  'use strict';
13
12
 
14
13
  var ProfilerContext = require('./ProfilerContext');
@@ -8,10 +8,9 @@
8
8
  *
9
9
  * @format
10
10
  */
11
- // flowlint ambiguous-object-type:error
12
11
  'use strict';
13
12
 
14
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
13
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
15
14
 
16
15
  var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
17
16
 
@@ -8,7 +8,6 @@
8
8
  *
9
9
  * @format
10
10
  */
11
- // flowlint ambiguous-object-type:error
12
11
  'use strict';
13
12
 
14
13
  var useMemoVariables = require('./useMemoVariables');
@@ -8,7 +8,6 @@
8
8
  *
9
9
  * @format
10
10
  */
11
- // flowlint ambiguous-object-type:error
12
11
  'use strict';
13
12
 
14
13
  var areEqual = require("fbjs/lib/areEqual");
@@ -8,10 +8,9 @@
8
8
  *
9
9
  * @format
10
10
  */
11
- // flowlint ambiguous-object-type:error
12
11
  'use strict';
13
12
 
14
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
13
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
15
14
 
16
15
  var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
17
16
 
@@ -63,6 +62,10 @@ function useMutation(mutation) {
63
62
  }
64
63
  }, [environment, isMountedRef, mutation]);
65
64
  var commit = useCallback(function (config) {
65
+ if (isMountedRef.current) {
66
+ setMutationInFlight(true);
67
+ }
68
+
66
69
  var disposable = commitMutationFn(environment, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, config), {}, {
67
70
  mutation: mutation,
68
71
  onCompleted: function onCompleted(response, errors) {
@@ -90,11 +93,6 @@ function useMutation(mutation) {
90
93
  }
91
94
  }));
92
95
  inFlightMutationsRef.current.add(disposable);
93
-
94
- if (isMountedRef.current) {
95
- setMutationInFlight(true);
96
- }
97
-
98
96
  return disposable;
99
97
  }, [cleanup, commitMutationFn, environment, isMountedRef, mutation]);
100
98
  return [commit, isMutationInFlight];