react-relay 14.0.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 -2
  2. package/ReactRelayContext.js +1 -1
  3. package/ReactRelayContext.js.flow +1 -2
  4. package/ReactRelayFragmentContainer.js.flow +6 -4
  5. package/ReactRelayFragmentMockRenderer.js.flow +1 -2
  6. package/ReactRelayLocalQueryRenderer.js.flow +5 -5
  7. package/ReactRelayPaginationContainer.js.flow +21 -14
  8. package/ReactRelayQueryFetcher.js.flow +28 -16
  9. package/ReactRelayQueryRenderer.js.flow +42 -13
  10. package/ReactRelayQueryRendererContext.js.flow +2 -3
  11. package/ReactRelayRefetchContainer.js.flow +9 -9
  12. package/ReactRelayTestMocker.js.flow +3 -3
  13. package/ReactRelayTypes.js.flow +7 -8
  14. package/RelayContext.js.flow +1 -2
  15. package/__flowtests__/ReactRelayFragmentContainer-flowtest.js.flow +4 -5
  16. package/__flowtests__/ReactRelayPaginationContainer-flowtest.js.flow +4 -5
  17. package/__flowtests__/ReactRelayRefetchContainer-flowtest.js.flow +4 -5
  18. package/__flowtests__/RelayModern-flowtest.js.flow +3 -4
  19. package/__flowtests__/RelayModernFlowtest_badref.graphql.js.flow +3 -4
  20. package/__flowtests__/RelayModernFlowtest_notref.graphql.js.flow +3 -4
  21. package/__flowtests__/RelayModernFlowtest_user.graphql.js.flow +3 -4
  22. package/__flowtests__/RelayModernFlowtest_users.graphql.js.flow +3 -4
  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 -2
  34. package/buildReactRelayContainer.js.flow +7 -7
  35. package/getRootVariablesForFragments.js.flow +1 -3
  36. package/hooks.js +1 -1
  37. package/hooks.js.flow +4 -2
  38. package/index.js +1 -1
  39. package/index.js.flow +6 -2
  40. package/isRelayEnvironment.js.flow +1 -2
  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 -2
  46. package/lib/ReactRelayContainerUtils.js +2 -3
  47. package/lib/ReactRelayContext.js +3 -4
  48. package/lib/ReactRelayFragmentContainer.js +47 -73
  49. package/lib/ReactRelayFragmentMockRenderer.js +2 -4
  50. package/lib/ReactRelayLocalQueryRenderer.js +18 -31
  51. package/lib/ReactRelayPaginationContainer.js +74 -164
  52. package/lib/ReactRelayQueryFetcher.js +49 -76
  53. package/lib/ReactRelayQueryRenderer.js +63 -84
  54. package/lib/ReactRelayQueryRendererContext.js +2 -2
  55. package/lib/ReactRelayRefetchContainer.js +58 -108
  56. package/lib/ReactRelayTestMocker.js +33 -68
  57. package/lib/ReactRelayTypes.js +2 -1
  58. package/lib/RelayContext.js +4 -7
  59. package/lib/assertFragmentMap.js +3 -5
  60. package/lib/buildReactRelayContainer.js +11 -27
  61. package/lib/getRootVariablesForFragments.js +6 -10
  62. package/lib/hooks.js +5 -18
  63. package/lib/index.js +7 -24
  64. package/lib/isRelayEnvironment.js +5 -4
  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 -8
  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 -8
  72. package/lib/relay-hooks/EntryPointContainer.react.js +9 -15
  73. package/lib/relay-hooks/EntryPointTypes.flow.js +5 -3
  74. package/lib/relay-hooks/FragmentResource.js +109 -203
  75. package/lib/relay-hooks/HooksImplementation.js +3 -6
  76. package/lib/relay-hooks/InternalLogger.js +2 -3
  77. package/lib/relay-hooks/LRUCache.js +2 -20
  78. package/lib/relay-hooks/LazyLoadEntryPointContainer_DEPRECATED.react.js +33 -54
  79. package/lib/relay-hooks/MatchContainer.js +15 -24
  80. package/lib/relay-hooks/ProfilerContext.js +3 -3
  81. package/lib/relay-hooks/QueryResource.js +31 -101
  82. package/lib/relay-hooks/RelayEnvironmentProvider.js +5 -9
  83. package/lib/relay-hooks/SuspenseResource.js +9 -33
  84. package/lib/relay-hooks/loadEntryPoint.js +19 -31
  85. package/lib/relay-hooks/loadQuery.js +42 -78
  86. package/lib/relay-hooks/preloadQuery_DEPRECATED.js +11 -37
  87. package/lib/relay-hooks/prepareEntryPoint_DEPRECATED.js +9 -15
  88. package/lib/relay-hooks/react-cache/RelayReactCache.js +7 -12
  89. package/lib/relay-hooks/react-cache/getQueryResultOrFetchQuery_REACT_CACHE.js +27 -81
  90. package/lib/relay-hooks/react-cache/readFragmentInternal_REACT_CACHE.js +206 -0
  91. package/lib/relay-hooks/react-cache/useFragmentInternal_REACT_CACHE.js +195 -215
  92. package/lib/relay-hooks/react-cache/useFragment_REACT_CACHE.js +5 -15
  93. package/lib/relay-hooks/react-cache/useLazyLoadQuery_REACT_CACHE.js +17 -24
  94. package/lib/relay-hooks/react-cache/usePaginationFragment_REACT_CACHE.js +149 -0
  95. package/lib/relay-hooks/react-cache/usePreloadedQuery_REACT_CACHE.js +24 -39
  96. package/lib/relay-hooks/react-cache/useRefetchableFragmentInternal_REACT_CACHE.js +325 -0
  97. package/lib/relay-hooks/react-cache/useRefetchableFragment_REACT_CACHE.js +37 -0
  98. package/lib/relay-hooks/useBlockingPaginationFragment.js +73 -93
  99. package/lib/relay-hooks/useClientQuery.js +30 -0
  100. package/lib/relay-hooks/useEntryPointLoader.js +18 -38
  101. package/lib/relay-hooks/useFetchTrackingRef.js +10 -12
  102. package/lib/relay-hooks/useFragment.js +8 -19
  103. package/lib/relay-hooks/useFragmentNode.js +20 -32
  104. package/lib/relay-hooks/useIsMountedRef.js +4 -6
  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 +7 -24
  108. package/lib/relay-hooks/useLazyLoadQueryNode.js +23 -34
  109. package/lib/relay-hooks/useLoadMoreFunction.js +46 -78
  110. package/lib/relay-hooks/useMemoOperationDescriptor.js +6 -15
  111. package/lib/relay-hooks/useMemoVariables.js +15 -34
  112. package/lib/relay-hooks/useMutation.js +9 -27
  113. package/lib/relay-hooks/usePaginationFragment.js +73 -76
  114. package/lib/relay-hooks/usePreloadedQuery.js +13 -44
  115. package/lib/relay-hooks/useQueryLoader.js +24 -49
  116. package/lib/relay-hooks/useRefetchableFragment.js +19 -17
  117. package/lib/relay-hooks/useRefetchableFragmentNode.js +65 -109
  118. package/lib/relay-hooks/useRelayEnvironment.js +4 -8
  119. package/lib/relay-hooks/useStaticFragmentNodeWarning.js +4 -8
  120. package/lib/relay-hooks/useSubscribeToInvalidationState.js +8 -9
  121. package/lib/relay-hooks/useSubscription.js +5 -10
  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 -4
  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 +3 -5
  134. package/relay-hooks/EntryPointTypes.flow.js.flow +37 -37
  135. package/relay-hooks/FragmentResource.js.flow +43 -19
  136. package/relay-hooks/HooksImplementation.js.flow +7 -9
  137. package/relay-hooks/InternalLogger.js.flow +1 -3
  138. package/relay-hooks/LRUCache.js.flow +1 -3
  139. package/relay-hooks/LazyLoadEntryPointContainer_DEPRECATED.react.js.flow +19 -14
  140. package/relay-hooks/MatchContainer.js.flow +6 -8
  141. package/relay-hooks/ProfilerContext.js.flow +1 -3
  142. package/relay-hooks/QueryResource.js.flow +29 -11
  143. package/relay-hooks/RelayEnvironmentProvider.js.flow +4 -6
  144. package/relay-hooks/SuspenseResource.js.flow +1 -3
  145. package/relay-hooks/__flowtests__/EntryPointTypes/EntryPointElementConfig-flowtest.js.flow +6 -4
  146. package/relay-hooks/__flowtests__/EntryPointTypes/NestedEntrypoints-flowtest.js.flow +4 -4
  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 +39 -39
  150. package/relay-hooks/__flowtests__/useFragment-flowtest.js.flow +1 -3
  151. package/relay-hooks/__flowtests__/usePaginationFragment-flowtest.js.flow +37 -38
  152. package/relay-hooks/__flowtests__/useRefetchableFragment-flowtest.js.flow +18 -20
  153. package/relay-hooks/__flowtests__/utils.js.flow +21 -12
  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 +9 -12
  157. package/relay-hooks/prepareEntryPoint_DEPRECATED.js.flow +13 -10
  158. package/relay-hooks/react-cache/RelayReactCache.js.flow +1 -3
  159. package/relay-hooks/react-cache/getQueryResultOrFetchQuery_REACT_CACHE.js.flow +26 -20
  160. package/relay-hooks/react-cache/readFragmentInternal_REACT_CACHE.js.flow +297 -0
  161. package/relay-hooks/react-cache/useFragmentInternal_REACT_CACHE.js.flow +136 -96
  162. package/relay-hooks/react-cache/useFragment_REACT_CACHE.js.flow +1 -3
  163. package/relay-hooks/react-cache/useLazyLoadQuery_REACT_CACHE.js.flow +3 -5
  164. package/relay-hooks/react-cache/usePaginationFragment_REACT_CACHE.js.flow +190 -0
  165. package/relay-hooks/react-cache/usePreloadedQuery_REACT_CACHE.js.flow +3 -6
  166. package/relay-hooks/react-cache/useRefetchableFragmentInternal_REACT_CACHE.js.flow +601 -0
  167. package/relay-hooks/react-cache/useRefetchableFragment_REACT_CACHE.js.flow +65 -0
  168. package/relay-hooks/useBlockingPaginationFragment.js.flow +86 -59
  169. package/relay-hooks/useClientQuery.js.flow +39 -0
  170. package/relay-hooks/useEntryPointLoader.js.flow +16 -14
  171. package/relay-hooks/useFetchTrackingRef.js.flow +7 -8
  172. package/relay-hooks/useFragment.js.flow +2 -4
  173. package/relay-hooks/useFragmentNode.js.flow +7 -8
  174. package/relay-hooks/useIsMountedRef.js.flow +2 -4
  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 +9 -32
  178. package/relay-hooks/useLazyLoadQueryNode.js.flow +4 -6
  179. package/relay-hooks/useLoadMoreFunction.js.flow +20 -17
  180. package/relay-hooks/useMemoOperationDescriptor.js.flow +3 -5
  181. package/relay-hooks/useMemoVariables.js.flow +13 -31
  182. package/relay-hooks/useMutation.js.flow +6 -8
  183. package/relay-hooks/usePaginationFragment.js.flow +75 -43
  184. package/relay-hooks/usePreloadedQuery.js.flow +49 -43
  185. package/relay-hooks/useQueryLoader.js.flow +89 -28
  186. package/relay-hooks/useRefetchableFragment.js.flow +83 -23
  187. package/relay-hooks/useRefetchableFragmentNode.js.flow +26 -22
  188. package/relay-hooks/useRelayEnvironment.js.flow +2 -4
  189. package/relay-hooks/useStaticFragmentNodeWarning.js.flow +3 -5
  190. package/relay-hooks/useSubscribeToInvalidationState.js.flow +2 -4
  191. package/relay-hooks/useSubscription.js.flow +1 -3
  192. package/relay-hooks/useUnsafeRef_DEPRECATED.js.flow +25 -0
  193. package/lib/readContext.js +0 -28
  194. package/readContext.js.flow +0 -31
@@ -4,13 +4,11 @@
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
- // flowlint ambiguous-object-type:error
13
-
14
12
  'use strict';
15
13
 
16
14
  import type {
@@ -19,10 +17,9 @@ import type {
19
17
  Disposable,
20
18
  GraphQLResponse,
21
19
  Observer,
22
- OperationType,
23
20
  ReaderFragment,
24
21
  ReaderPaginationMetadata,
25
- VariablesOf,
22
+ Variables,
26
23
  } from 'relay-runtime';
27
24
 
28
25
  const useFetchTrackingRef = require('./useFetchTrackingRef');
@@ -32,8 +29,8 @@ const useRelayEnvironment = require('./useRelayEnvironment');
32
29
  const invariant = require('invariant');
33
30
  const {useCallback, useEffect, useState} = require('react');
34
31
  const {
35
- ConnectionInterface,
36
32
  __internal: {fetchQuery},
33
+ ConnectionInterface,
37
34
  createOperationDescriptor,
38
35
  getPaginationVariables,
39
36
  getSelector,
@@ -41,15 +38,15 @@ const {
41
38
  } = require('relay-runtime');
42
39
  const warning = require('warning');
43
40
 
44
- export type LoadMoreFn<TQuery: OperationType> = (
41
+ export type LoadMoreFn<TVariables: Variables> = (
45
42
  count: number,
46
- options?: {|
43
+ options?: {
47
44
  onComplete?: (Error | null) => void,
48
- UNSTABLE_extraVariables?: $Shape<VariablesOf<TQuery>>,
49
- |},
45
+ UNSTABLE_extraVariables?: $Shape<TVariables>,
46
+ },
50
47
  ) => Disposable;
51
48
 
52
- export type UseLoadMoreFunctionArgs = {|
49
+ export type UseLoadMoreFunctionArgs = {
53
50
  direction: Direction,
54
51
  fragmentNode: ReaderFragment,
55
52
  fragmentRef: mixed,
@@ -62,11 +59,11 @@ export type UseLoadMoreFunctionArgs = {|
62
59
  componentDisplayName: string,
63
60
  observer: Observer<GraphQLResponse>,
64
61
  onReset: () => void,
65
- |};
62
+ };
66
63
 
67
- function useLoadMoreFunction<TQuery: OperationType>(
64
+ function useLoadMoreFunction<TVariables: Variables>(
68
65
  args: UseLoadMoreFunctionArgs,
69
- ): [LoadMoreFn<TQuery>, boolean, () => void] {
66
+ ): [LoadMoreFn<TVariables>, boolean, () => void] {
70
67
  const {
71
68
  direction,
72
69
  fragmentNode,
@@ -125,7 +122,13 @@ function useLoadMoreFunction<TQuery: OperationType>(
125
122
  }, [disposeFetch]);
126
123
 
127
124
  const loadMore = useCallback(
128
- (count, options) => {
125
+ (
126
+ count: number,
127
+ options: void | {
128
+ UNSTABLE_extraVariables?: $Shape<TVariables>,
129
+ onComplete?: (Error | null) => void,
130
+ },
131
+ ) => {
129
132
  // TODO(T41131846): Fetch/Caching policies for loadMore
130
133
 
131
134
  const onComplete = options?.onComplete;
@@ -265,10 +268,10 @@ function getConnectionState(
265
268
  fragmentNode: ReaderFragment,
266
269
  fragmentData: mixed,
267
270
  connectionPathInFragmentData: $ReadOnlyArray<string | number>,
268
- ): {|
271
+ ): {
269
272
  cursor: ?string,
270
273
  hasMore: boolean,
271
- |} {
274
+ } {
272
275
  const {
273
276
  EDGES,
274
277
  PAGE_INFO,
@@ -4,13 +4,11 @@
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
- // flowlint ambiguous-object-type:error
13
-
14
12
  'use strict';
15
13
 
16
14
  import type {
@@ -31,8 +29,8 @@ function useMemoOperationDescriptor(
31
29
  variables: Variables,
32
30
  cacheConfig?: ?CacheConfig,
33
31
  ): OperationDescriptor {
34
- const [memoVariables] = useMemoVariables(variables);
35
- const [memoCacheConfig] = useMemoVariables(cacheConfig || {});
32
+ const memoVariables = useMemoVariables(variables);
33
+ const memoCacheConfig = useMemoVariables(cacheConfig || {});
36
34
  return useMemo(
37
35
  () =>
38
36
  createOperationDescriptor(
@@ -4,51 +4,33 @@
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
- // flowlint ambiguous-object-type:error
13
-
14
12
  'use strict';
15
13
 
16
14
  import type {Variables} from 'relay-runtime';
17
15
 
18
16
  const areEqual = require('areEqual');
19
- const React = require('react');
20
-
21
- const {useMemo, useRef, useState} = React;
17
+ const {useState} = require('react');
22
18
 
19
+ /**
20
+ * Memoizes the passed in `variables` object based on `areEqual` equality.
21
+ * This is useful when a `variables` object is used as a value in a depencency
22
+ * array as it might often be constructed during render.
23
+ */
23
24
  function useMemoVariables<TVariables: Variables | null>(
24
25
  variables: TVariables,
25
- ): [TVariables, number] {
26
- // The value of this ref is a counter that should be incremented when
27
- // variables change. This allows us to use the counter as a
28
- // memoization value to indicate if the computation for useMemo
29
- // should be re-executed.
30
- const variablesChangedGenerationRef = useRef(0);
31
-
32
- // We mirror the variables to check if they have changed between renders
33
- const [mirroredVariables, setMirroredVariables] = useState<Variables | null>(
34
- variables,
35
- );
36
-
37
- const variablesChanged = !areEqual(variables, mirroredVariables);
38
- if (variablesChanged) {
39
- variablesChangedGenerationRef.current =
40
- (variablesChangedGenerationRef.current ?? 0) + 1;
26
+ ): TVariables {
27
+ const [mirroredVariables, setMirroredVariables] = useState(variables);
28
+ if (areEqual(variables, mirroredVariables)) {
29
+ return mirroredVariables;
30
+ } else {
41
31
  setMirroredVariables(variables);
32
+ return variables;
42
33
  }
43
-
44
- // NOTE: We disable react-hooks-deps warning because we explicitly
45
- // don't want to memoize on object identity
46
- // eslint-disable-next-line react-hooks/exhaustive-deps
47
- const memoVariables = useMemo(
48
- () => variables,
49
- [variablesChangedGenerationRef.current],
50
- );
51
- return [memoVariables, variablesChangedGenerationRef.current ?? 0];
52
34
  }
53
35
 
54
36
  module.exports = useMemoVariables;
@@ -4,13 +4,11 @@
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
- // flowlint ambiguous-object-type:error
13
-
14
12
  'use strict';
15
13
 
16
14
  import type {
@@ -32,7 +30,7 @@ const {commitMutation: defaultCommitMutation} = require('relay-runtime');
32
30
 
33
31
  const {useState, useEffect, useRef, useCallback} = React;
34
32
 
35
- export type UseMutationConfig<TMutation: MutationParameters> = {|
33
+ export type UseMutationConfig<TMutation: MutationParameters> = {
36
34
  configs?: Array<DeclarativeMutationConfig>,
37
35
  onError?: ?(error: Error) => void,
38
36
  onCompleted?: ?(
@@ -50,7 +48,7 @@ export type UseMutationConfig<TMutation: MutationParameters> = {|
50
48
  updater?: ?SelectorStoreUpdater<TMutation['response']>,
51
49
  uploadables?: UploadableMap,
52
50
  variables: TMutation['variables'],
53
- |};
51
+ };
54
52
 
55
53
  function useMutation<TMutation: MutationParameters>(
56
54
  mutation: GraphQLTaggedNode,
@@ -63,11 +61,11 @@ function useMutation<TMutation: MutationParameters>(
63
61
  const isMountedRef = useIsMountedRef();
64
62
  const environmentRef = useRef(environment);
65
63
  const mutationRef = useRef(mutation);
66
- const inFlightMutationsRef = useRef(new Set());
64
+ const inFlightMutationsRef = useRef(new Set<Disposable>());
67
65
  const [isMutationInFlight, setMutationInFlight] = useState(false);
68
66
 
69
67
  const cleanup = useCallback(
70
- disposable => {
68
+ (disposable: Disposable) => {
71
69
  if (
72
70
  environmentRef.current === environment &&
73
71
  mutationRef.current === mutation
@@ -100,7 +98,7 @@ function useMutation<TMutation: MutationParameters>(
100
98
  if (isMountedRef.current) {
101
99
  setMutationInFlight(true);
102
100
  }
103
- const disposable = commitMutationFn(environment, {
101
+ const disposable: Disposable = commitMutationFn(environment, {
104
102
  ...config,
105
103
  mutation,
106
104
  onCompleted: (response, errors) => {
@@ -4,25 +4,25 @@
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
- // flowlint ambiguous-object-type:error
13
-
14
12
  'use strict';
15
13
 
16
14
  import type {LoadMoreFn, UseLoadMoreFunctionArgs} from './useLoadMoreFunction';
17
- import type {RefetchFnDynamic} from './useRefetchableFragmentNode';
15
+ import type {RefetchFn} from './useRefetchableFragment';
16
+ import type {Options} from './useRefetchableFragmentNode';
18
17
  import type {
19
18
  FragmentType,
20
19
  GraphQLResponse,
21
- GraphQLTaggedNode,
22
20
  Observer,
23
- OperationType,
21
+ RefetchableFragment,
22
+ Variables,
24
23
  } from 'relay-runtime';
25
24
 
25
+ const HooksImplementation = require('./HooksImplementation');
26
26
  const useLoadMoreFunction = require('./useLoadMoreFunction');
27
27
  const useRefetchableFragmentNode = require('./useRefetchableFragmentNode');
28
28
  const useStaticFragmentNodeWarning = require('./useStaticFragmentNodeWarning');
@@ -33,36 +33,28 @@ const {
33
33
  getPaginationMetadata,
34
34
  } = require('relay-runtime');
35
35
 
36
- export type ReturnType<TQuery: OperationType, TKey, TFragmentData> = {|
37
- data: TFragmentData,
38
- loadNext: LoadMoreFn<TQuery>,
39
- loadPrevious: LoadMoreFn<TQuery>,
40
- hasNext: boolean,
41
- hasPrevious: boolean,
42
- isLoadingNext: boolean,
43
- isLoadingPrevious: boolean,
44
- refetch: RefetchFnDynamic<TQuery, TKey>,
45
- |};
36
+ // This separate type export is only needed as long as we are injecting
37
+ // a separate hooks implementation in ./HooksImplementation -- it can
38
+ // be removed after we stop doing that.
39
+ export type UsePaginationFragmentType = <
40
+ TFragmentType: FragmentType,
41
+ TVariables: Variables,
42
+ TData,
43
+ TKey: ?{+$fragmentSpreads: TFragmentType, ...},
44
+ >(
45
+ fragmentInput: RefetchableFragment<TFragmentType, TData, TVariables>,
46
+ parentFragmentRef: TKey,
47
+ ) => ReturnType<TVariables, TData, TKey>;
46
48
 
47
- function usePaginationFragment<
48
- TQuery: OperationType,
49
- TKey: ?{+$data?: mixed, +$fragmentSpreads: FragmentType, ...},
49
+ function usePaginationFragment_LEGACY<
50
+ TFragmentType: FragmentType,
51
+ TVariables: Variables,
52
+ TData,
53
+ TKey: ?{+$fragmentSpreads: TFragmentType, ...},
50
54
  >(
51
- fragmentInput: GraphQLTaggedNode,
55
+ fragmentInput: RefetchableFragment<TFragmentType, TData, TVariables>,
52
56
  parentFragmentRef: TKey,
53
- ): ReturnType<
54
- TQuery,
55
- TKey,
56
- // NOTE: This $Call ensures that the type of the returned data is either:
57
- // - nullable if the provided ref type is nullable
58
- // - non-nullable if the provided ref type is non-nullable
59
- // prettier-ignore
60
- $Call<
61
- & (<TFragmentData>( { +$data?: TFragmentData, ... }) => TFragmentData)
62
- & (<TFragmentData>(?{ +$data?: TFragmentData, ... }) => ?TFragmentData),
63
- TKey,
64
- >,
65
- > {
57
+ ): ReturnType<TVariables, TData, TKey> {
66
58
  const fragmentNode = getFragment(fragmentInput);
67
59
  useStaticFragmentNodeWarning(
68
60
  fragmentNode,
@@ -78,14 +70,14 @@ function usePaginationFragment<
78
70
  } = getPaginationMetadata(fragmentNode, componentDisplayName);
79
71
 
80
72
  const {fragmentData, fragmentRef, refetch} = useRefetchableFragmentNode<
81
- TQuery,
82
- TKey,
73
+ $FlowFixMe,
74
+ $FlowFixMe,
83
75
  >(fragmentNode, parentFragmentRef, componentDisplayName);
84
76
  const fragmentIdentifier = getFragmentIdentifier(fragmentNode, fragmentRef);
85
77
 
86
78
  // Backward pagination
87
79
  const [loadPrevious, hasPrevious, isLoadingPrevious, disposeFetchPrevious] =
88
- useLoadMore<TQuery>({
80
+ useLoadMore<TVariables>({
89
81
  componentDisplayName,
90
82
  connectionPathInFragmentData,
91
83
  direction: 'backward',
@@ -100,7 +92,7 @@ function usePaginationFragment<
100
92
 
101
93
  // Forward pagination
102
94
  const [loadNext, hasNext, isLoadingNext, disposeFetchNext] =
103
- useLoadMore<TQuery>({
95
+ useLoadMore<TVariables>({
104
96
  componentDisplayName,
105
97
  connectionPathInFragmentData,
106
98
  direction: 'forward',
@@ -113,8 +105,8 @@ function usePaginationFragment<
113
105
  paginationRequest,
114
106
  });
115
107
 
116
- const refetchPagination: RefetchFnDynamic<TQuery, TKey> = useCallback(
117
- (variables, options) => {
108
+ const refetchPagination: RefetchFn<TVariables, TKey> = useCallback(
109
+ (variables: TVariables, options: void | Options) => {
118
110
  disposeFetchNext();
119
111
  disposeFetchPrevious();
120
112
  return refetch(variables, {...options, __environment: undefined});
@@ -134,7 +126,7 @@ function usePaginationFragment<
134
126
  });
135
127
  }
136
128
  return {
137
- data: fragmentData,
129
+ data: (fragmentData: $FlowFixMe),
138
130
  loadNext,
139
131
  loadPrevious,
140
132
  hasNext,
@@ -145,7 +137,7 @@ function usePaginationFragment<
145
137
  };
146
138
  }
147
139
 
148
- function useLoadMore<TQuery: OperationType>(
140
+ function useLoadMore<TVariables: Variables>(
149
141
  args: $Diff<
150
142
  UseLoadMoreFunctionArgs,
151
143
  {
@@ -154,7 +146,7 @@ function useLoadMore<TQuery: OperationType>(
154
146
  ...
155
147
  },
156
148
  >,
157
- ): [LoadMoreFn<TQuery>, boolean, boolean, () => void] {
149
+ ): [LoadMoreFn<TVariables>, boolean, boolean, () => void] {
158
150
  const [isLoadingMore, setIsLoadingMore] = useState(false);
159
151
  const observer = {
160
152
  start: () => setIsLoadingMore(true),
@@ -162,7 +154,7 @@ function useLoadMore<TQuery: OperationType>(
162
154
  error: () => setIsLoadingMore(false),
163
155
  };
164
156
  const handleReset = () => setIsLoadingMore(false);
165
- const [loadMore, hasMore, disposeFetch] = useLoadMoreFunction({
157
+ const [loadMore, hasMore, disposeFetch] = useLoadMoreFunction<TVariables>({
166
158
  ...args,
167
159
  observer,
168
160
  onReset: handleReset,
@@ -170,4 +162,44 @@ function useLoadMore<TQuery: OperationType>(
170
162
  return [loadMore, hasMore, isLoadingMore, disposeFetch];
171
163
  }
172
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
+
184
+ function usePaginationFragment<
185
+ TFragmentType: FragmentType,
186
+ TVariables: Variables,
187
+ TData,
188
+ TKey: ?{+$fragmentSpreads: TFragmentType, ...},
189
+ >(
190
+ fragmentInput: RefetchableFragment<TFragmentType, TData, TVariables>,
191
+ parentFragmentRef: TKey,
192
+ ): ReturnType<TVariables, TData, TKey> {
193
+ const impl = HooksImplementation.get();
194
+ if (impl) {
195
+ return impl.usePaginationFragment<TFragmentType, TVariables, TData, TKey>(
196
+ fragmentInput,
197
+ parentFragmentRef,
198
+ );
199
+ } else {
200
+ // eslint-disable-next-line react-hooks/rules-of-hooks
201
+ return usePaginationFragment_LEGACY(fragmentInput, parentFragmentRef);
202
+ }
203
+ }
204
+
173
205
  module.exports = usePaginationFragment;
@@ -4,23 +4,20 @@
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
- // flowlint ambiguous-object-type:error
13
-
14
12
  'use strict';
15
13
 
16
- import type {PreloadedQuery} from './EntryPointTypes.flow';
17
14
  import type {
18
- GraphQLTaggedNode,
19
- OperationType,
20
- RenderPolicy,
21
- } from 'relay-runtime';
15
+ EnvironmentProviderOptions,
16
+ PreloadedQueryInner,
17
+ PreloadedQueryInner_DEPRECATED,
18
+ } from './EntryPointTypes.flow';
19
+ import type {Query, RenderPolicy, Variables} from 'relay-runtime';
22
20
 
23
- const HooksImplementation = require('./HooksImplementation');
24
21
  const {useTrackLoadQueryInRender} = require('./loadQuery');
25
22
  const useLazyLoadQueryNode = require('./useLazyLoadQueryNode');
26
23
  const useMemoOperationDescriptor = require('./useMemoOperationDescriptor');
@@ -32,24 +29,44 @@ const {
32
29
  } = require('relay-runtime');
33
30
  const warning = require('warning');
34
31
 
35
- // This separate type export is only needed as long as we are injecting
36
- // a separate hooks implementation in ./HooksImplementation -- it can
37
- // be removed after we stop doing that.
38
- export type UsePreloadedQueryHookType = <TQuery: OperationType>(
39
- gqlQuery: GraphQLTaggedNode,
40
- preloadedQuery: PreloadedQuery<TQuery>,
41
- options?: {|
42
- UNSTABLE_renderPolicy?: RenderPolicy,
43
- |},
44
- ) => 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
+ >;
45
54
 
46
- function usePreloadedQuery_LEGACY<TQuery: OperationType>(
47
- gqlQuery: GraphQLTaggedNode,
48
- preloadedQuery: PreloadedQuery<TQuery>,
49
- options?: {|
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
+ >,
66
+ options?: {
50
67
  UNSTABLE_renderPolicy?: RenderPolicy,
51
- |},
52
- ): TQuery['response'] {
68
+ },
69
+ ): TData {
53
70
  // We need to use this hook in order to be able to track if
54
71
  // loadQuery was called during render
55
72
  useTrackLoadQueryInRender();
@@ -139,7 +156,12 @@ function usePreloadedQuery_LEGACY<TQuery: OperationType>(
139
156
  };
140
157
  }
141
158
 
142
- const data = useLazyLoadQueryNode(useLazyLoadQueryNodeParams);
159
+ const data = useLazyLoadQueryNode<{
160
+ variables: TVariables,
161
+ response: TData,
162
+ rawResponse?: $NonMaybeType<TRawResponse>,
163
+ }>(useLazyLoadQueryNodeParams);
164
+
143
165
  if (__DEV__) {
144
166
  // eslint-disable-next-line react-hooks/rules-of-hooks
145
167
  useDebugValue({
@@ -154,20 +176,4 @@ function usePreloadedQuery_LEGACY<TQuery: OperationType>(
154
176
  return data;
155
177
  }
156
178
 
157
- function usePreloadedQuery<TQuery: OperationType>(
158
- gqlQuery: GraphQLTaggedNode,
159
- preloadedQuery: PreloadedQuery<TQuery>,
160
- options?: {|
161
- UNSTABLE_renderPolicy?: RenderPolicy,
162
- |},
163
- ): TQuery['response'] {
164
- const impl = HooksImplementation.get();
165
- if (impl) {
166
- return impl.usePreloadedQuery(gqlQuery, preloadedQuery, options);
167
- } else {
168
- // eslint-disable-next-line react-hooks/rules-of-hooks
169
- return usePreloadedQuery_LEGACY(gqlQuery, preloadedQuery, options);
170
- }
171
- }
172
-
173
- module.exports = (usePreloadedQuery: UsePreloadedQueryHookType);
179
+ module.exports = usePreloadedQuery;