react-relay 11.0.0 → 13.0.0-rc.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. package/ReactRelayContext.js +1 -1
  2. package/ReactRelayContext.js.flow +2 -3
  3. package/ReactRelayFragmentContainer.js.flow +24 -24
  4. package/ReactRelayFragmentMockRenderer.js.flow +1 -1
  5. package/ReactRelayLocalQueryRenderer.js.flow +7 -8
  6. package/ReactRelayPaginationContainer.js.flow +111 -54
  7. package/ReactRelayQueryFetcher.js.flow +9 -10
  8. package/ReactRelayQueryRenderer.js.flow +115 -81
  9. package/ReactRelayRefetchContainer.js.flow +40 -35
  10. package/ReactRelayTestMocker.js.flow +16 -12
  11. package/ReactRelayTypes.js.flow +10 -10
  12. package/RelayContext.js.flow +3 -3
  13. package/__flowtests__/ReactRelayFragmentContainer-flowtest.js.flow +1 -2
  14. package/__flowtests__/ReactRelayPaginationContainer-flowtest.js.flow +12 -7
  15. package/__flowtests__/ReactRelayRefetchContainer-flowtest.js.flow +10 -6
  16. package/__flowtests__/RelayModern-flowtest.js.flow +78 -46
  17. package/__flowtests__/RelayModernFlowtest_badref.graphql.js.flow +5 -4
  18. package/__flowtests__/RelayModernFlowtest_notref.graphql.js.flow +5 -4
  19. package/__flowtests__/RelayModernFlowtest_user.graphql.js.flow +4 -3
  20. package/__flowtests__/RelayModernFlowtest_users.graphql.js.flow +4 -3
  21. package/__flowtests__/__generated__/ReactRelayFragmentContainerFlowtest_viewer.graphql.js.flow +72 -0
  22. package/__flowtests__/__generated__/ReactRelayFragmentContainerFlowtest_viewer2.graphql.js.flow +72 -0
  23. package/__flowtests__/__generated__/ReactRelayPaginationContainerFlowtestQuery.graphql.js.flow +227 -0
  24. package/__flowtests__/__generated__/ReactRelayPaginationContainerFlowtest_viewer.graphql.js.flow +164 -0
  25. package/__flowtests__/__generated__/ReactRelayRefetchContainerFlowtestQuery.graphql.js.flow +227 -0
  26. package/__flowtests__/__generated__/ReactRelayRefetchContainerFlowtest_viewer.graphql.js.flow +164 -0
  27. package/__flowtests__/__generated__/RelayModernFlowtest_badref.graphql.js.flow +66 -0
  28. package/__flowtests__/__generated__/RelayModernFlowtest_notref.graphql.js.flow +66 -0
  29. package/__flowtests__/__generated__/RelayModernFlowtest_user.graphql.js.flow +59 -0
  30. package/__flowtests__/__generated__/RelayModernFlowtest_users.graphql.js.flow +61 -0
  31. package/assertFragmentMap.js.flow +2 -2
  32. package/buildReactRelayContainer.js.flow +15 -12
  33. package/getRootVariablesForFragments.js.flow +2 -3
  34. package/hooks.js +1 -1
  35. package/hooks.js.flow +5 -6
  36. package/index.js +1 -1
  37. package/index.js.flow +6 -7
  38. package/jest-react/enqueueTask.js.flow +56 -0
  39. package/jest-react/index.js.flow +12 -0
  40. package/jest-react/internalAct.js.flow +139 -0
  41. package/legacy.js +1 -1
  42. package/lib/ReactRelayFragmentContainer.js +21 -15
  43. package/lib/ReactRelayFragmentMockRenderer.js +2 -2
  44. package/lib/ReactRelayLocalQueryRenderer.js +7 -8
  45. package/lib/ReactRelayPaginationContainer.js +92 -30
  46. package/lib/ReactRelayQueryFetcher.js +3 -3
  47. package/lib/ReactRelayQueryRenderer.js +86 -53
  48. package/lib/ReactRelayRefetchContainer.js +36 -21
  49. package/lib/ReactRelayTestMocker.js +7 -6
  50. package/lib/RelayContext.js +3 -2
  51. package/lib/assertFragmentMap.js +3 -2
  52. package/lib/buildReactRelayContainer.js +14 -11
  53. package/lib/hooks.js +5 -5
  54. package/lib/index.js +7 -7
  55. package/lib/jest-react/enqueueTask.js +53 -0
  56. package/lib/jest-react/index.js +13 -0
  57. package/lib/jest-react/internalAct.js +116 -0
  58. package/lib/multi-actor/ActorChange.js +30 -0
  59. package/lib/multi-actor/index.js +11 -0
  60. package/lib/multi-actor/useRelayActorEnvironment.js +29 -0
  61. package/lib/relay-hooks/EntryPointContainer.react.js +3 -3
  62. package/lib/relay-hooks/FragmentResource.js +351 -94
  63. package/lib/relay-hooks/LRUCache.js +1 -1
  64. package/lib/relay-hooks/LazyLoadEntryPointContainer_DEPRECATED.react.js +4 -4
  65. package/lib/relay-hooks/MatchContainer.js +1 -1
  66. package/lib/relay-hooks/QueryResource.js +172 -29
  67. package/lib/relay-hooks/RelayEnvironmentProvider.js +5 -3
  68. package/lib/relay-hooks/SuspenseResource.js +130 -0
  69. package/lib/relay-hooks/loadQuery.js +42 -20
  70. package/lib/relay-hooks/preloadQuery_DEPRECATED.js +24 -15
  71. package/lib/relay-hooks/useBlockingPaginationFragment.js +4 -5
  72. package/lib/relay-hooks/useEntryPointLoader.js +2 -2
  73. package/lib/relay-hooks/useFetchTrackingRef.js +2 -1
  74. package/lib/relay-hooks/useFragment.js +8 -7
  75. package/lib/relay-hooks/useFragmentNode.js +4 -4
  76. package/lib/relay-hooks/useIsOperationNodeActive.js +3 -3
  77. package/lib/relay-hooks/useLazyLoadQuery.js +3 -3
  78. package/lib/relay-hooks/useLazyLoadQueryNode.js +10 -4
  79. package/lib/relay-hooks/useLoadMoreFunction.js +6 -8
  80. package/lib/relay-hooks/useMemoOperationDescriptor.js +2 -2
  81. package/lib/relay-hooks/useMemoVariables.js +2 -2
  82. package/lib/relay-hooks/useMutation.js +17 -6
  83. package/lib/relay-hooks/usePaginationFragment.js +2 -3
  84. package/lib/relay-hooks/usePreloadedQuery.js +8 -7
  85. package/lib/relay-hooks/useQueryLoader.js +30 -10
  86. package/lib/relay-hooks/useRefetchableFragmentNode.js +12 -14
  87. package/lib/relay-hooks/useRelayEnvironment.js +3 -3
  88. package/lib/relay-hooks/useStaticFragmentNodeWarning.js +2 -2
  89. package/lib/relay-hooks/useSubscribeToInvalidationState.js +2 -1
  90. package/lib/relay-hooks/useSubscription.js +10 -7
  91. package/multi-actor/ActorChange.js.flow +58 -0
  92. package/multi-actor/index.js.flow +14 -0
  93. package/multi-actor/useRelayActorEnvironment.js.flow +49 -0
  94. package/package.json +3 -2
  95. package/react-relay-hooks.js +2 -2
  96. package/react-relay-hooks.min.js +2 -2
  97. package/react-relay-legacy.js +2 -2
  98. package/react-relay-legacy.min.js +2 -2
  99. package/react-relay.js +2 -2
  100. package/react-relay.min.js +2 -2
  101. package/relay-hooks/EntryPointContainer.react.js.flow +8 -15
  102. package/relay-hooks/EntryPointTypes.flow.js.flow +24 -25
  103. package/relay-hooks/FragmentResource.js.flow +376 -95
  104. package/relay-hooks/LazyLoadEntryPointContainer_DEPRECATED.react.js.flow +32 -46
  105. package/relay-hooks/MatchContainer.js.flow +3 -2
  106. package/relay-hooks/QueryResource.js.flow +216 -25
  107. package/relay-hooks/RelayEnvironmentProvider.js.flow +14 -4
  108. package/relay-hooks/SuspenseResource.js.flow +115 -0
  109. package/relay-hooks/__flowtests__/EntryPointTypes/EntryPointElementConfig-flowtest.js.flow +4 -3
  110. package/relay-hooks/__flowtests__/EntryPointTypes/NestedEntrypoints-flowtest.js.flow +1 -1
  111. package/relay-hooks/__flowtests__/useBlockingPaginationFragment-flowtest.js.flow +10 -9
  112. package/relay-hooks/__flowtests__/useFragment-flowtest.js.flow +8 -7
  113. package/relay-hooks/__flowtests__/usePaginationFragment-flowtest.js.flow +10 -9
  114. package/relay-hooks/__flowtests__/useRefetchableFragment-flowtest.js.flow +10 -9
  115. package/relay-hooks/__flowtests__/utils.js.flow +8 -12
  116. package/relay-hooks/loadEntryPoint.js.flow +6 -12
  117. package/relay-hooks/loadQuery.js.flow +49 -31
  118. package/relay-hooks/preloadQuery_DEPRECATED.js.flow +30 -21
  119. package/relay-hooks/prepareEntryPoint_DEPRECATED.js.flow +6 -12
  120. package/relay-hooks/useBlockingPaginationFragment.js.flow +13 -11
  121. package/relay-hooks/useEntryPointLoader.js.flow +7 -10
  122. package/relay-hooks/useFetchTrackingRef.js.flow +2 -2
  123. package/relay-hooks/useFragment.js.flow +26 -46
  124. package/relay-hooks/useFragmentNode.js.flow +5 -7
  125. package/relay-hooks/useIsOperationNodeActive.js.flow +3 -5
  126. package/relay-hooks/useIsParentQueryActive.js.flow +3 -4
  127. package/relay-hooks/useLazyLoadQuery.js.flow +9 -10
  128. package/relay-hooks/useLazyLoadQueryNode.js.flow +19 -13
  129. package/relay-hooks/useLoadMoreFunction.js.flow +20 -27
  130. package/relay-hooks/useMemoOperationDescriptor.js.flow +5 -7
  131. package/relay-hooks/useMemoVariables.js.flow +6 -6
  132. package/relay-hooks/useMutation.js.flow +26 -26
  133. package/relay-hooks/usePaginationFragment.js.flow +38 -44
  134. package/relay-hooks/usePreloadedQuery.js.flow +18 -14
  135. package/relay-hooks/useQueryLoader.js.flow +41 -22
  136. package/relay-hooks/useRefetchableFragment.js.flow +7 -8
  137. package/relay-hooks/useRefetchableFragmentNode.js.flow +24 -30
  138. package/relay-hooks/useRelayEnvironment.js.flow +2 -4
  139. package/relay-hooks/useStaticFragmentNodeWarning.js.flow +2 -3
  140. package/relay-hooks/useSubscribeToInvalidationState.js.flow +3 -6
  141. package/relay-hooks/useSubscription.js.flow +20 -10
  142. package/lib/relay-hooks/getPaginationMetadata.js +0 -41
  143. package/lib/relay-hooks/getPaginationVariables.js +0 -66
  144. package/lib/relay-hooks/getRefetchMetadata.js +0 -36
  145. package/lib/relay-hooks/getValueAtPath.js +0 -51
  146. package/relay-hooks/getPaginationMetadata.js.flow +0 -74
  147. package/relay-hooks/getPaginationVariables.js.flow +0 -108
  148. package/relay-hooks/getRefetchMetadata.js.flow +0 -80
  149. package/relay-hooks/getValueAtPath.js.flow +0 -46
@@ -13,24 +13,26 @@
13
13
 
14
14
  'use strict';
15
15
 
16
- const getPaginationMetadata = require('./getPaginationMetadata');
17
- const useLoadMoreFunction = require('./useLoadMoreFunction');
18
- const useRefetchableFragmentNode = require('./useRefetchableFragmentNode');
19
- const useStaticFragmentNodeWarning = require('./useStaticFragmentNodeWarning');
20
-
21
- const {useCallback, useDebugValue, useState} = require('react');
22
- const {getFragment, getFragmentIdentifier} = require('relay-runtime');
23
-
24
16
  import type {LoadMoreFn, UseLoadMoreFunctionArgs} from './useLoadMoreFunction';
25
17
  import type {RefetchFnDynamic} from './useRefetchableFragmentNode';
26
18
  import type {
19
+ FragmentType,
27
20
  GraphQLResponse,
28
21
  GraphQLTaggedNode,
29
22
  Observer,
30
23
  OperationType,
31
- FragmentReference,
32
24
  } from 'relay-runtime';
33
25
 
26
+ const useLoadMoreFunction = require('./useLoadMoreFunction');
27
+ const useRefetchableFragmentNode = require('./useRefetchableFragmentNode');
28
+ const useStaticFragmentNodeWarning = require('./useStaticFragmentNodeWarning');
29
+ const {useCallback, useDebugValue, useState} = require('react');
30
+ const {
31
+ getFragment,
32
+ getFragmentIdentifier,
33
+ getPaginationMetadata,
34
+ } = require('relay-runtime');
35
+
34
36
  export type ReturnType<TQuery: OperationType, TKey, TFragmentData> = {|
35
37
  data: TFragmentData,
36
38
  loadNext: LoadMoreFn<TQuery>,
@@ -44,7 +46,7 @@ export type ReturnType<TQuery: OperationType, TKey, TFragmentData> = {|
44
46
 
45
47
  function usePaginationFragment<
46
48
  TQuery: OperationType,
47
- TKey: ?{+$data?: mixed, +$fragmentRefs: FragmentReference, ...},
49
+ TKey: ?{+$data?: mixed, +$fragmentSpreads: FragmentType, ...},
48
50
  >(
49
51
  fragmentInput: GraphQLTaggedNode,
50
52
  parentFragmentRef: TKey,
@@ -82,42 +84,34 @@ function usePaginationFragment<
82
84
  const fragmentIdentifier = getFragmentIdentifier(fragmentNode, fragmentRef);
83
85
 
84
86
  // Backward pagination
85
- const [
86
- loadPrevious,
87
- hasPrevious,
88
- isLoadingPrevious,
89
- disposeFetchPrevious,
90
- ] = useLoadMore<TQuery>({
91
- componentDisplayName,
92
- connectionPathInFragmentData,
93
- direction: 'backward',
94
- fragmentData,
95
- fragmentIdentifier,
96
- fragmentNode,
97
- fragmentRef,
98
- identifierField,
99
- paginationMetadata,
100
- paginationRequest,
101
- });
87
+ const [loadPrevious, hasPrevious, isLoadingPrevious, disposeFetchPrevious] =
88
+ useLoadMore<TQuery>({
89
+ componentDisplayName,
90
+ connectionPathInFragmentData,
91
+ direction: 'backward',
92
+ fragmentData,
93
+ fragmentIdentifier,
94
+ fragmentNode,
95
+ fragmentRef,
96
+ identifierField,
97
+ paginationMetadata,
98
+ paginationRequest,
99
+ });
102
100
 
103
101
  // Forward pagination
104
- const [
105
- loadNext,
106
- hasNext,
107
- isLoadingNext,
108
- disposeFetchNext,
109
- ] = useLoadMore<TQuery>({
110
- componentDisplayName,
111
- connectionPathInFragmentData,
112
- direction: 'forward',
113
- fragmentData,
114
- fragmentIdentifier,
115
- fragmentNode,
116
- fragmentRef,
117
- identifierField,
118
- paginationMetadata,
119
- paginationRequest,
120
- });
102
+ const [loadNext, hasNext, isLoadingNext, disposeFetchNext] =
103
+ useLoadMore<TQuery>({
104
+ componentDisplayName,
105
+ connectionPathInFragmentData,
106
+ direction: 'forward',
107
+ fragmentData,
108
+ fragmentIdentifier,
109
+ fragmentNode,
110
+ fragmentRef,
111
+ identifierField,
112
+ paginationMetadata,
113
+ paginationRequest,
114
+ });
121
115
 
122
116
  const refetchPagination: RefetchFnDynamic<TQuery, TKey> = useCallback(
123
117
  (variables, options) => {
@@ -13,24 +13,23 @@
13
13
 
14
14
  'use strict';
15
15
 
16
- const invariant = require('invariant');
16
+ import type {PreloadedQuery} from './EntryPointTypes.flow';
17
+ import type {
18
+ GraphQLTaggedNode,
19
+ OperationType,
20
+ RenderPolicy,
21
+ } from 'relay-runtime';
22
+
23
+ const {useTrackLoadQueryInRender} = require('./loadQuery');
17
24
  const useLazyLoadQueryNode = require('./useLazyLoadQueryNode');
18
25
  const useMemoOperationDescriptor = require('./useMemoOperationDescriptor');
19
26
  const useRelayEnvironment = require('./useRelayEnvironment');
20
- const warning = require('warning');
21
-
22
- const {useTrackLoadQueryInRender} = require('./loadQuery');
27
+ const invariant = require('invariant');
23
28
  const {useDebugValue} = require('react');
24
29
  const {
25
30
  __internal: {fetchQueryDeduped, fetchQuery},
26
31
  } = require('relay-runtime');
27
-
28
- import type {PreloadedQuery} from './EntryPointTypes.flow';
29
- import type {
30
- GraphQLTaggedNode,
31
- OperationType,
32
- RenderPolicy,
33
- } from 'relay-runtime';
32
+ const warning = require('warning');
34
33
 
35
34
  function usePreloadedQuery<TQuery: OperationType>(
36
35
  gqlQuery: GraphQLTaggedNode,
@@ -38,14 +37,19 @@ function usePreloadedQuery<TQuery: OperationType>(
38
37
  options?: {|
39
38
  UNSTABLE_renderPolicy?: RenderPolicy,
40
39
  |},
41
- ): $ElementType<TQuery, 'response'> {
40
+ ): TQuery['response'] {
42
41
  // We need to use this hook in order to be able to track if
43
42
  // loadQuery was called during render
44
43
  useTrackLoadQueryInRender();
45
44
 
46
45
  const environment = useRelayEnvironment();
47
- const {fetchKey, fetchPolicy, source, variables} = preloadedQuery;
48
- const operation = useMemoOperationDescriptor(gqlQuery, variables);
46
+ const {fetchKey, fetchPolicy, source, variables, networkCacheConfig} =
47
+ preloadedQuery;
48
+ const operation = useMemoOperationDescriptor(
49
+ gqlQuery,
50
+ variables,
51
+ networkCacheConfig,
52
+ );
49
53
 
50
54
  let useLazyLoadQueryNodeParams;
51
55
  if (preloadedQuery.kind === 'PreloadedQuery_DEPRECATED') {
@@ -13,15 +13,9 @@
13
13
 
14
14
  'use strict';
15
15
 
16
- const useIsMountedRef = require('./useIsMountedRef');
17
- const useRelayEnvironment = require('./useRelayEnvironment');
18
-
19
- const {loadQuery, useTrackLoadQueryInRender} = require('./loadQuery');
20
- const {useCallback, useEffect, useRef, useState} = require('react');
21
-
22
16
  import type {
23
- PreloadableConcreteRequest,
24
17
  LoadQueryOptions,
18
+ PreloadableConcreteRequest,
25
19
  PreloadedQuery,
26
20
  } from './EntryPointTypes.flow';
27
21
  import type {
@@ -30,8 +24,14 @@ import type {
30
24
  OperationType,
31
25
  } from 'relay-runtime';
32
26
 
27
+ const {loadQuery, useTrackLoadQueryInRender} = require('./loadQuery');
28
+ const useIsMountedRef = require('./useIsMountedRef');
29
+ const useRelayEnvironment = require('./useRelayEnvironment');
30
+ const {useCallback, useEffect, useRef, useState} = require('react');
31
+ const {getRequest} = require('relay-runtime');
32
+
33
33
  export type LoaderFn<TQuery: OperationType> = (
34
- variables: $ElementType<TQuery, 'variables'>,
34
+ variables: TQuery['variables'],
35
35
  options?: UseQueryLoaderLoadQueryOptions,
36
36
  ) => void;
37
37
 
@@ -54,6 +54,16 @@ type NullQueryReference = {|
54
54
  |};
55
55
  const initialNullQueryReferenceState = {kind: 'NullQueryReference'};
56
56
 
57
+ function requestIsLiveQuery<TQuery: OperationType>(
58
+ preloadableRequest: GraphQLTaggedNode | PreloadableConcreteRequest<TQuery>,
59
+ ): boolean {
60
+ if (preloadableRequest.kind === 'PreloadableConcreteRequest') {
61
+ return (preloadableRequest: $FlowFixMe).params.metadata.live !== undefined;
62
+ }
63
+ const request = getRequest(preloadableRequest);
64
+ return request.params.metadata.live !== undefined;
65
+ }
66
+
57
67
  function useQueryLoader<TQuery: OperationType>(
58
68
  preloadableRequest: GraphQLTaggedNode | PreloadableConcreteRequest<TQuery>,
59
69
  initialQueryReference?: ?PreloadedQuery<TQuery>,
@@ -93,12 +103,10 @@ function useQueryLoader<TQuery: OperationType>(
93
103
  PreloadedQuery<TQuery> | NullQueryReference,
94
104
  >(() => initialQueryReferenceInternal);
95
105
 
96
- const [
97
- previousInitialQueryReference,
98
- setPreviousInitialQueryReference,
99
- ] = useState<PreloadedQuery<TQuery> | NullQueryReference>(
100
- () => initialQueryReferenceInternal,
101
- );
106
+ const [previousInitialQueryReference, setPreviousInitialQueryReference] =
107
+ useState<PreloadedQuery<TQuery> | NullQueryReference>(
108
+ () => initialQueryReferenceInternal,
109
+ );
102
110
 
103
111
  if (initialQueryReferenceInternal !== previousInitialQueryReference) {
104
112
  // Rendering the query reference makes it "managed" by this hook, so
@@ -120,7 +128,7 @@ function useQueryLoader<TQuery: OperationType>(
120
128
 
121
129
  const queryLoaderCallback = useCallback(
122
130
  (
123
- variables: $ElementType<TQuery, 'variables'>,
131
+ variables: TQuery['variables'],
124
132
  options?: ?UseQueryLoaderLoadQueryOptions,
125
133
  ) => {
126
134
  const mergedOptions: ?UseQueryLoaderLoadQueryOptions =
@@ -209,24 +217,35 @@ function useQueryLoader<TQuery: OperationType>(
209
217
 
210
218
  undisposedQueryReferences.delete(undisposedQueryReference);
211
219
  if (undisposedQueryReference.kind !== 'NullQueryReference') {
212
- undisposedQueryReference.dispose &&
213
- undisposedQueryReference.dispose();
220
+ if (requestIsLiveQuery(preloadableRequest)) {
221
+ undisposedQueryReference.dispose &&
222
+ undisposedQueryReference.dispose();
223
+ } else {
224
+ undisposedQueryReference.releaseQuery &&
225
+ undisposedQueryReference.releaseQuery();
226
+ }
214
227
  }
215
228
  }
216
229
  }
217
- }, [queryReference, isMountedRef, queryLoaderCallback]);
230
+ }, [queryReference, isMountedRef, queryLoaderCallback, preloadableRequest]);
218
231
 
219
232
  useEffect(() => {
220
233
  return function disposeAllRemainingQueryReferences() {
221
234
  // undisposedQueryReferences.current is never reassigned
222
235
  // eslint-disable-next-line react-hooks/exhaustive-deps
223
- for (const unhandledStateChange of undisposedQueryReferencesRef.current) {
224
- if (unhandledStateChange.kind !== 'NullQueryReference') {
225
- unhandledStateChange.dispose && unhandledStateChange.dispose();
236
+ for (const undisposedQueryReference of undisposedQueryReferencesRef.current) {
237
+ if (undisposedQueryReference.kind !== 'NullQueryReference') {
238
+ if (requestIsLiveQuery(preloadableRequest)) {
239
+ undisposedQueryReference.dispose &&
240
+ undisposedQueryReference.dispose();
241
+ } else {
242
+ undisposedQueryReference.releaseQuery &&
243
+ undisposedQueryReference.releaseQuery();
244
+ }
226
245
  }
227
246
  }
228
247
  };
229
- }, []);
248
+ }, [preloadableRequest]);
230
249
 
231
250
  return [
232
251
  queryReference.kind === 'NullQueryReference' ? null : queryReference,
@@ -13,19 +13,18 @@
13
13
 
14
14
  'use strict';
15
15
 
16
- const useRefetchableFragmentNode = require('./useRefetchableFragmentNode');
17
- const useStaticFragmentNodeWarning = require('./useStaticFragmentNodeWarning');
18
-
19
- const {useDebugValue} = require('react');
20
- const {getFragment} = require('relay-runtime');
21
-
22
16
  import type {RefetchFnDynamic} from './useRefetchableFragmentNode';
23
17
  import type {
18
+ FragmentType,
24
19
  GraphQLTaggedNode,
25
20
  OperationType,
26
- FragmentReference,
27
21
  } from 'relay-runtime';
28
22
 
23
+ const useRefetchableFragmentNode = require('./useRefetchableFragmentNode');
24
+ const useStaticFragmentNodeWarning = require('./useStaticFragmentNodeWarning');
25
+ const {useDebugValue} = require('react');
26
+ const {getFragment} = require('relay-runtime');
27
+
29
28
  type ReturnType<TQuery: OperationType, TKey: ?{+$data?: mixed, ...}> = [
30
29
  // NOTE: This $Call ensures that the type of the returned data is either:
31
30
  // - nullable if the provided ref type is nullable
@@ -41,7 +40,7 @@ type ReturnType<TQuery: OperationType, TKey: ?{+$data?: mixed, ...}> = [
41
40
 
42
41
  function useRefetchableFragment<
43
42
  TQuery: OperationType,
44
- TKey: ?{+$data?: mixed, +$fragmentRefs: FragmentReference, ...},
43
+ TKey: ?{+$data?: mixed, +$fragmentSpreads: FragmentType, ...},
45
44
  >(
46
45
  fragmentInput: GraphQLTaggedNode,
47
46
  fragmentRef: TKey,
@@ -13,27 +13,6 @@
13
13
 
14
14
  'use strict';
15
15
 
16
- const ProfilerContext = require('./ProfilerContext');
17
-
18
- const getRefetchMetadata = require('./getRefetchMetadata');
19
- const getValueAtPath = require('./getValueAtPath');
20
- const invariant = require('invariant');
21
- const useFragmentNode = require('./useFragmentNode');
22
- const useIsMountedRef = require('./useIsMountedRef');
23
- const useQueryLoader = require('./useQueryLoader');
24
- const useRelayEnvironment = require('./useRelayEnvironment');
25
- const warning = require('warning');
26
-
27
- const {getFragmentResourceForEnvironment} = require('./FragmentResource');
28
- const {getQueryResourceForEnvironment} = require('./QueryResource');
29
- const {useCallback, useContext, useReducer} = require('react');
30
- const {
31
- __internal: {fetchQuery},
32
- createOperationDescriptor,
33
- getFragmentIdentifier,
34
- getSelector,
35
- } = require('relay-runtime');
36
-
37
16
  import type {LoaderFn} from './useQueryLoader';
38
17
  import type {
39
18
  Disposable,
@@ -47,6 +26,25 @@ import type {
47
26
  VariablesOf,
48
27
  } from 'relay-runtime';
49
28
 
29
+ const {getFragmentResourceForEnvironment} = require('./FragmentResource');
30
+ const ProfilerContext = require('./ProfilerContext');
31
+ const {getQueryResourceForEnvironment} = require('./QueryResource');
32
+ const useFragmentNode = require('./useFragmentNode');
33
+ const useIsMountedRef = require('./useIsMountedRef');
34
+ const useQueryLoader = require('./useQueryLoader');
35
+ const useRelayEnvironment = require('./useRelayEnvironment');
36
+ const invariant = require('invariant');
37
+ const {useCallback, useContext, useReducer} = require('react');
38
+ const {
39
+ __internal: {fetchQuery},
40
+ createOperationDescriptor,
41
+ getFragmentIdentifier,
42
+ getRefetchMetadata,
43
+ getSelector,
44
+ getValueAtPath,
45
+ } = require('relay-runtime');
46
+ const warning = require('warning');
47
+
50
48
  export type RefetchFn<
51
49
  TQuery: OperationType,
52
50
  TOptions = Options,
@@ -176,11 +174,8 @@ function useRefetchableFragmentNode<
176
174
  componentDisplayName: string,
177
175
  ): ReturnType<TQuery, TKey, InternalOptions> {
178
176
  const parentEnvironment = useRelayEnvironment();
179
- const {
180
- refetchableRequest,
181
- fragmentRefPathInResponse,
182
- identifierField,
183
- } = getRefetchMetadata(fragmentNode, componentDisplayName);
177
+ const {refetchableRequest, fragmentRefPathInResponse, identifierField} =
178
+ getRefetchMetadata(fragmentNode, componentDisplayName);
184
179
  const fragmentIdentifier = getFragmentIdentifier(
185
180
  fragmentNode,
186
181
  parentFragmentRef,
@@ -213,9 +208,8 @@ function useRefetchableFragmentNode<
213
208
  const shouldReset =
214
209
  environment !== mirroredEnvironment ||
215
210
  fragmentIdentifier !== mirroredFragmentIdentifier;
216
- const [queryRef, loadQuery, disposeQuery] = useQueryLoader<TQuery>(
217
- refetchableRequest,
218
- );
211
+ const [queryRef, loadQuery, disposeQuery] =
212
+ useQueryLoader<TQuery>(refetchableRequest);
219
213
 
220
214
  let fragmentRef = parentFragmentRef;
221
215
  if (shouldReset) {
@@ -499,7 +493,7 @@ function useRefetchFunction<TQuery: OperationType>(
499
493
  // - fragmentNode and parentFragmentRef are also captured by including
500
494
  // fragmentIdentifier
501
495
  // eslint-disable-next-line react-hooks/exhaustive-deps
502
- [fragmentIdentifier, dispatch, disposeQuery, identifierValue],
496
+ [fragmentIdentifier, dispatch, disposeQuery, identifierValue, loadQuery],
503
497
  );
504
498
  }
505
499
 
@@ -13,13 +13,11 @@
13
13
 
14
14
  'use strict';
15
15
 
16
- const ReactRelayContext = require('react-relay/ReactRelayContext');
16
+ import type {IEnvironment} from 'relay-runtime';
17
17
 
18
18
  const invariant = require('invariant');
19
-
20
19
  const {useContext} = require('react');
21
-
22
- import type {IEnvironment} from 'relay-runtime';
20
+ const ReactRelayContext = require('react-relay/ReactRelayContext');
23
21
 
24
22
  function useRelayEnvironment(): IEnvironment {
25
23
  const context = useContext(ReactRelayContext);
@@ -13,11 +13,10 @@
13
13
 
14
14
  'use strict';
15
15
 
16
- const warning = require('warning');
16
+ import type {ReaderFragment} from 'relay-runtime';
17
17
 
18
18
  const {useRef} = require('react');
19
-
20
- import type {ReaderFragment} from 'relay-runtime';
19
+ const warning = require('warning');
21
20
 
22
21
  function useStaticFragmentNodeWarning(
23
22
  fragmentNode: ReaderFragment,
@@ -13,12 +13,11 @@
13
13
 
14
14
  'use strict';
15
15
 
16
- const useRelayEnvironment = require('./useRelayEnvironment');
16
+ import type {DataID, Disposable, InvalidationState} from 'relay-runtime';
17
17
 
18
+ const useRelayEnvironment = require('./useRelayEnvironment');
18
19
  const {useEffect, useRef} = require('react');
19
20
 
20
- import type {DataID, Disposable, InvalidationState} from 'relay-runtime';
21
-
22
21
  /**
23
22
  * This hook subscribes a callback to the invalidation state of the given data
24
23
  * ids.
@@ -35,9 +34,7 @@ function useSubscribeToInvalidationState(
35
34
  const environment = useRelayEnvironment();
36
35
  const disposableRef = useRef(null);
37
36
 
38
- const stableDataIDs = Array.from(dataIDs)
39
- .sort()
40
- .join('');
37
+ const stableDataIDs = Array.from(dataIDs).sort().join('');
41
38
  useEffect(() => {
42
39
  const store = environment.getStore();
43
40
  const invalidationState = store.lookupInvalidationState(dataIDs);
@@ -12,26 +12,36 @@
12
12
 
13
13
  'use strict';
14
14
 
15
- const React = require('react');
15
+ import type {
16
+ Disposable,
17
+ GraphQLSubscriptionConfig,
18
+ IEnvironment,
19
+ SubscriptionParameters,
20
+ } from 'relay-runtime';
16
21
 
17
22
  const useRelayEnvironment = require('./useRelayEnvironment');
18
-
23
+ const {useEffect} = require('react');
19
24
  const {requestSubscription} = require('relay-runtime');
20
25
 
21
- import type {GraphQLSubscriptionConfig} from 'relay-runtime';
26
+ type RequestSubscriptionFn<TSubscriptionParameters: SubscriptionParameters> = (
27
+ environment: IEnvironment,
28
+ config: GraphQLSubscriptionConfig<TSubscriptionParameters>,
29
+ ) => Disposable;
22
30
 
23
- function useSubscription<TSubscriptionPayload>(
24
- config: GraphQLSubscriptionConfig<TSubscriptionPayload>,
25
- requestSubscriptionFn?: typeof requestSubscription,
31
+ function useSubscription<TSubscriptionParameters: SubscriptionParameters>(
32
+ config: GraphQLSubscriptionConfig<TSubscriptionParameters>,
33
+ requestSubscriptionFn?: RequestSubscriptionFn<TSubscriptionParameters>,
26
34
  ): void {
27
35
  // N.B. this will re-subscribe every render if config or requestSubscriptionFn
28
36
  // are not memoized.
29
37
  // Please do not pass an object defined in-line.
30
- const actualRequestSubscription =
31
- requestSubscriptionFn ?? requestSubscription;
38
+ const actualRequestSubscription: RequestSubscriptionFn<TSubscriptionParameters> =
39
+ requestSubscriptionFn ?? (requestSubscription: $FlowFixMe);
32
40
  const environment = useRelayEnvironment();
33
- React.useEffect(() => {
34
- const {dispose} = requestSubscription(environment, config);
41
+ useEffect(() => {
42
+ // $FlowFixMe[method-unbinding] added when improving typing for this parameters
43
+ const {dispose} = actualRequestSubscription(environment, config);
44
+ // $FlowFixMe[incompatible-call]
35
45
  return dispose;
36
46
  }, [environment, config, actualRequestSubscription]);
37
47
  }
@@ -1,41 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- * @emails oncall+relay
8
- *
9
- * @format
10
- */
11
- // flowlint ambiguous-object-type:error
12
- 'use strict';
13
-
14
- var getRefetchMetadata = require('./getRefetchMetadata');
15
-
16
- var invariant = require("fbjs/lib/invariant");
17
-
18
- function getPaginationMetadata(fragmentNode, componentDisplayName) {
19
- var _fragmentNode$metadat, _fragmentNode$metadat2;
20
-
21
- var _getRefetchMetadata = getRefetchMetadata(fragmentNode, componentDisplayName),
22
- paginationRequest = _getRefetchMetadata.refetchableRequest,
23
- refetchMetadata = _getRefetchMetadata.refetchMetadata;
24
-
25
- var paginationMetadata = refetchMetadata.connection;
26
- !(paginationMetadata != null) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: getPaginationMetadata(): Expected fragment `%s` to include a ' + 'connection when using `%s`. Did you forget to add a @connection ' + 'directive to the connection field in the fragment?', componentDisplayName, fragmentNode.name) : invariant(false) : void 0;
27
- var connectionPathInFragmentData = paginationMetadata.path;
28
- var connectionMetadata = ((_fragmentNode$metadat = (_fragmentNode$metadat2 = fragmentNode.metadata) === null || _fragmentNode$metadat2 === void 0 ? void 0 : _fragmentNode$metadat2.connection) !== null && _fragmentNode$metadat !== void 0 ? _fragmentNode$metadat : [])[0];
29
- !(connectionMetadata != null) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: getPaginationMetadata(): Expected fragment `%s` to include a ' + 'connection when using `%s`. Did you forget to add a @connection ' + 'directive to the connection field in the fragment?', componentDisplayName, fragmentNode.name) : invariant(false) : void 0;
30
- var identifierField = refetchMetadata.identifierField;
31
- !(identifierField == null || typeof identifierField === 'string') ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: getRefetchMetadata(): Expected `identifierField` to be a string.') : invariant(false) : void 0;
32
- return {
33
- connectionPathInFragmentData: connectionPathInFragmentData,
34
- identifierField: identifierField,
35
- paginationRequest: paginationRequest,
36
- paginationMetadata: paginationMetadata,
37
- stream: connectionMetadata.stream === true
38
- };
39
- }
40
-
41
- module.exports = getPaginationMetadata;
@@ -1,66 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- * @emails oncall+relay
8
- *
9
- * @format
10
- */
11
- // flowlint ambiguous-object-type:error
12
- 'use strict';
13
-
14
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
15
-
16
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
17
-
18
- var _objectSpread4 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
19
-
20
- var invariant = require("fbjs/lib/invariant");
21
-
22
- var warning = require("fbjs/lib/warning");
23
-
24
- function getPaginationVariables(direction, count, cursor, baseVariables, extraVariables, paginationMetadata) {
25
- var _objectSpread3;
26
-
27
- var backwardMetadata = paginationMetadata.backward,
28
- forwardMetadata = paginationMetadata.forward;
29
-
30
- if (direction === 'backward') {
31
- var _objectSpread2;
32
-
33
- !(backwardMetadata != null && backwardMetadata.count != null && backwardMetadata.cursor != null) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: Expected backward pagination metadata to be available. ' + "If you're seeing this, this is likely a bug in Relay.") : invariant(false) : void 0;
34
- process.env.NODE_ENV !== "production" ? warning(!extraVariables.hasOwnProperty(backwardMetadata.cursor), 'Relay: `UNSTABLE_extraVariables` provided by caller should not ' + 'contain cursor variable `%s`. This variable is automatically ' + 'determined by Relay.', backwardMetadata.cursor) : void 0;
35
- process.env.NODE_ENV !== "production" ? warning(!extraVariables.hasOwnProperty(backwardMetadata.count), 'Relay: `UNSTABLE_extraVariables` provided by caller should not ' + 'contain count variable `%s`. This variable is automatically ' + 'determined by Relay.', backwardMetadata.count) : void 0;
36
-
37
- var _paginationVariables = (0, _objectSpread4["default"])((0, _objectSpread4["default"])((0, _objectSpread4["default"])({}, baseVariables), extraVariables), {}, (_objectSpread2 = {}, (0, _defineProperty2["default"])(_objectSpread2, backwardMetadata.cursor, cursor), (0, _defineProperty2["default"])(_objectSpread2, backwardMetadata.count, count), _objectSpread2));
38
-
39
- if (forwardMetadata && forwardMetadata.cursor) {
40
- _paginationVariables[forwardMetadata.cursor] = null;
41
- }
42
-
43
- if (forwardMetadata && forwardMetadata.count) {
44
- _paginationVariables[forwardMetadata.count] = null;
45
- }
46
-
47
- return _paginationVariables;
48
- }
49
-
50
- !(forwardMetadata != null && forwardMetadata.count != null && forwardMetadata.cursor != null) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: Expected forward pagination metadata to be available. ' + "If you're seeing this, this is likely a bug in Relay.") : invariant(false) : void 0;
51
- process.env.NODE_ENV !== "production" ? warning(!extraVariables.hasOwnProperty(forwardMetadata.cursor), 'Relay: `UNSTABLE_extraVariables` provided by caller should not ' + 'contain cursor variable `%s`. This variable is automatically ' + 'determined by Relay.', forwardMetadata.cursor) : void 0;
52
- process.env.NODE_ENV !== "production" ? warning(!extraVariables.hasOwnProperty(forwardMetadata.count), 'Relay: `UNSTABLE_extraVariables` provided by caller should not ' + 'contain count variable `%s`. This variable is automatically ' + 'determined by Relay.', forwardMetadata.count) : void 0;
53
- var paginationVariables = (0, _objectSpread4["default"])((0, _objectSpread4["default"])((0, _objectSpread4["default"])({}, baseVariables), extraVariables), {}, (_objectSpread3 = {}, (0, _defineProperty2["default"])(_objectSpread3, forwardMetadata.cursor, cursor), (0, _defineProperty2["default"])(_objectSpread3, forwardMetadata.count, count), _objectSpread3));
54
-
55
- if (backwardMetadata && backwardMetadata.cursor) {
56
- paginationVariables[backwardMetadata.cursor] = null;
57
- }
58
-
59
- if (backwardMetadata && backwardMetadata.count) {
60
- paginationVariables[backwardMetadata.count] = null;
61
- }
62
-
63
- return paginationVariables;
64
- }
65
-
66
- module.exports = getPaginationVariables;
@@ -1,36 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- * @emails oncall+relay
8
- *
9
- * @format
10
- */
11
- // flowlint ambiguous-object-type:error
12
- 'use strict';
13
-
14
- var invariant = require("fbjs/lib/invariant");
15
-
16
- function getRefetchMetadata(fragmentNode, componentDisplayName) {
17
- var _fragmentNode$metadat, _fragmentNode$metadat2;
18
-
19
- !(((_fragmentNode$metadat = fragmentNode.metadata) === null || _fragmentNode$metadat === void 0 ? void 0 : _fragmentNode$metadat.plural) !== true) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: getRefetchMetadata(): Expected fragment `%s` not to be plural when using ' + '`%s`. Remove `@relay(plural: true)` from fragment `%s` ' + 'in order to use it with `%s`.', fragmentNode.name, componentDisplayName, fragmentNode.name, componentDisplayName) : invariant(false) : void 0;
20
- var refetchMetadata = (_fragmentNode$metadat2 = fragmentNode.metadata) === null || _fragmentNode$metadat2 === void 0 ? void 0 : _fragmentNode$metadat2.refetch;
21
- !(refetchMetadata != null) ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: getRefetchMetadata(): Expected fragment `%s` to be refetchable when using `%s`. ' + 'Did you forget to add a @refetchable directive to the fragment?', componentDisplayName, fragmentNode.name) : invariant(false) : void 0; // handle both commonjs and es modules
22
-
23
- var refetchableRequest = refetchMetadata.operation["default"] ? refetchMetadata.operation["default"] : refetchMetadata.operation;
24
- var fragmentRefPathInResponse = refetchMetadata.fragmentPathInResult;
25
- !(typeof refetchableRequest !== 'string') ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: getRefetchMetadata(): Expected refetch query to be an ' + "operation and not a string when using `%s`. If you're seeing this, " + 'this is likely a bug in Relay.', componentDisplayName) : invariant(false) : void 0;
26
- var identifierField = refetchMetadata.identifierField;
27
- !(identifierField == null || typeof identifierField === 'string') ? process.env.NODE_ENV !== "production" ? invariant(false, 'Relay: getRefetchMetadata(): Expected `identifierField` to be a string.') : invariant(false) : void 0;
28
- return {
29
- fragmentRefPathInResponse: fragmentRefPathInResponse,
30
- identifierField: identifierField,
31
- refetchableRequest: refetchableRequest,
32
- refetchMetadata: refetchMetadata
33
- };
34
- }
35
-
36
- module.exports = getRefetchMetadata;