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,70 @@
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
+ * @emails oncall+relay
9
+ * @format
10
+ */
11
+
12
+ 'use strict';
13
+
14
+ import type {
15
+ CacheConfig,
16
+ FetchPolicy,
17
+ Query,
18
+ RenderPolicy,
19
+ Variables,
20
+ } from 'relay-runtime';
21
+
22
+ const {useTrackLoadQueryInRender} = require('../loadQuery');
23
+ const useMemoOperationDescriptor = require('../useMemoOperationDescriptor');
24
+ const useRelayEnvironment = require('../useRelayEnvironment');
25
+ const getQueryResultOrFetchQuery = require('./getQueryResultOrFetchQuery_REACT_CACHE');
26
+ const useFragmentInternal = require('./useFragmentInternal_REACT_CACHE');
27
+ const {useEffect} = require('react');
28
+
29
+ function useLazyLoadQuery_REACT_CACHE<TVariables: Variables, TData>(
30
+ gqlQuery: Query<TVariables, TData>,
31
+ variables: TVariables,
32
+ options?: {
33
+ fetchKey?: string | number,
34
+ fetchPolicy?: FetchPolicy,
35
+ networkCacheConfig?: CacheConfig,
36
+ UNSTABLE_renderPolicy?: RenderPolicy,
37
+ },
38
+ ): TData {
39
+ useTrackLoadQueryInRender();
40
+ const environment = useRelayEnvironment();
41
+
42
+ const queryOperationDescriptor = useMemoOperationDescriptor(
43
+ gqlQuery,
44
+ variables,
45
+ options?.networkCacheConfig ?? {force: true},
46
+ );
47
+
48
+ // Get the query going if needed -- this may suspend.
49
+ const [queryResult, effect] = getQueryResultOrFetchQuery(
50
+ environment,
51
+ queryOperationDescriptor,
52
+ {
53
+ fetchPolicy: options?.fetchPolicy,
54
+ renderPolicy: options?.UNSTABLE_renderPolicy,
55
+ fetchKey: options?.fetchKey,
56
+ },
57
+ );
58
+
59
+ useEffect(effect);
60
+
61
+ // Read the query's root fragment -- this may suspend.
62
+ const {fragmentNode, fragmentRef} = queryResult;
63
+ // $FlowExpectedError[incompatible-return] Is this a fixable incompatible-return?
64
+ return useFragmentInternal(fragmentNode, fragmentRef, 'useLazyLoadQuery()', {
65
+ fetchPolicy: options?.fetchPolicy,
66
+ networkCacheConfig: options?.networkCacheConfig,
67
+ });
68
+ }
69
+
70
+ module.exports = useLazyLoadQuery_REACT_CACHE;
@@ -0,0 +1,171 @@
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
+ 'use strict';
13
+
14
+ import type {LoadMoreFn, UseLoadMoreFunctionArgs} from '../useLoadMoreFunction';
15
+ import type {RefetchFnDynamic} from './useRefetchableFragmentInternal_REACT_CACHE';
16
+ import type {
17
+ FragmentType,
18
+ GraphQLResponse,
19
+ GraphQLTaggedNode,
20
+ Observer,
21
+ OperationType,
22
+ } from 'relay-runtime';
23
+
24
+ const useLoadMoreFunction = require('../useLoadMoreFunction');
25
+ const useStaticFragmentNodeWarning = require('../useStaticFragmentNodeWarning');
26
+ const useRefetchableFragmentInternal = require('./useRefetchableFragmentInternal_REACT_CACHE');
27
+ const {useCallback, useDebugValue, useState} = require('react');
28
+ const {
29
+ getFragment,
30
+ getFragmentIdentifier,
31
+ getPaginationMetadata,
32
+ } = require('relay-runtime');
33
+
34
+ export type ReturnType<TQuery: OperationType, TKey, TFragmentData> = {
35
+ data: TFragmentData,
36
+ loadNext: LoadMoreFn<TQuery>,
37
+ loadPrevious: LoadMoreFn<TQuery>,
38
+ hasNext: boolean,
39
+ hasPrevious: boolean,
40
+ isLoadingNext: boolean,
41
+ isLoadingPrevious: boolean,
42
+ refetch: RefetchFnDynamic<TQuery, TKey>,
43
+ };
44
+
45
+ function usePaginationFragment<
46
+ TQuery: OperationType,
47
+ TKey: ?{+$data?: mixed, +$fragmentSpreads: FragmentType, ...},
48
+ >(
49
+ fragmentInput: GraphQLTaggedNode,
50
+ parentFragmentRef: TKey,
51
+ ): ReturnType<
52
+ TQuery,
53
+ TKey,
54
+ // NOTE: This $Call ensures that the type of the returned data is either:
55
+ // - nullable if the provided ref type is nullable
56
+ // - non-nullable if the provided ref type is non-nullable
57
+ // prettier-ignore
58
+ $Call<
59
+ & (<TFragmentData>( { +$data?: TFragmentData, ... }) => TFragmentData)
60
+ & (<TFragmentData>(?{ +$data?: TFragmentData, ... }) => ?TFragmentData),
61
+ TKey,
62
+ >,
63
+ > {
64
+ const fragmentNode = getFragment(fragmentInput);
65
+ useStaticFragmentNodeWarning(
66
+ fragmentNode,
67
+ 'first argument of usePaginationFragment()',
68
+ );
69
+ const componentDisplayName = 'usePaginationFragment()';
70
+
71
+ const {
72
+ connectionPathInFragmentData,
73
+ paginationRequest,
74
+ paginationMetadata,
75
+ identifierField,
76
+ } = getPaginationMetadata(fragmentNode, componentDisplayName);
77
+
78
+ const {fragmentData, fragmentRef, refetch} = useRefetchableFragmentInternal<
79
+ TQuery,
80
+ TKey,
81
+ >(fragmentNode, parentFragmentRef, componentDisplayName);
82
+ const fragmentIdentifier = getFragmentIdentifier(fragmentNode, fragmentRef);
83
+
84
+ // Backward pagination
85
+ const [loadPrevious, hasPrevious, isLoadingPrevious, disposeFetchPrevious] =
86
+ useLoadMore<TQuery>({
87
+ componentDisplayName,
88
+ connectionPathInFragmentData,
89
+ direction: 'backward',
90
+ fragmentData,
91
+ fragmentIdentifier,
92
+ fragmentNode,
93
+ fragmentRef,
94
+ identifierField,
95
+ paginationMetadata,
96
+ paginationRequest,
97
+ });
98
+
99
+ // Forward pagination
100
+ const [loadNext, hasNext, isLoadingNext, disposeFetchNext] =
101
+ useLoadMore<TQuery>({
102
+ componentDisplayName,
103
+ connectionPathInFragmentData,
104
+ direction: 'forward',
105
+ fragmentData,
106
+ fragmentIdentifier,
107
+ fragmentNode,
108
+ fragmentRef,
109
+ identifierField,
110
+ paginationMetadata,
111
+ paginationRequest,
112
+ });
113
+
114
+ const refetchPagination: RefetchFnDynamic<TQuery, TKey> = useCallback(
115
+ (variables, options) => {
116
+ disposeFetchNext();
117
+ disposeFetchPrevious();
118
+ return refetch(variables, {...options, __environment: undefined});
119
+ },
120
+ [disposeFetchNext, disposeFetchPrevious, refetch],
121
+ );
122
+
123
+ if (__DEV__) {
124
+ // eslint-disable-next-line react-hooks/rules-of-hooks
125
+ useDebugValue({
126
+ fragment: fragmentNode.name,
127
+ data: fragmentData,
128
+ hasNext,
129
+ isLoadingNext,
130
+ hasPrevious,
131
+ isLoadingPrevious,
132
+ });
133
+ }
134
+ return {
135
+ data: fragmentData,
136
+ loadNext,
137
+ loadPrevious,
138
+ hasNext,
139
+ hasPrevious,
140
+ isLoadingNext,
141
+ isLoadingPrevious,
142
+ refetch: refetchPagination,
143
+ };
144
+ }
145
+
146
+ function useLoadMore<TQuery: OperationType>(
147
+ args: $Diff<
148
+ UseLoadMoreFunctionArgs,
149
+ {
150
+ observer: Observer<GraphQLResponse>,
151
+ onReset: () => void,
152
+ ...
153
+ },
154
+ >,
155
+ ): [LoadMoreFn<TQuery>, boolean, boolean, () => void] {
156
+ const [isLoadingMore, setIsLoadingMore] = useState(false);
157
+ const observer = {
158
+ start: () => setIsLoadingMore(true),
159
+ complete: () => setIsLoadingMore(false),
160
+ error: () => setIsLoadingMore(false),
161
+ };
162
+ const handleReset = () => setIsLoadingMore(false);
163
+ const [loadMore, hasMore, disposeFetch] = useLoadMoreFunction({
164
+ ...args,
165
+ observer,
166
+ onReset: handleReset,
167
+ });
168
+ return [loadMore, hasMore, isLoadingMore, disposeFetch];
169
+ }
170
+
171
+ module.exports = usePaginationFragment;
@@ -0,0 +1,151 @@
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
+ * @emails oncall+relay
9
+ * @format
10
+ */
11
+
12
+ 'use strict';
13
+
14
+ import type {PreloadedQuery} from '../EntryPointTypes.flow';
15
+ import type {
16
+ GraphQLTaggedNode,
17
+ OperationType,
18
+ RenderPolicy,
19
+ } from 'relay-runtime';
20
+
21
+ const {useTrackLoadQueryInRender} = require('../loadQuery');
22
+ const useMemoOperationDescriptor = require('../useMemoOperationDescriptor');
23
+ const useRelayEnvironment = require('../useRelayEnvironment');
24
+ const getQueryResultOrFetchQuery = require('./getQueryResultOrFetchQuery_REACT_CACHE');
25
+ const useFragmentInternal = require('./useFragmentInternal_REACT_CACHE');
26
+ const invariant = require('invariant');
27
+ const {useDebugValue, useEffect} = require('react');
28
+ const {
29
+ __internal: {fetchQueryDeduped, fetchQuery},
30
+ } = require('relay-runtime');
31
+ const warning = require('warning');
32
+
33
+ function usePreloadedQuery_REACT_CACHE<TQuery: OperationType>(
34
+ gqlQuery: GraphQLTaggedNode,
35
+ preloadedQuery: PreloadedQuery<TQuery>,
36
+ options?: {
37
+ UNSTABLE_renderPolicy?: RenderPolicy,
38
+ },
39
+ ): TQuery['response'] {
40
+ const environment = useRelayEnvironment();
41
+
42
+ useTrackLoadQueryInRender();
43
+
44
+ const {fetchKey, fetchPolicy, source, variables, networkCacheConfig} =
45
+ preloadedQuery;
46
+ const operation = useMemoOperationDescriptor(
47
+ gqlQuery,
48
+ variables,
49
+ networkCacheConfig,
50
+ );
51
+
52
+ let fetchObservable;
53
+ if (preloadedQuery.kind === 'PreloadedQuery_DEPRECATED') {
54
+ invariant(
55
+ operation.request.node.params.name === preloadedQuery.name,
56
+ 'usePreloadedQuery(): Expected data to be prefetched for query `%s`, ' +
57
+ 'got prefetch results for query `%s`.',
58
+ operation.request.node.params.name,
59
+ preloadedQuery.name,
60
+ );
61
+ fetchObservable = fetchQueryDeduped(
62
+ environment,
63
+ operation.request.identifier,
64
+ () => {
65
+ if (environment === preloadedQuery.environment && source != null) {
66
+ return environment.executeWithSource({operation, source});
67
+ } else {
68
+ return environment.execute({operation});
69
+ }
70
+ },
71
+ );
72
+ } else {
73
+ warning(
74
+ preloadedQuery.isDisposed === false,
75
+ 'usePreloadedQuery(): Expected preloadedQuery to not be disposed yet. ' +
76
+ 'This is because disposing the query marks it for future garbage ' +
77
+ 'collection, and as such query results may no longer be present in the Relay ' +
78
+ 'store. In the future, this will become a hard error.',
79
+ );
80
+ const fallbackFetchObservable = fetchQuery(environment, operation);
81
+ if (source != null && environment === preloadedQuery.environment) {
82
+ // If the source observable exists and the environments match, reuse
83
+ // the source observable.
84
+ // If the source observable happens to be empty, we need to fall back
85
+ // and re-execute and de-dupe the query (at render time).
86
+ fetchObservable = source.ifEmpty(fallbackFetchObservable);
87
+ } else if (environment !== preloadedQuery.environment) {
88
+ // If a call to loadQuery is made with a particular environment, and that
89
+ // preloaded query is passed to usePreloadedQuery in a different environment
90
+ // context, we cannot re-use the existing preloaded query.
91
+ // Instead, we need to fall back and re-execute and de-dupe the query with
92
+ // the new environment (at render time).
93
+ // TODO T68036756 track occurences of this warning and turn it into a hard error
94
+ warning(
95
+ false,
96
+ 'usePreloadedQuery(): usePreloadedQuery was passed a preloaded query ' +
97
+ 'that was created with a different environment than the one that is currently ' +
98
+ 'in context. In the future, this will become a hard error.',
99
+ );
100
+ fetchObservable = fallbackFetchObservable;
101
+ } else {
102
+ // if (source == null)
103
+ // If the source observable does not exist, we need to
104
+ // fall back and re-execute and de-dupe the query (at render time).
105
+ fetchObservable = fallbackFetchObservable;
106
+ }
107
+ }
108
+
109
+ // Get the query going if needed -- this may suspend.
110
+ const [queryResult, effect] = getQueryResultOrFetchQuery(
111
+ environment,
112
+ operation,
113
+ {
114
+ fetchPolicy,
115
+ renderPolicy: options?.UNSTABLE_renderPolicy,
116
+ fetchKey,
117
+ fetchObservable,
118
+ },
119
+ );
120
+
121
+ useEffect(effect);
122
+
123
+ // Read the query's root fragment -- this may suspend.
124
+ const {fragmentNode, fragmentRef} = queryResult;
125
+ // $FlowExpectedError[incompatible-return] Is this a fixable incompatible-return?
126
+ const data = useFragmentInternal(
127
+ fragmentNode,
128
+ fragmentRef,
129
+ 'usePreloadedQuery()',
130
+ {
131
+ fetchPolicy: fetchPolicy,
132
+ networkCacheConfig: networkCacheConfig,
133
+ },
134
+ );
135
+
136
+ if (__DEV__) {
137
+ // eslint-disable-next-line react-hooks/rules-of-hooks
138
+ useDebugValue({
139
+ query: preloadedQuery.name,
140
+ variables: preloadedQuery.variables,
141
+ data,
142
+ fetchKey,
143
+ fetchPolicy,
144
+ renderPolicy: options?.UNSTABLE_renderPolicy,
145
+ });
146
+ }
147
+
148
+ return data;
149
+ }
150
+
151
+ module.exports = usePreloadedQuery_REACT_CACHE;