react-relay 14.1.0 → 15.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. package/ReactRelayContainerUtils.js.flow +1 -0
  2. package/ReactRelayContext.js +1 -1
  3. package/ReactRelayContext.js.flow +1 -0
  4. package/ReactRelayFragmentContainer.js.flow +6 -2
  5. package/ReactRelayFragmentMockRenderer.js.flow +1 -0
  6. package/ReactRelayLocalQueryRenderer.js.flow +5 -3
  7. package/ReactRelayPaginationContainer.js.flow +21 -12
  8. package/ReactRelayQueryFetcher.js.flow +19 -5
  9. package/ReactRelayQueryRenderer.js.flow +32 -1
  10. package/ReactRelayQueryRendererContext.js.flow +1 -0
  11. package/ReactRelayRefetchContainer.js.flow +9 -5
  12. package/ReactRelayTestMocker.js.flow +3 -1
  13. package/ReactRelayTypes.js.flow +1 -0
  14. package/RelayContext.js.flow +1 -0
  15. package/__flowtests__/ReactRelayFragmentContainer-flowtest.js.flow +2 -1
  16. package/__flowtests__/ReactRelayPaginationContainer-flowtest.js.flow +1 -0
  17. package/__flowtests__/ReactRelayRefetchContainer-flowtest.js.flow +1 -0
  18. package/__flowtests__/RelayModern-flowtest.js.flow +1 -0
  19. package/__flowtests__/RelayModernFlowtest_badref.graphql.js.flow +1 -0
  20. package/__flowtests__/RelayModernFlowtest_notref.graphql.js.flow +1 -0
  21. package/__flowtests__/RelayModernFlowtest_user.graphql.js.flow +1 -0
  22. package/__flowtests__/RelayModernFlowtest_users.graphql.js.flow +1 -0
  23. package/__flowtests__/__generated__/ReactRelayFragmentContainerFlowtest_viewer.graphql.js.flow +3 -1
  24. package/__flowtests__/__generated__/ReactRelayFragmentContainerFlowtest_viewer2.graphql.js.flow +3 -1
  25. package/__flowtests__/__generated__/ReactRelayPaginationContainerFlowtestQuery.graphql.js.flow +4 -2
  26. package/__flowtests__/__generated__/ReactRelayPaginationContainerFlowtest_viewer.graphql.js.flow +3 -1
  27. package/__flowtests__/__generated__/ReactRelayRefetchContainerFlowtestQuery.graphql.js.flow +4 -2
  28. package/__flowtests__/__generated__/ReactRelayRefetchContainerFlowtest_viewer.graphql.js.flow +3 -1
  29. package/__flowtests__/__generated__/RelayModernFlowtest_badref.graphql.js.flow +4 -2
  30. package/__flowtests__/__generated__/RelayModernFlowtest_notref.graphql.js.flow +4 -2
  31. package/__flowtests__/__generated__/RelayModernFlowtest_user.graphql.js.flow +3 -1
  32. package/__flowtests__/__generated__/RelayModernFlowtest_users.graphql.js.flow +3 -1
  33. package/assertFragmentMap.js.flow +1 -0
  34. package/buildReactRelayContainer.js.flow +7 -5
  35. package/getRootVariablesForFragments.js.flow +1 -1
  36. package/hooks.js +1 -1
  37. package/hooks.js.flow +4 -0
  38. package/index.js +1 -1
  39. package/index.js.flow +4 -0
  40. package/isRelayEnvironment.js.flow +1 -0
  41. package/jest-react/enqueueTask.js.flow +1 -1
  42. package/jest-react/index.js.flow +1 -1
  43. package/jest-react/internalAct.js.flow +1 -1
  44. package/legacy.js +1 -1
  45. package/legacy.js.flow +1 -0
  46. package/lib/ReactRelayContainerUtils.js +2 -2
  47. package/lib/ReactRelayContext.js +3 -3
  48. package/lib/ReactRelayFragmentContainer.js +39 -66
  49. package/lib/ReactRelayFragmentMockRenderer.js +2 -3
  50. package/lib/ReactRelayLocalQueryRenderer.js +18 -30
  51. package/lib/ReactRelayPaginationContainer.js +66 -159
  52. package/lib/ReactRelayQueryFetcher.js +48 -74
  53. package/lib/ReactRelayQueryRenderer.js +62 -82
  54. package/lib/ReactRelayQueryRendererContext.js +2 -1
  55. package/lib/ReactRelayRefetchContainer.js +52 -99
  56. package/lib/ReactRelayTestMocker.js +32 -66
  57. package/lib/ReactRelayTypes.js +2 -0
  58. package/lib/RelayContext.js +4 -6
  59. package/lib/assertFragmentMap.js +3 -4
  60. package/lib/buildReactRelayContainer.js +10 -25
  61. package/lib/getRootVariablesForFragments.js +5 -8
  62. package/lib/hooks.js +5 -17
  63. package/lib/index.js +5 -24
  64. package/lib/isRelayEnvironment.js +5 -3
  65. package/lib/jest-react/enqueueTask.js +5 -9
  66. package/lib/jest-react/index.js +0 -1
  67. package/lib/jest-react/internalAct.js +9 -20
  68. package/lib/legacy.js +2 -7
  69. package/lib/multi-actor/ActorChange.js +2 -5
  70. package/lib/multi-actor/index.js +2 -1
  71. package/lib/multi-actor/useRelayActorEnvironment.js +4 -7
  72. package/lib/relay-hooks/EntryPointContainer.react.js +9 -14
  73. package/lib/relay-hooks/EntryPointTypes.flow.js +5 -2
  74. package/lib/relay-hooks/FragmentResource.js +102 -196
  75. package/lib/relay-hooks/HooksImplementation.js +2 -5
  76. package/lib/relay-hooks/InternalLogger.js +2 -2
  77. package/lib/relay-hooks/LRUCache.js +2 -19
  78. package/lib/relay-hooks/LazyLoadEntryPointContainer_DEPRECATED.react.js +33 -53
  79. package/lib/relay-hooks/MatchContainer.js +14 -22
  80. package/lib/relay-hooks/ProfilerContext.js +3 -2
  81. package/lib/relay-hooks/QueryResource.js +30 -99
  82. package/lib/relay-hooks/RelayEnvironmentProvider.js +5 -8
  83. package/lib/relay-hooks/SuspenseResource.js +8 -31
  84. package/lib/relay-hooks/loadEntryPoint.js +19 -31
  85. package/lib/relay-hooks/loadQuery.js +41 -77
  86. package/lib/relay-hooks/preloadQuery_DEPRECATED.js +10 -35
  87. package/lib/relay-hooks/prepareEntryPoint_DEPRECATED.js +9 -14
  88. package/lib/relay-hooks/react-cache/RelayReactCache.js +7 -11
  89. package/lib/relay-hooks/react-cache/getQueryResultOrFetchQuery_REACT_CACHE.js +24 -78
  90. package/lib/relay-hooks/react-cache/readFragmentInternal_REACT_CACHE.js +23 -56
  91. package/lib/relay-hooks/react-cache/useFragmentInternal_REACT_CACHE.js +65 -143
  92. package/lib/relay-hooks/react-cache/useFragment_REACT_CACHE.js +5 -14
  93. package/lib/relay-hooks/react-cache/useLazyLoadQuery_REACT_CACHE.js +17 -23
  94. package/lib/relay-hooks/react-cache/usePaginationFragment_REACT_CACHE.js +73 -74
  95. package/lib/relay-hooks/react-cache/usePreloadedQuery_REACT_CACHE.js +24 -38
  96. package/lib/relay-hooks/react-cache/useRefetchableFragmentInternal_REACT_CACHE.js +60 -102
  97. package/lib/relay-hooks/react-cache/useRefetchableFragment_REACT_CACHE.js +7 -15
  98. package/lib/relay-hooks/useBlockingPaginationFragment.js +71 -92
  99. package/lib/relay-hooks/useClientQuery.js +3 -6
  100. package/lib/relay-hooks/useEntryPointLoader.js +17 -36
  101. package/lib/relay-hooks/useFetchTrackingRef.js +10 -11
  102. package/lib/relay-hooks/useFragment.js +8 -18
  103. package/lib/relay-hooks/useFragmentNode.js +20 -31
  104. package/lib/relay-hooks/useIsMountedRef.js +4 -5
  105. package/lib/relay-hooks/useIsOperationNodeActive.js +8 -20
  106. package/lib/relay-hooks/useIsParentQueryActive.js +3 -6
  107. package/lib/relay-hooks/useLazyLoadQuery.js +6 -9
  108. package/lib/relay-hooks/useLazyLoadQueryNode.js +23 -33
  109. package/lib/relay-hooks/useLoadMoreFunction.js +45 -76
  110. package/lib/relay-hooks/useMemoOperationDescriptor.js +6 -14
  111. package/lib/relay-hooks/useMemoVariables.js +15 -33
  112. package/lib/relay-hooks/useMutation.js +8 -25
  113. package/lib/relay-hooks/usePaginationFragment.js +61 -76
  114. package/lib/relay-hooks/usePreloadedQuery.js +12 -29
  115. package/lib/relay-hooks/useQueryLoader.js +23 -47
  116. package/lib/relay-hooks/useRefetchableFragment.js +8 -18
  117. package/lib/relay-hooks/useRefetchableFragmentNode.js +64 -107
  118. package/lib/relay-hooks/useRelayEnvironment.js +4 -7
  119. package/lib/relay-hooks/useStaticFragmentNodeWarning.js +4 -7
  120. package/lib/relay-hooks/useSubscribeToInvalidationState.js +8 -8
  121. package/lib/relay-hooks/useSubscription.js +5 -9
  122. package/lib/relay-hooks/useUnsafeRef_DEPRECATED.js +29 -0
  123. package/multi-actor/ActorChange.js.flow +1 -1
  124. package/multi-actor/index.js.flow +1 -1
  125. package/multi-actor/useRelayActorEnvironment.js.flow +2 -2
  126. package/package.json +2 -2
  127. package/react-relay-hooks.js +2 -2
  128. package/react-relay-hooks.min.js +2 -2
  129. package/react-relay-legacy.js +2 -2
  130. package/react-relay-legacy.min.js +2 -2
  131. package/react-relay.js +2 -2
  132. package/react-relay.min.js +2 -2
  133. package/relay-hooks/EntryPointContainer.react.js.flow +1 -1
  134. package/relay-hooks/EntryPointTypes.flow.js.flow +7 -5
  135. package/relay-hooks/FragmentResource.js.flow +34 -8
  136. package/relay-hooks/HooksImplementation.js.flow +1 -1
  137. package/relay-hooks/InternalLogger.js.flow +1 -1
  138. package/relay-hooks/LRUCache.js.flow +1 -1
  139. package/relay-hooks/LazyLoadEntryPointContainer_DEPRECATED.react.js.flow +15 -8
  140. package/relay-hooks/MatchContainer.js.flow +1 -1
  141. package/relay-hooks/ProfilerContext.js.flow +1 -1
  142. package/relay-hooks/QueryResource.js.flow +25 -5
  143. package/relay-hooks/RelayEnvironmentProvider.js.flow +2 -2
  144. package/relay-hooks/SuspenseResource.js.flow +1 -1
  145. package/relay-hooks/__flowtests__/EntryPointTypes/EntryPointElementConfig-flowtest.js.flow +3 -1
  146. package/relay-hooks/__flowtests__/EntryPointTypes/NestedEntrypoints-flowtest.js.flow +2 -2
  147. package/relay-hooks/__flowtests__/__generated__/useFragmentFlowtest_user.graphql.js.flow +3 -1
  148. package/relay-hooks/__flowtests__/__generated__/useFragmentFlowtest_users.graphql.js.flow +3 -1
  149. package/relay-hooks/__flowtests__/useBlockingPaginationFragment-flowtest.js.flow +35 -33
  150. package/relay-hooks/__flowtests__/useFragment-flowtest.js.flow +1 -1
  151. package/relay-hooks/__flowtests__/usePaginationFragment-flowtest.js.flow +33 -32
  152. package/relay-hooks/__flowtests__/useRefetchableFragment-flowtest.js.flow +18 -18
  153. package/relay-hooks/__flowtests__/utils.js.flow +13 -2
  154. package/relay-hooks/loadEntryPoint.js.flow +11 -6
  155. package/relay-hooks/loadQuery.js.flow +11 -7
  156. package/relay-hooks/preloadQuery_DEPRECATED.js.flow +5 -6
  157. package/relay-hooks/prepareEntryPoint_DEPRECATED.js.flow +13 -8
  158. package/relay-hooks/react-cache/RelayReactCache.js.flow +1 -1
  159. package/relay-hooks/react-cache/getQueryResultOrFetchQuery_REACT_CACHE.js.flow +4 -4
  160. package/relay-hooks/react-cache/readFragmentInternal_REACT_CACHE.js.flow +3 -3
  161. package/relay-hooks/react-cache/useFragmentInternal_REACT_CACHE.js.flow +4 -4
  162. package/relay-hooks/react-cache/useFragment_REACT_CACHE.js.flow +1 -1
  163. package/relay-hooks/react-cache/useLazyLoadQuery_REACT_CACHE.js.flow +1 -1
  164. package/relay-hooks/react-cache/usePaginationFragment_REACT_CACHE.js.flow +41 -22
  165. package/relay-hooks/react-cache/usePreloadedQuery_REACT_CACHE.js.flow +1 -2
  166. package/relay-hooks/react-cache/useRefetchableFragmentInternal_REACT_CACHE.js.flow +11 -5
  167. package/relay-hooks/react-cache/useRefetchableFragment_REACT_CACHE.js.flow +2 -2
  168. package/relay-hooks/useBlockingPaginationFragment.js.flow +85 -56
  169. package/relay-hooks/useClientQuery.js.flow +1 -1
  170. package/relay-hooks/useEntryPointLoader.js.flow +10 -6
  171. package/relay-hooks/useFetchTrackingRef.js.flow +5 -4
  172. package/relay-hooks/useFragment.js.flow +2 -2
  173. package/relay-hooks/useFragmentNode.js.flow +5 -4
  174. package/relay-hooks/useIsMountedRef.js.flow +1 -1
  175. package/relay-hooks/useIsOperationNodeActive.js.flow +1 -1
  176. package/relay-hooks/useIsParentQueryActive.js.flow +1 -1
  177. package/relay-hooks/useLazyLoadQuery.js.flow +2 -2
  178. package/relay-hooks/useLazyLoadQueryNode.js.flow +2 -2
  179. package/relay-hooks/useLoadMoreFunction.js.flow +14 -9
  180. package/relay-hooks/useMemoOperationDescriptor.js.flow +3 -3
  181. package/relay-hooks/useMemoVariables.js.flow +13 -29
  182. package/relay-hooks/useMutation.js.flow +4 -4
  183. package/relay-hooks/usePaginationFragment.js.flow +53 -46
  184. package/relay-hooks/usePreloadedQuery.js.flow +47 -22
  185. package/relay-hooks/useQueryLoader.js.flow +85 -22
  186. package/relay-hooks/useRefetchableFragment.js.flow +64 -33
  187. package/relay-hooks/useRefetchableFragmentNode.js.flow +10 -4
  188. package/relay-hooks/useRelayEnvironment.js.flow +2 -2
  189. package/relay-hooks/useStaticFragmentNodeWarning.js.flow +3 -3
  190. package/relay-hooks/useSubscribeToInvalidationState.js.flow +2 -2
  191. package/relay-hooks/useSubscription.js.flow +1 -1
  192. package/relay-hooks/useUnsafeRef_DEPRECATED.js.flow +25 -0
  193. package/lib/readContext.js +0 -27
  194. package/readContext.js.flow +0 -29
@@ -4,21 +4,22 @@
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
- * @emails oncall+relay
8
7
  * @flow strict-local
9
8
  * @format
9
+ * @oncall relay
10
10
  */
11
11
 
12
12
  'use strict';
13
13
 
14
14
  import type {LoadMoreFn, UseLoadMoreFunctionArgs} from './useLoadMoreFunction';
15
- import type {RefetchFnDynamic} from './useRefetchableFragmentNode';
15
+ import type {RefetchFn} from './useRefetchableFragment';
16
+ import type {Options} from './useRefetchableFragmentNode';
16
17
  import type {
17
18
  FragmentType,
18
19
  GraphQLResponse,
19
- GraphQLTaggedNode,
20
20
  Observer,
21
- OperationType,
21
+ RefetchableFragment,
22
+ Variables,
22
23
  } from 'relay-runtime';
23
24
 
24
25
  const HooksImplementation = require('./HooksImplementation');
@@ -32,43 +33,28 @@ const {
32
33
  getPaginationMetadata,
33
34
  } = require('relay-runtime');
34
35
 
35
- export type ReturnType<TQuery: OperationType, TKey> = {
36
- // NOTE: This $Call ensures that the type of the returned data is either:
37
- // - nullable if the provided ref type is nullable
38
- // - non-nullable if the provided ref type is non-nullable
39
- // prettier-ignore
40
- data: $Call<
41
- & (<TFragmentData>( { +$data?: TFragmentData, ... }) => TFragmentData)
42
- & (<TFragmentData>(?{ +$data?: TFragmentData, ... }) => ?TFragmentData),
43
- TKey,
44
- >,
45
- loadNext: LoadMoreFn<TQuery>,
46
- loadPrevious: LoadMoreFn<TQuery>,
47
- hasNext: boolean,
48
- hasPrevious: boolean,
49
- isLoadingNext: boolean,
50
- isLoadingPrevious: boolean,
51
- refetch: RefetchFnDynamic<TQuery, TKey>,
52
- };
53
-
54
36
  // This separate type export is only needed as long as we are injecting
55
37
  // a separate hooks implementation in ./HooksImplementation -- it can
56
38
  // be removed after we stop doing that.
57
39
  export type UsePaginationFragmentType = <
58
- TQuery: OperationType,
59
- TKey: ?{+$data?: mixed, +$fragmentSpreads: FragmentType, ...},
40
+ TFragmentType: FragmentType,
41
+ TVariables: Variables,
42
+ TData,
43
+ TKey: ?{+$fragmentSpreads: TFragmentType, ...},
60
44
  >(
61
- fragmentInput: GraphQLTaggedNode,
45
+ fragmentInput: RefetchableFragment<TFragmentType, TData, TVariables>,
62
46
  parentFragmentRef: TKey,
63
- ) => ReturnType<TQuery, TKey>;
47
+ ) => ReturnType<TVariables, TData, TKey>;
64
48
 
65
49
  function usePaginationFragment_LEGACY<
66
- TQuery: OperationType,
67
- TKey: ?{+$data?: mixed, +$fragmentSpreads: FragmentType, ...},
50
+ TFragmentType: FragmentType,
51
+ TVariables: Variables,
52
+ TData,
53
+ TKey: ?{+$fragmentSpreads: TFragmentType, ...},
68
54
  >(
69
- fragmentInput: GraphQLTaggedNode,
55
+ fragmentInput: RefetchableFragment<TFragmentType, TData, TVariables>,
70
56
  parentFragmentRef: TKey,
71
- ): ReturnType<TQuery, TKey> {
57
+ ): ReturnType<TVariables, TData, TKey> {
72
58
  const fragmentNode = getFragment(fragmentInput);
73
59
  useStaticFragmentNodeWarning(
74
60
  fragmentNode,
@@ -84,14 +70,14 @@ function usePaginationFragment_LEGACY<
84
70
  } = getPaginationMetadata(fragmentNode, componentDisplayName);
85
71
 
86
72
  const {fragmentData, fragmentRef, refetch} = useRefetchableFragmentNode<
87
- TQuery,
88
- TKey,
73
+ $FlowFixMe,
74
+ $FlowFixMe,
89
75
  >(fragmentNode, parentFragmentRef, componentDisplayName);
90
76
  const fragmentIdentifier = getFragmentIdentifier(fragmentNode, fragmentRef);
91
77
 
92
78
  // Backward pagination
93
79
  const [loadPrevious, hasPrevious, isLoadingPrevious, disposeFetchPrevious] =
94
- useLoadMore<TQuery>({
80
+ useLoadMore<TVariables>({
95
81
  componentDisplayName,
96
82
  connectionPathInFragmentData,
97
83
  direction: 'backward',
@@ -106,7 +92,7 @@ function usePaginationFragment_LEGACY<
106
92
 
107
93
  // Forward pagination
108
94
  const [loadNext, hasNext, isLoadingNext, disposeFetchNext] =
109
- useLoadMore<TQuery>({
95
+ useLoadMore<TVariables>({
110
96
  componentDisplayName,
111
97
  connectionPathInFragmentData,
112
98
  direction: 'forward',
@@ -119,8 +105,8 @@ function usePaginationFragment_LEGACY<
119
105
  paginationRequest,
120
106
  });
121
107
 
122
- const refetchPagination: RefetchFnDynamic<TQuery, TKey> = useCallback(
123
- (variables, options) => {
108
+ const refetchPagination: RefetchFn<TVariables, TKey> = useCallback(
109
+ (variables: TVariables, options: void | Options) => {
124
110
  disposeFetchNext();
125
111
  disposeFetchPrevious();
126
112
  return refetch(variables, {...options, __environment: undefined});
@@ -140,7 +126,7 @@ function usePaginationFragment_LEGACY<
140
126
  });
141
127
  }
142
128
  return {
143
- data: fragmentData,
129
+ data: (fragmentData: $FlowFixMe),
144
130
  loadNext,
145
131
  loadPrevious,
146
132
  hasNext,
@@ -151,7 +137,7 @@ function usePaginationFragment_LEGACY<
151
137
  };
152
138
  }
153
139
 
154
- function useLoadMore<TQuery: OperationType>(
140
+ function useLoadMore<TVariables: Variables>(
155
141
  args: $Diff<
156
142
  UseLoadMoreFunctionArgs,
157
143
  {
@@ -160,7 +146,7 @@ function useLoadMore<TQuery: OperationType>(
160
146
  ...
161
147
  },
162
148
  >,
163
- ): [LoadMoreFn<TQuery>, boolean, boolean, () => void] {
149
+ ): [LoadMoreFn<TVariables>, boolean, boolean, () => void] {
164
150
  const [isLoadingMore, setIsLoadingMore] = useState(false);
165
151
  const observer = {
166
152
  start: () => setIsLoadingMore(true),
@@ -168,7 +154,7 @@ function useLoadMore<TQuery: OperationType>(
168
154
  error: () => setIsLoadingMore(false),
169
155
  };
170
156
  const handleReset = () => setIsLoadingMore(false);
171
- const [loadMore, hasMore, disposeFetch] = useLoadMoreFunction({
157
+ const [loadMore, hasMore, disposeFetch] = useLoadMoreFunction<TVariables>({
172
158
  ...args,
173
159
  observer,
174
160
  onReset: handleReset,
@@ -176,16 +162,37 @@ function useLoadMore<TQuery: OperationType>(
176
162
  return [loadMore, hasMore, isLoadingMore, disposeFetch];
177
163
  }
178
164
 
165
+ export type ReturnType<TVariables, TData, TKey> = {
166
+ // NOTE: This $Call ensures that the type of the returned data is either:
167
+ // - nullable if the provided ref type is nullable
168
+ // - non-nullable if the provided ref type is non-nullable
169
+ // prettier-ignore
170
+ data: $Call<
171
+ & (<TFragmentType>( { +$fragmentSpreads: TFragmentType, ... }) => TData)
172
+ & (<TFragmentType>(?{ +$fragmentSpreads: TFragmentType, ... }) => ?TData),
173
+ TKey,
174
+ >,
175
+ loadNext: LoadMoreFn<TVariables>,
176
+ loadPrevious: LoadMoreFn<TVariables>,
177
+ hasNext: boolean,
178
+ hasPrevious: boolean,
179
+ isLoadingNext: boolean,
180
+ isLoadingPrevious: boolean,
181
+ refetch: RefetchFn<TVariables, TKey>,
182
+ };
183
+
179
184
  function usePaginationFragment<
180
- TQuery: OperationType,
181
- TKey: ?{+$data?: mixed, +$fragmentSpreads: FragmentType, ...},
185
+ TFragmentType: FragmentType,
186
+ TVariables: Variables,
187
+ TData,
188
+ TKey: ?{+$fragmentSpreads: TFragmentType, ...},
182
189
  >(
183
- fragmentInput: GraphQLTaggedNode,
190
+ fragmentInput: RefetchableFragment<TFragmentType, TData, TVariables>,
184
191
  parentFragmentRef: TKey,
185
- ): ReturnType<TQuery, TKey> {
192
+ ): ReturnType<TVariables, TData, TKey> {
186
193
  const impl = HooksImplementation.get();
187
194
  if (impl) {
188
- return impl.usePaginationFragment<TQuery, TKey>(
195
+ return impl.usePaginationFragment<TFragmentType, TVariables, TData, TKey>(
189
196
  fragmentInput,
190
197
  parentFragmentRef,
191
198
  );
@@ -4,19 +4,19 @@
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
- * @emails oncall+relay
8
7
  * @flow strict-local
9
8
  * @format
9
+ * @oncall relay
10
10
  */
11
11
 
12
12
  'use strict';
13
13
 
14
- import type {PreloadedQuery} from './EntryPointTypes.flow';
15
14
  import type {
16
- GraphQLTaggedNode,
17
- OperationType,
18
- RenderPolicy,
19
- } from 'relay-runtime';
15
+ EnvironmentProviderOptions,
16
+ PreloadedQueryInner,
17
+ PreloadedQueryInner_DEPRECATED,
18
+ } from './EntryPointTypes.flow';
19
+ import type {Query, RenderPolicy, Variables} from 'relay-runtime';
20
20
 
21
21
  const {useTrackLoadQueryInRender} = require('./loadQuery');
22
22
  const useLazyLoadQueryNode = require('./useLazyLoadQueryNode');
@@ -29,24 +29,44 @@ const {
29
29
  } = require('relay-runtime');
30
30
  const warning = require('warning');
31
31
 
32
- // This separate type export is only needed as long as we are injecting
33
- // a separate hooks implementation in ./HooksImplementation -- it can
34
- // be removed after we stop doing that.
35
- export type UsePreloadedQueryHookType = <TQuery: OperationType>(
36
- gqlQuery: GraphQLTaggedNode,
37
- preloadedQuery: PreloadedQuery<TQuery>,
38
- options?: {
39
- UNSTABLE_renderPolicy?: RenderPolicy,
40
- },
41
- ) => TQuery['response'];
32
+ type PreloadedQuery<
33
+ TVariables: Variables,
34
+ TData,
35
+ TRawResponse,
36
+ TEnvironmentProviderOptions = EnvironmentProviderOptions,
37
+ > =
38
+ | PreloadedQueryInner_DEPRECATED<
39
+ {
40
+ variables: TVariables,
41
+ response: TData,
42
+ rawResponse?: TRawResponse,
43
+ },
44
+ TEnvironmentProviderOptions,
45
+ >
46
+ | PreloadedQueryInner<
47
+ {
48
+ variables: TVariables,
49
+ response: TData,
50
+ rawResponse?: TRawResponse,
51
+ },
52
+ TEnvironmentProviderOptions,
53
+ >;
42
54
 
43
- function usePreloadedQuery<TQuery: OperationType>(
44
- gqlQuery: GraphQLTaggedNode,
45
- preloadedQuery: PreloadedQuery<TQuery>,
55
+ function usePreloadedQuery<
56
+ TVariables: Variables,
57
+ TData,
58
+ TRawResponse: ?{...} = void,
59
+ >(
60
+ gqlQuery: Query<TVariables, TData, TRawResponse>,
61
+ preloadedQuery: PreloadedQuery<
62
+ TVariables,
63
+ TData,
64
+ $NonMaybeType<TRawResponse>,
65
+ >,
46
66
  options?: {
47
67
  UNSTABLE_renderPolicy?: RenderPolicy,
48
68
  },
49
- ): TQuery['response'] {
69
+ ): TData {
50
70
  // We need to use this hook in order to be able to track if
51
71
  // loadQuery was called during render
52
72
  useTrackLoadQueryInRender();
@@ -136,7 +156,12 @@ function usePreloadedQuery<TQuery: OperationType>(
136
156
  };
137
157
  }
138
158
 
139
- const data = useLazyLoadQueryNode(useLazyLoadQueryNodeParams);
159
+ const data = useLazyLoadQueryNode<{
160
+ variables: TVariables,
161
+ response: TData,
162
+ rawResponse?: $NonMaybeType<TRawResponse>,
163
+ }>(useLazyLoadQueryNodeParams);
164
+
140
165
  if (__DEV__) {
141
166
  // eslint-disable-next-line react-hooks/rules-of-hooks
142
167
  useDebugValue({
@@ -151,4 +176,4 @@ function usePreloadedQuery<TQuery: OperationType>(
151
176
  return data;
152
177
  }
153
178
 
154
- module.exports = (usePreloadedQuery: UsePreloadedQueryHookType);
179
+ module.exports = usePreloadedQuery;
@@ -4,9 +4,9 @@
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
- * @emails oncall+relay
8
7
  * @flow strict-local
9
8
  * @format
9
+ * @oncall relay
10
10
  */
11
11
 
12
12
  'use strict';
@@ -17,9 +17,10 @@ import type {
17
17
  PreloadedQuery,
18
18
  } from './EntryPointTypes.flow';
19
19
  import type {
20
- GraphQLTaggedNode,
21
20
  IEnvironment,
22
21
  OperationType,
22
+ Query,
23
+ Variables,
23
24
  } from 'relay-runtime';
24
25
 
25
26
  const {loadQuery, useTrackLoadQueryInRender} = require('./loadQuery');
@@ -38,12 +39,6 @@ export type UseQueryLoaderLoadQueryOptions = $ReadOnly<{
38
39
  +__environment?: ?IEnvironment,
39
40
  }>;
40
41
 
41
- type UseQueryLoaderHookReturnType<TQuery: OperationType> = [
42
- ?PreloadedQuery<TQuery>,
43
- LoaderFn<TQuery>,
44
- () => void,
45
- ];
46
-
47
42
  // NullQueryReference needs to implement referential equality,
48
43
  // so that multiple NullQueryReferences can be in the same set
49
44
  // (corresponding to multiple calls to disposeQuery).
@@ -52,20 +47,91 @@ type NullQueryReference = {
52
47
  };
53
48
  const initialNullQueryReferenceState = {kind: 'NullQueryReference'};
54
49
 
55
- function requestIsLiveQuery<TQuery: OperationType>(
56
- preloadableRequest: GraphQLTaggedNode | PreloadableConcreteRequest<TQuery>,
50
+ function requestIsLiveQuery<
51
+ TVariables: Variables,
52
+ TData,
53
+ TRawResponse: ?{...} = void,
54
+ TQuery: OperationType = {
55
+ response: TData,
56
+ variables: TVariables,
57
+ rawResponse?: $NonMaybeType<TRawResponse>,
58
+ },
59
+ >(
60
+ preloadableRequest:
61
+ | Query<TVariables, TData, TRawResponse>
62
+ | PreloadableConcreteRequest<TQuery>,
57
63
  ): boolean {
58
64
  if (preloadableRequest.kind === 'PreloadableConcreteRequest') {
59
- return (preloadableRequest: $FlowFixMe).params.metadata.live !== undefined;
65
+ return preloadableRequest.params.metadata.live !== undefined;
60
66
  }
61
67
  const request = getRequest(preloadableRequest);
62
68
  return request.params.metadata.live !== undefined;
63
69
  }
64
70
 
65
- function useQueryLoader<TQuery: OperationType>(
66
- preloadableRequest: GraphQLTaggedNode | PreloadableConcreteRequest<TQuery>,
71
+ type UseQueryLoaderHookReturnType<
72
+ TVariables: Variables,
73
+ TData,
74
+ TRawResponse: ?{...} = void,
75
+ > = [
76
+ ?PreloadedQuery<{
77
+ response: TData,
78
+ variables: TVariables,
79
+ rawResponse?: $NonMaybeType<TRawResponse>,
80
+ }>,
81
+ (variables: TVariables, options?: UseQueryLoaderLoadQueryOptions) => void,
82
+ () => void,
83
+ ];
84
+
85
+ type ExtractVariablesType = <T>({+variables: T, ...}) => T;
86
+ type ExtractResponseType = <T>({+response: T, ...}) => T;
87
+
88
+ declare function useQueryLoader<
89
+ TVariables: Variables,
90
+ TData,
91
+ TRawResponse: ?{...} = void,
92
+ >(
93
+ preloadableRequest: Query<TVariables, TData, TRawResponse>,
94
+ ): UseQueryLoaderHookReturnType<TVariables, TData>;
95
+
96
+ declare function useQueryLoader<
97
+ TVariables: Variables,
98
+ TData,
99
+ TRawResponse: ?{...} = void,
100
+ >(
101
+ preloadableRequest: Query<TVariables, TData, TRawResponse>,
102
+ initialQueryReference: ?PreloadedQuery<{
103
+ response: TData,
104
+ variables: TVariables,
105
+ rawResponse?: $NonMaybeType<TRawResponse>,
106
+ }>,
107
+ ): UseQueryLoaderHookReturnType<TVariables, TData>;
108
+
109
+ declare function useQueryLoader<TQuery: OperationType>(
110
+ preloadableRequest: PreloadableConcreteRequest<TQuery>,
67
111
  initialQueryReference?: ?PreloadedQuery<TQuery>,
68
- ): UseQueryLoaderHookReturnType<TQuery> {
112
+ ): UseQueryLoaderHookReturnType<
113
+ $Call<ExtractVariablesType, TQuery>,
114
+ $Call<ExtractResponseType, TQuery>,
115
+ >;
116
+
117
+ function useQueryLoader<
118
+ TVariables: Variables,
119
+ TData,
120
+ TRawResponse: ?{...} = void,
121
+ >(
122
+ preloadableRequest: Query<TVariables, TData, TRawResponse>,
123
+ initialQueryReference?: ?PreloadedQuery<{
124
+ response: TData,
125
+ variables: TVariables,
126
+ rawResponse?: $NonMaybeType<TRawResponse>,
127
+ }>,
128
+ ): UseQueryLoaderHookReturnType<TVariables, TData> {
129
+ type QueryType = {
130
+ response: TData,
131
+ variables: TVariables,
132
+ rawResponse?: $NonMaybeType<TRawResponse>,
133
+ };
134
+
69
135
  /**
70
136
  * We want to always call `queryReference.dispose()` for every call to
71
137
  * `setQueryReference(loadQuery(...))` so that no leaks of data in Relay stores
@@ -94,15 +160,15 @@ function useQueryLoader<TQuery: OperationType>(
94
160
 
95
161
  const isMountedRef = useIsMountedRef();
96
162
  const undisposedQueryReferencesRef = useRef<
97
- Set<PreloadedQuery<TQuery> | NullQueryReference>,
163
+ Set<PreloadedQuery<QueryType> | NullQueryReference>,
98
164
  >(new Set([initialQueryReferenceInternal]));
99
165
 
100
166
  const [queryReference, setQueryReference] = useState<
101
- PreloadedQuery<TQuery> | NullQueryReference,
167
+ PreloadedQuery<QueryType> | NullQueryReference,
102
168
  >(() => initialQueryReferenceInternal);
103
169
 
104
170
  const [previousInitialQueryReference, setPreviousInitialQueryReference] =
105
- useState<PreloadedQuery<TQuery> | NullQueryReference>(
171
+ useState<PreloadedQuery<QueryType> | NullQueryReference>(
106
172
  () => initialQueryReferenceInternal,
107
173
  );
108
174
 
@@ -125,10 +191,7 @@ function useQueryLoader<TQuery: OperationType>(
125
191
  }, [isMountedRef]);
126
192
 
127
193
  const queryLoaderCallback = useCallback(
128
- (
129
- variables: TQuery['variables'],
130
- options?: ?UseQueryLoaderLoadQueryOptions,
131
- ) => {
194
+ (variables: TVariables, options?: ?UseQueryLoaderLoadQueryOptions) => {
132
195
  const mergedOptions: ?UseQueryLoaderLoadQueryOptions =
133
196
  options != null && options.hasOwnProperty('__environment')
134
197
  ? {
@@ -138,7 +201,7 @@ function useQueryLoader<TQuery: OperationType>(
138
201
  }
139
202
  : options;
140
203
  if (isMountedRef.current) {
141
- const updatedQueryReference = loadQuery(
204
+ const updatedQueryReference = loadQuery<QueryType>(
142
205
  options?.__environment ?? environment,
143
206
  preloadableRequest,
144
207
  variables,
@@ -4,18 +4,19 @@
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
- * @emails oncall+relay
8
7
  * @flow strict-local
9
8
  * @format
9
+ * @oncall relay
10
10
  */
11
11
 
12
12
  'use strict';
13
13
 
14
- import type {RefetchFnDynamic} from './useRefetchableFragmentNode';
14
+ import type {Options} from './useRefetchableFragmentNode';
15
15
  import type {
16
+ Disposable,
16
17
  FragmentType,
17
- GraphQLTaggedNode,
18
- OperationType,
18
+ RefetchableFragment,
19
+ Variables,
19
20
  } from 'relay-runtime';
20
21
 
21
22
  const HooksImplementation = require('./HooksImplementation');
@@ -24,66 +25,96 @@ const useStaticFragmentNodeWarning = require('./useStaticFragmentNodeWarning');
24
25
  const {useDebugValue} = require('react');
25
26
  const {getFragment} = require('relay-runtime');
26
27
 
27
- type ReturnType<TQuery: OperationType, TKey: ?{+$data?: mixed, ...}> = [
28
+ type RefetchVariables<TVariables, TKey> =
29
+ // NOTE: This $Call ensures that the type of the returned variables is either:
30
+ // - nullable if the provided ref type is nullable
31
+ // - non-nullable if the provided ref type is non-nullable
32
+ // prettier-ignore
33
+ $Call<
34
+ & (<TFragmentType>( { +$fragmentSpreads: TFragmentType, ... }) => $Shape<TVariables>)
35
+ & (<TFragmentType>(?{ +$fragmentSpreads: TFragmentType, ... }) => TVariables),
36
+ TKey,
37
+ >;
38
+
39
+ type RefetchFnBase<TVars, TOptions> = (
40
+ vars: TVars,
41
+ options?: TOptions,
42
+ ) => Disposable;
43
+
44
+ export type RefetchFn<TVariables, TKey, TOptions = Options> = RefetchFnBase<
45
+ RefetchVariables<TVariables, TKey>,
46
+ TOptions,
47
+ >;
48
+
49
+ type ReturnType<TVariables, TData, TKey> = [
28
50
  // NOTE: This $Call ensures that the type of the returned data is either:
29
51
  // - nullable if the provided ref type is nullable
30
52
  // - non-nullable if the provided ref type is non-nullable
31
53
  // prettier-ignore
32
54
  $Call<
33
- & (<TFragmentData>( { +$data?: TFragmentData, ... }) => TFragmentData)
34
- & (<TFragmentData>(?{ +$data?: TFragmentData, ... }) => ?TFragmentData),
55
+ & (<TFragmentType>( { +$fragmentSpreads: TFragmentType, ... }) => TData)
56
+ & (<TFragmentType>(?{ +$fragmentSpreads: TFragmentType, ... }) => ?TData),
35
57
  TKey,
36
58
  >,
37
- RefetchFnDynamic<TQuery, TKey>,
59
+ RefetchFn<TVariables, TKey>,
38
60
  ];
39
61
 
40
- // This separate type export is only needed as long as we are injecting
41
- // a separate hooks implementation in ./HooksImplementation -- it can
42
- // be removed after we stop doing that.
43
62
  export type UseRefetchableFragmentType = <
44
- TQuery: OperationType,
45
- TKey: ?{+$data?: mixed, +$fragmentSpreads: FragmentType, ...},
63
+ TFragmentType: FragmentType,
64
+ TVariables: Variables,
65
+ TData,
66
+ TKey: ?{+$fragmentSpreads: TFragmentType, ...},
46
67
  >(
47
- fragmentInput: GraphQLTaggedNode,
48
- fragmentRef: TKey,
49
- ) => ReturnType<TQuery, TKey>;
68
+ fragment: RefetchableFragment<TFragmentType, TData, TVariables>,
69
+ key: TKey,
70
+ ) => ReturnType<TVariables, TData, TKey>;
50
71
 
51
72
  function useRefetchableFragment_LEGACY<
52
- TQuery: OperationType,
53
- TKey: ?{+$data?: mixed, +$fragmentSpreads: FragmentType, ...},
73
+ TFragmentType: FragmentType,
74
+ TVariables: Variables,
75
+ TData,
76
+ TKey: ?{+$fragmentSpreads: TFragmentType, ...},
54
77
  >(
55
- fragmentInput: GraphQLTaggedNode,
78
+ fragmentInput: RefetchableFragment<TFragmentType, TData, TVariables>,
56
79
  fragmentRef: TKey,
57
- ): ReturnType<TQuery, TKey> {
80
+ ): ReturnType<TVariables, TData, TKey> {
58
81
  const fragmentNode = getFragment(fragmentInput);
59
82
  useStaticFragmentNodeWarning(
60
83
  fragmentNode,
61
84
  'first argument of useRefetchableFragment()',
62
85
  );
63
- const {fragmentData, refetch} = useRefetchableFragmentNode<TQuery, TKey>(
64
- fragmentNode,
65
- fragmentRef,
66
- 'useRefetchableFragment()',
67
- );
86
+ const {fragmentData, refetch} = useRefetchableFragmentNode<
87
+ {
88
+ response: TData,
89
+ variables: TVariables,
90
+ },
91
+ {
92
+ +$data: mixed,
93
+ ...
94
+ },
95
+ >(fragmentNode, fragmentRef, 'useRefetchableFragment()');
68
96
  if (__DEV__) {
69
97
  // eslint-disable-next-line react-hooks/rules-of-hooks
70
98
  useDebugValue({fragment: fragmentNode.name, data: fragmentData});
71
99
  }
72
- /* $FlowExpectedError[prop-missing] : Exposed options is a subset of internal
73
- * options */
74
- return [fragmentData, (refetch: RefetchFnDynamic<TQuery, TKey>)];
100
+
101
+ // $FlowFixMe[incompatible-return]
102
+ // $FlowFixMe[prop-missing]
103
+ return [fragmentData, refetch];
75
104
  }
76
105
 
77
106
  function useRefetchableFragment<
78
- TQuery: OperationType,
79
- TKey: ?{+$data?: mixed, +$fragmentSpreads: FragmentType, ...},
107
+ TFragmentType: FragmentType,
108
+ TVariables: Variables,
109
+ TData,
110
+ TKey: ?{+$fragmentSpreads: TFragmentType, ...},
80
111
  >(
81
- fragmentInput: GraphQLTaggedNode,
112
+ fragmentInput: RefetchableFragment<TFragmentType, TData, TVariables>,
82
113
  parentFragmentRef: TKey,
83
- ): ReturnType<TQuery, TKey> {
114
+ ): ReturnType<TVariables, TData, TKey> {
84
115
  const impl = HooksImplementation.get();
85
116
  if (impl) {
86
- return impl.useRefetchableFragment<TQuery, TKey>(
117
+ return impl.useRefetchableFragment<TFragmentType, TVariables, TData, TKey>(
87
118
  fragmentInput,
88
119
  parentFragmentRef,
89
120
  );
@@ -4,9 +4,9 @@
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
- * @emails oncall+relay
8
7
  * @flow strict-local
9
8
  * @format
9
+ * @oncall relay
10
10
  */
11
11
 
12
12
  'use strict';
@@ -207,8 +207,11 @@ function useRefetchableFragmentNode<
207
207
  const shouldReset =
208
208
  environment !== mirroredEnvironment ||
209
209
  fragmentIdentifier !== mirroredFragmentIdentifier;
210
- const [queryRef, loadQuery, disposeQuery] =
211
- useQueryLoader<TQuery>(refetchableRequest);
210
+ const [queryRef, loadQuery, disposeQuery] = useQueryLoader<
211
+ TQuery['variables'],
212
+ TQuery['response'],
213
+ TQuery['rawResponse'],
214
+ >((refetchableRequest: $FlowFixMe));
212
215
 
213
216
  let fragmentRef = parentFragmentRef;
214
217
  if (shouldReset) {
@@ -391,7 +394,10 @@ function useRefetchFunction<TQuery: OperationType>(
391
394
  ? fragmentData[identifierField]
392
395
  : null;
393
396
  return useCallback(
394
- (providedRefetchVariables, options) => {
397
+ (
398
+ providedRefetchVariables: VariablesOf<TQuery>,
399
+ options: void | InternalOptions,
400
+ ) => {
395
401
  // Bail out and warn if we're trying to refetch after the component
396
402
  // has unmounted
397
403
  if (isMountedRef.current !== true) {
@@ -4,18 +4,18 @@
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
- * @emails oncall+relay
8
7
  * @flow strict-local
9
8
  * @format
9
+ * @oncall relay
10
10
  */
11
11
 
12
12
  'use strict';
13
13
 
14
14
  import type {IEnvironment} from 'relay-runtime';
15
15
 
16
+ const ReactRelayContext = require('./../ReactRelayContext');
16
17
  const invariant = require('invariant');
17
18
  const {useContext} = require('react');
18
- const ReactRelayContext = require('react-relay/ReactRelayContext');
19
19
 
20
20
  function useRelayEnvironment(): IEnvironment {
21
21
  const context = useContext(ReactRelayContext);