react-relay 13.1.1 → 14.1.0
Sign up to get free protection for your applications and to get access to all the features.
- package/ReactRelayContainerUtils.js.flow +0 -2
- package/ReactRelayContext.js +1 -1
- package/ReactRelayContext.js.flow +0 -2
- package/ReactRelayFragmentContainer.js.flow +7 -6
- package/ReactRelayFragmentMockRenderer.js.flow +0 -2
- package/ReactRelayLocalQueryRenderer.js.flow +1 -3
- package/ReactRelayPaginationContainer.js.flow +13 -10
- package/ReactRelayQueryFetcher.js.flow +10 -11
- package/ReactRelayQueryRenderer.js.flow +15 -16
- package/ReactRelayQueryRendererContext.js.flow +1 -3
- package/ReactRelayRefetchContainer.js.flow +10 -7
- package/ReactRelayTestMocker.js.flow +0 -2
- package/ReactRelayTypes.js.flow +6 -8
- package/RelayContext.js.flow +0 -2
- package/__flowtests__/ReactRelayFragmentContainer-flowtest.js.flow +2 -4
- package/__flowtests__/ReactRelayPaginationContainer-flowtest.js.flow +3 -5
- package/__flowtests__/ReactRelayRefetchContainer-flowtest.js.flow +3 -5
- package/__flowtests__/RelayModern-flowtest.js.flow +2 -4
- package/__flowtests__/RelayModernFlowtest_badref.graphql.js.flow +2 -4
- package/__flowtests__/RelayModernFlowtest_notref.graphql.js.flow +2 -4
- package/__flowtests__/RelayModernFlowtest_user.graphql.js.flow +2 -4
- package/__flowtests__/RelayModernFlowtest_users.graphql.js.flow +2 -4
- package/__flowtests__/__generated__/ReactRelayFragmentContainerFlowtest_viewer.graphql.js.flow +2 -2
- package/__flowtests__/__generated__/ReactRelayFragmentContainerFlowtest_viewer2.graphql.js.flow +2 -2
- package/__flowtests__/__generated__/ReactRelayPaginationContainerFlowtestQuery.graphql.js.flow +3 -3
- package/__flowtests__/__generated__/ReactRelayPaginationContainerFlowtest_viewer.graphql.js.flow +3 -3
- package/__flowtests__/__generated__/ReactRelayRefetchContainerFlowtestQuery.graphql.js.flow +3 -3
- package/__flowtests__/__generated__/ReactRelayRefetchContainerFlowtest_viewer.graphql.js.flow +3 -3
- package/__flowtests__/__generated__/RelayModernFlowtest_badref.graphql.js.flow +2 -2
- package/__flowtests__/__generated__/RelayModernFlowtest_notref.graphql.js.flow +2 -2
- package/__flowtests__/__generated__/RelayModernFlowtest_user.graphql.js.flow +2 -2
- package/__flowtests__/__generated__/RelayModernFlowtest_users.graphql.js.flow +2 -2
- package/assertFragmentMap.js.flow +0 -2
- package/buildReactRelayContainer.js.flow +2 -4
- package/getRootVariablesForFragments.js.flow +0 -2
- package/hooks.js +1 -1
- package/hooks.js.flow +0 -2
- package/index.js +1 -1
- package/index.js.flow +2 -2
- package/isRelayEnvironment.js.flow +0 -2
- package/jest-react/internalAct.js.flow +25 -9
- package/legacy.js +1 -1
- package/legacy.js.flow +0 -2
- package/lib/ReactRelayContainerUtils.js +0 -1
- package/lib/ReactRelayContext.js +0 -1
- package/lib/ReactRelayFragmentContainer.js +10 -9
- package/lib/ReactRelayFragmentMockRenderer.js +0 -1
- package/lib/ReactRelayLocalQueryRenderer.js +0 -1
- package/lib/ReactRelayPaginationContainer.js +14 -11
- package/lib/ReactRelayQueryFetcher.js +2 -2
- package/lib/ReactRelayQueryRenderer.js +2 -4
- package/lib/ReactRelayQueryRendererContext.js +0 -1
- package/lib/ReactRelayRefetchContainer.js +11 -14
- package/lib/ReactRelayTestMocker.js +1 -2
- package/lib/ReactRelayTypes.js +0 -1
- package/lib/RelayContext.js +0 -1
- package/lib/assertFragmentMap.js +0 -1
- package/lib/buildReactRelayContainer.js +1 -2
- package/lib/getRootVariablesForFragments.js +1 -2
- package/lib/hooks.js +0 -1
- package/lib/index.js +3 -1
- package/lib/isRelayEnvironment.js +0 -1
- package/lib/jest-react/internalAct.js +24 -4
- package/lib/legacy.js +0 -1
- package/lib/multi-actor/useRelayActorEnvironment.js +0 -1
- package/lib/readContext.js +2 -2
- package/lib/relay-hooks/EntryPointContainer.react.js +0 -1
- package/lib/relay-hooks/EntryPointTypes.flow.js +0 -1
- package/lib/relay-hooks/FragmentResource.js +68 -29
- package/lib/relay-hooks/HooksImplementation.js +29 -0
- package/lib/relay-hooks/InternalLogger.js +0 -1
- package/lib/relay-hooks/LRUCache.js +0 -1
- package/lib/relay-hooks/LazyLoadEntryPointContainer_DEPRECATED.react.js +0 -1
- package/lib/relay-hooks/MatchContainer.js +2 -2
- package/lib/relay-hooks/ProfilerContext.js +0 -1
- package/lib/relay-hooks/QueryResource.js +5 -168
- package/lib/relay-hooks/RelayEnvironmentProvider.js +0 -1
- package/lib/relay-hooks/SuspenseResource.js +1 -2
- package/lib/relay-hooks/loadQuery.js +1 -1
- package/lib/relay-hooks/preloadQuery_DEPRECATED.js +8 -13
- package/lib/relay-hooks/prepareEntryPoint_DEPRECATED.js +0 -1
- package/lib/relay-hooks/react-cache/RelayReactCache.js +36 -0
- package/lib/relay-hooks/react-cache/getQueryResultOrFetchQuery_REACT_CACHE.js +344 -0
- package/lib/relay-hooks/react-cache/readFragmentInternal_REACT_CACHE.js +239 -0
- package/lib/relay-hooks/react-cache/useFragmentInternal_REACT_CACHE.js +598 -0
- package/lib/relay-hooks/react-cache/useFragment_REACT_CACHE.js +50 -0
- package/lib/relay-hooks/react-cache/useLazyLoadQuery_REACT_CACHE.js +55 -0
- package/lib/relay-hooks/react-cache/usePaginationFragment_REACT_CACHE.js +150 -0
- package/lib/relay-hooks/react-cache/usePreloadedQuery_REACT_CACHE.js +124 -0
- package/lib/relay-hooks/react-cache/useRefetchableFragmentInternal_REACT_CACHE.js +367 -0
- package/lib/relay-hooks/react-cache/useRefetchableFragment_REACT_CACHE.js +45 -0
- package/lib/relay-hooks/useBlockingPaginationFragment.js +4 -3
- package/lib/relay-hooks/useClientQuery.js +33 -0
- package/lib/relay-hooks/useEntryPointLoader.js +1 -2
- package/lib/relay-hooks/useFetchTrackingRef.js +0 -1
- package/lib/relay-hooks/useFragment.js +15 -2
- package/lib/relay-hooks/useFragmentNode.js +0 -1
- package/lib/relay-hooks/useIsMountedRef.js +0 -1
- package/lib/relay-hooks/useLazyLoadQuery.js +4 -2
- package/lib/relay-hooks/useLazyLoadQueryNode.js +0 -1
- package/lib/relay-hooks/useLoadMoreFunction.js +1 -2
- package/lib/relay-hooks/useMemoOperationDescriptor.js +0 -1
- package/lib/relay-hooks/useMemoVariables.js +0 -1
- package/lib/relay-hooks/useMutation.js +5 -7
- package/lib/relay-hooks/usePaginationFragment.js +15 -3
- package/lib/relay-hooks/usePreloadedQuery.js +4 -2
- package/lib/relay-hooks/useQueryLoader.js +1 -2
- package/lib/relay-hooks/useRefetchableFragment.js +14 -2
- package/lib/relay-hooks/useRefetchableFragmentNode.js +1 -2
- package/lib/relay-hooks/useRelayEnvironment.js +0 -1
- package/lib/relay-hooks/useStaticFragmentNodeWarning.js +0 -1
- package/lib/relay-hooks/useSubscribeToInvalidationState.js +0 -1
- package/lib/relay-hooks/useSubscription.js +0 -1
- package/multi-actor/useRelayActorEnvironment.js.flow +0 -2
- package/package.json +3 -3
- package/react-relay-hooks.js +2 -2
- package/react-relay-hooks.min.js +2 -2
- package/react-relay-legacy.js +2 -2
- package/react-relay-legacy.min.js +2 -2
- package/react-relay.js +2 -2
- package/react-relay.min.js +2 -2
- package/readContext.js.flow +1 -2
- package/relay-hooks/EntryPointContainer.react.js.flow +2 -4
- package/relay-hooks/EntryPointTypes.flow.js.flow +30 -32
- package/relay-hooks/FragmentResource.js.flow +80 -37
- package/relay-hooks/HooksImplementation.js.flow +43 -0
- package/relay-hooks/InternalLogger.js.flow +0 -2
- package/relay-hooks/LRUCache.js.flow +0 -2
- package/relay-hooks/LazyLoadEntryPointContainer_DEPRECATED.react.js.flow +4 -6
- package/relay-hooks/MatchContainer.js.flow +11 -6
- package/relay-hooks/ProfilerContext.js.flow +0 -2
- package/relay-hooks/QueryResource.js.flow +12 -209
- package/relay-hooks/RelayEnvironmentProvider.js.flow +2 -4
- package/relay-hooks/SuspenseResource.js.flow +0 -2
- package/relay-hooks/__flowtests__/EntryPointTypes/EntryPointElementConfig-flowtest.js.flow +3 -3
- package/relay-hooks/__flowtests__/EntryPointTypes/NestedEntrypoints-flowtest.js.flow +2 -2
- package/relay-hooks/__flowtests__/__generated__/useFragmentFlowtest_user.graphql.js.flow +2 -2
- package/relay-hooks/__flowtests__/__generated__/useFragmentFlowtest_users.graphql.js.flow +2 -2
- package/relay-hooks/__flowtests__/useBlockingPaginationFragment-flowtest.js.flow +4 -6
- package/relay-hooks/__flowtests__/useFragment-flowtest.js.flow +0 -2
- package/relay-hooks/__flowtests__/usePaginationFragment-flowtest.js.flow +4 -6
- package/relay-hooks/__flowtests__/useRefetchableFragment-flowtest.js.flow +0 -2
- package/relay-hooks/__flowtests__/utils.js.flow +8 -10
- package/relay-hooks/loadQuery.js.flow +2 -1
- package/relay-hooks/preloadQuery_DEPRECATED.js.flow +11 -20
- package/relay-hooks/prepareEntryPoint_DEPRECATED.js.flow +0 -2
- package/relay-hooks/react-cache/RelayReactCache.js.flow +40 -0
- package/relay-hooks/react-cache/getQueryResultOrFetchQuery_REACT_CACHE.js.flow +430 -0
- package/relay-hooks/react-cache/readFragmentInternal_REACT_CACHE.js.flow +297 -0
- package/relay-hooks/react-cache/useFragmentInternal_REACT_CACHE.js.flow +599 -0
- package/relay-hooks/react-cache/useFragment_REACT_CACHE.js.flow +72 -0
- package/relay-hooks/react-cache/useLazyLoadQuery_REACT_CACHE.js.flow +70 -0
- package/relay-hooks/react-cache/usePaginationFragment_REACT_CACHE.js.flow +171 -0
- package/relay-hooks/react-cache/usePreloadedQuery_REACT_CACHE.js.flow +151 -0
- package/relay-hooks/react-cache/useRefetchableFragmentInternal_REACT_CACHE.js.flow +595 -0
- package/relay-hooks/react-cache/useRefetchableFragment_REACT_CACHE.js.flow +65 -0
- package/relay-hooks/useBlockingPaginationFragment.js.flow +4 -6
- package/relay-hooks/useClientQuery.js.flow +39 -0
- package/relay-hooks/useEntryPointLoader.js.flow +6 -8
- package/relay-hooks/useFetchTrackingRef.js.flow +2 -4
- package/relay-hooks/useFragment.js.flow +17 -12
- package/relay-hooks/useFragmentNode.js.flow +2 -4
- package/relay-hooks/useIsMountedRef.js.flow +1 -3
- package/relay-hooks/useLazyLoadQuery.js.flow +17 -5
- package/relay-hooks/useLazyLoadQueryNode.js.flow +2 -4
- package/relay-hooks/useLoadMoreFunction.js.flow +6 -8
- package/relay-hooks/useMemoOperationDescriptor.js.flow +0 -2
- package/relay-hooks/useMemoVariables.js.flow +0 -2
- package/relay-hooks/useMutation.js.flow +5 -7
- package/relay-hooks/usePaginationFragment.js.flow +44 -19
- package/relay-hooks/usePreloadedQuery.js.flow +14 -5
- package/relay-hooks/useQueryLoader.js.flow +4 -6
- package/relay-hooks/useRefetchableFragment.js.flow +32 -3
- package/relay-hooks/useRefetchableFragmentNode.js.flow +38 -25
- package/relay-hooks/useRelayEnvironment.js.flow +0 -2
- package/relay-hooks/useStaticFragmentNodeWarning.js.flow +0 -2
- package/relay-hooks/useSubscribeToInvalidationState.js.flow +0 -2
- package/relay-hooks/useSubscription.js.flow +14 -10
@@ -0,0 +1,595 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*
|
7
|
+
* @emails oncall+relay
|
8
|
+
* @flow strict-local
|
9
|
+
* @format
|
10
|
+
*/
|
11
|
+
|
12
|
+
'use strict';
|
13
|
+
|
14
|
+
import type {LoaderFn} from '../useQueryLoader';
|
15
|
+
import type {
|
16
|
+
ConcreteRequest,
|
17
|
+
Disposable,
|
18
|
+
FetchPolicy,
|
19
|
+
IEnvironment,
|
20
|
+
OperationDescriptor,
|
21
|
+
OperationType,
|
22
|
+
ReaderFragment,
|
23
|
+
RenderPolicy,
|
24
|
+
Variables,
|
25
|
+
VariablesOf,
|
26
|
+
} from 'relay-runtime';
|
27
|
+
|
28
|
+
const ProfilerContext = require('../ProfilerContext');
|
29
|
+
const {getQueryResourceForEnvironment} = require('../QueryResource');
|
30
|
+
const useIsMountedRef = require('../useIsMountedRef');
|
31
|
+
const useQueryLoader = require('../useQueryLoader');
|
32
|
+
const useRelayEnvironment = require('../useRelayEnvironment');
|
33
|
+
const readFragmentInternal = require('./readFragmentInternal_REACT_CACHE');
|
34
|
+
const useFragmentInternal = require('./useFragmentInternal_REACT_CACHE');
|
35
|
+
const invariant = require('invariant');
|
36
|
+
const {useCallback, useContext, useReducer} = require('react');
|
37
|
+
const {
|
38
|
+
__internal: {fetchQuery},
|
39
|
+
createOperationDescriptor,
|
40
|
+
getFragmentIdentifier,
|
41
|
+
getRefetchMetadata,
|
42
|
+
getSelector,
|
43
|
+
getValueAtPath,
|
44
|
+
} = require('relay-runtime');
|
45
|
+
const warning = require('warning');
|
46
|
+
|
47
|
+
export type RefetchFn<
|
48
|
+
TQuery: OperationType,
|
49
|
+
TOptions = Options,
|
50
|
+
> = RefetchFnExact<TQuery, TOptions>;
|
51
|
+
|
52
|
+
// NOTE: RefetchFnDynamic returns a refetch function that:
|
53
|
+
// - Expects the /exact/ set of query variables if the provided key type is
|
54
|
+
// /nullable/.
|
55
|
+
// - Or, expects /a subset/ of the query variables if the provided key type is
|
56
|
+
// /non-null/.
|
57
|
+
// prettier-ignore
|
58
|
+
export type RefetchFnDynamic<
|
59
|
+
TQuery: OperationType,
|
60
|
+
TKey: ?{ +$data?: mixed, ... },
|
61
|
+
TOptions = Options,
|
62
|
+
> = $Call<
|
63
|
+
& (( { +$data?: mixed, ... }) => RefetchFnInexact<TQuery, TOptions>)
|
64
|
+
& ((?{ +$data?: mixed, ... }) => RefetchFnExact<TQuery, TOptions>),
|
65
|
+
TKey
|
66
|
+
>;
|
67
|
+
|
68
|
+
export type ReturnType<
|
69
|
+
TQuery: OperationType,
|
70
|
+
TKey: ?{+$data?: mixed, ...},
|
71
|
+
TOptions = Options,
|
72
|
+
> = {
|
73
|
+
fragmentData: mixed,
|
74
|
+
fragmentRef: mixed,
|
75
|
+
refetch: RefetchFnDynamic<TQuery, TKey, TOptions>,
|
76
|
+
};
|
77
|
+
|
78
|
+
export type Options = {
|
79
|
+
fetchPolicy?: FetchPolicy,
|
80
|
+
onComplete?: (Error | null) => void,
|
81
|
+
UNSTABLE_renderPolicy?: RenderPolicy,
|
82
|
+
};
|
83
|
+
|
84
|
+
type InternalOptions = {
|
85
|
+
...Options,
|
86
|
+
__environment?: IEnvironment,
|
87
|
+
};
|
88
|
+
|
89
|
+
type RefetchFnBase<TVars, TOptions> = (
|
90
|
+
vars: TVars,
|
91
|
+
options?: TOptions,
|
92
|
+
) => Disposable;
|
93
|
+
|
94
|
+
type RefetchFnExact<TQuery: OperationType, TOptions = Options> = RefetchFnBase<
|
95
|
+
VariablesOf<TQuery>,
|
96
|
+
TOptions,
|
97
|
+
>;
|
98
|
+
type RefetchFnInexact<
|
99
|
+
TQuery: OperationType,
|
100
|
+
TOptions = Options,
|
101
|
+
> = RefetchFnBase<$Shape<VariablesOf<TQuery>>, TOptions>;
|
102
|
+
|
103
|
+
type Action =
|
104
|
+
| {
|
105
|
+
type: 'reset',
|
106
|
+
environment: IEnvironment,
|
107
|
+
fragmentIdentifier: string,
|
108
|
+
}
|
109
|
+
| {
|
110
|
+
type: 'refetch',
|
111
|
+
refetchQuery: OperationDescriptor,
|
112
|
+
fetchPolicy?: FetchPolicy,
|
113
|
+
renderPolicy?: RenderPolicy,
|
114
|
+
onComplete?: (Error | null) => void,
|
115
|
+
refetchEnvironment: ?IEnvironment,
|
116
|
+
};
|
117
|
+
|
118
|
+
type RefetchState = {
|
119
|
+
fetchPolicy: FetchPolicy | void,
|
120
|
+
mirroredEnvironment: IEnvironment,
|
121
|
+
mirroredFragmentIdentifier: string,
|
122
|
+
onComplete: ((Error | null) => void) | void,
|
123
|
+
refetchEnvironment?: ?IEnvironment,
|
124
|
+
refetchQuery: OperationDescriptor | null,
|
125
|
+
renderPolicy: RenderPolicy | void,
|
126
|
+
};
|
127
|
+
|
128
|
+
type DebugIDandTypename = {
|
129
|
+
id: string,
|
130
|
+
typename: string,
|
131
|
+
...
|
132
|
+
};
|
133
|
+
|
134
|
+
function reducer(state: RefetchState, action: Action): RefetchState {
|
135
|
+
switch (action.type) {
|
136
|
+
case 'refetch': {
|
137
|
+
return {
|
138
|
+
...state,
|
139
|
+
fetchPolicy: action.fetchPolicy,
|
140
|
+
mirroredEnvironment:
|
141
|
+
action.refetchEnvironment ?? state.mirroredEnvironment,
|
142
|
+
onComplete: action.onComplete,
|
143
|
+
refetchEnvironment: action.refetchEnvironment,
|
144
|
+
refetchQuery: action.refetchQuery,
|
145
|
+
renderPolicy: action.renderPolicy,
|
146
|
+
};
|
147
|
+
}
|
148
|
+
case 'reset': {
|
149
|
+
return {
|
150
|
+
fetchPolicy: undefined,
|
151
|
+
mirroredEnvironment: action.environment,
|
152
|
+
mirroredFragmentIdentifier: action.fragmentIdentifier,
|
153
|
+
onComplete: undefined,
|
154
|
+
refetchQuery: null,
|
155
|
+
renderPolicy: undefined,
|
156
|
+
};
|
157
|
+
}
|
158
|
+
default: {
|
159
|
+
(action.type: empty);
|
160
|
+
throw new Error('useRefetchableFragmentNode: Unexpected action type');
|
161
|
+
}
|
162
|
+
}
|
163
|
+
}
|
164
|
+
|
165
|
+
function useRefetchableFragmentNode<
|
166
|
+
TQuery: OperationType,
|
167
|
+
TKey: ?{+$data?: mixed, ...},
|
168
|
+
>(
|
169
|
+
fragmentNode: ReaderFragment,
|
170
|
+
parentFragmentRef: mixed,
|
171
|
+
componentDisplayName: string,
|
172
|
+
): ReturnType<TQuery, TKey, InternalOptions> {
|
173
|
+
const parentEnvironment = useRelayEnvironment();
|
174
|
+
const {refetchableRequest, fragmentRefPathInResponse, identifierField} =
|
175
|
+
getRefetchMetadata(fragmentNode, componentDisplayName);
|
176
|
+
const fragmentIdentifier = getFragmentIdentifier(
|
177
|
+
fragmentNode,
|
178
|
+
parentFragmentRef,
|
179
|
+
);
|
180
|
+
|
181
|
+
const [refetchState, dispatch] = useReducer(reducer, {
|
182
|
+
fetchPolicy: undefined,
|
183
|
+
mirroredEnvironment: parentEnvironment,
|
184
|
+
mirroredFragmentIdentifier: fragmentIdentifier,
|
185
|
+
onComplete: undefined,
|
186
|
+
refetchEnvironment: null,
|
187
|
+
refetchQuery: null,
|
188
|
+
renderPolicy: undefined,
|
189
|
+
});
|
190
|
+
const {
|
191
|
+
fetchPolicy,
|
192
|
+
mirroredEnvironment,
|
193
|
+
mirroredFragmentIdentifier,
|
194
|
+
onComplete,
|
195
|
+
refetchEnvironment,
|
196
|
+
refetchQuery,
|
197
|
+
renderPolicy,
|
198
|
+
} = refetchState;
|
199
|
+
const environment = refetchEnvironment ?? parentEnvironment;
|
200
|
+
|
201
|
+
const QueryResource = getQueryResourceForEnvironment(environment);
|
202
|
+
const profilerContext = useContext(ProfilerContext);
|
203
|
+
|
204
|
+
const shouldReset =
|
205
|
+
environment !== mirroredEnvironment ||
|
206
|
+
fragmentIdentifier !== mirroredFragmentIdentifier;
|
207
|
+
const [queryRef, loadQuery, disposeQuery] =
|
208
|
+
useQueryLoader<TQuery>(refetchableRequest);
|
209
|
+
|
210
|
+
let fragmentRef = parentFragmentRef;
|
211
|
+
if (shouldReset) {
|
212
|
+
dispatch({
|
213
|
+
type: 'reset',
|
214
|
+
environment,
|
215
|
+
fragmentIdentifier,
|
216
|
+
});
|
217
|
+
disposeQuery();
|
218
|
+
} else if (refetchQuery != null && queryRef != null) {
|
219
|
+
// If refetch was called, we expect to have a refetchQuery and queryRef
|
220
|
+
// in state, since both state updates to set the refetchQuery and the
|
221
|
+
// queryRef occur simultaneously.
|
222
|
+
// In this case, we need to read the refetched query data (potentially
|
223
|
+
// suspending if it's in flight), and extract the new fragment ref
|
224
|
+
// from the query in order read the current @refetchable fragment
|
225
|
+
// with the updated fragment owner as the new refetchQuery.
|
226
|
+
|
227
|
+
// Before observing the refetch, record the current ID and typename
|
228
|
+
// so that, if we are refetching existing data on
|
229
|
+
// a field that implements Node, after refetching we
|
230
|
+
// can validate that the received data is consistent
|
231
|
+
let debugPreviousIDAndTypename: ?DebugIDandTypename;
|
232
|
+
if (__DEV__) {
|
233
|
+
debugPreviousIDAndTypename = debugFunctions.getInitialIDAndType(
|
234
|
+
refetchQuery.request.variables,
|
235
|
+
fragmentRefPathInResponse,
|
236
|
+
environment,
|
237
|
+
);
|
238
|
+
}
|
239
|
+
|
240
|
+
const handleQueryCompleted = (maybeError: void | Error) => {
|
241
|
+
onComplete && onComplete(maybeError ?? null);
|
242
|
+
};
|
243
|
+
|
244
|
+
// The queryRef.source obtained from useQueryLoader will be
|
245
|
+
// an observable we can consume /if/ a network request was
|
246
|
+
// started. Otherwise, given that QueryResource.prepare
|
247
|
+
// always expects an observable we fall back to a new network
|
248
|
+
// observable. Note however that if loadQuery did not make a network
|
249
|
+
// request, we don't expect to make one here, unless the state of
|
250
|
+
// the cache has changed between the call to refetch and this
|
251
|
+
// render.
|
252
|
+
const fetchObservable =
|
253
|
+
queryRef.source != null
|
254
|
+
? queryRef.source
|
255
|
+
: fetchQuery(environment, refetchQuery);
|
256
|
+
|
257
|
+
// Now wwe can we read the refetch query here using the
|
258
|
+
// queryRef provided from useQueryLoader. Note that the
|
259
|
+
// network request is started during the call to refetch,
|
260
|
+
// but if the refetch query is still in flight, we will suspend
|
261
|
+
// at this point:
|
262
|
+
const queryResult = profilerContext.wrapPrepareQueryResource(() => {
|
263
|
+
return QueryResource.prepare(
|
264
|
+
refetchQuery,
|
265
|
+
fetchObservable,
|
266
|
+
fetchPolicy,
|
267
|
+
renderPolicy,
|
268
|
+
{
|
269
|
+
error: handleQueryCompleted,
|
270
|
+
complete: () => {
|
271
|
+
// Validate that the type of the object we got back matches the type
|
272
|
+
// of the object already in the store
|
273
|
+
if (__DEV__) {
|
274
|
+
debugFunctions.checkSameTypeAfterRefetch(
|
275
|
+
debugPreviousIDAndTypename,
|
276
|
+
environment,
|
277
|
+
fragmentNode,
|
278
|
+
componentDisplayName,
|
279
|
+
);
|
280
|
+
}
|
281
|
+
handleQueryCompleted();
|
282
|
+
},
|
283
|
+
},
|
284
|
+
queryRef.fetchKey,
|
285
|
+
profilerContext,
|
286
|
+
);
|
287
|
+
});
|
288
|
+
|
289
|
+
const queryData = readFragmentInternal(
|
290
|
+
environment,
|
291
|
+
queryResult.fragmentNode,
|
292
|
+
queryResult.fragmentRef,
|
293
|
+
componentDisplayName,
|
294
|
+
).data;
|
295
|
+
invariant(
|
296
|
+
queryData != null,
|
297
|
+
'Relay: Expected to be able to read refetch query response. ' +
|
298
|
+
"If you're seeing this, this is likely a bug in Relay.",
|
299
|
+
);
|
300
|
+
|
301
|
+
// After reading/fetching the refetch query, we extract from the
|
302
|
+
// refetch query response the new fragment ref we need to use to read
|
303
|
+
// the fragment. The new fragment ref will point to the refetch query
|
304
|
+
// as its fragment owner.
|
305
|
+
const refetchedFragmentRef = getValueAtPath(
|
306
|
+
queryData,
|
307
|
+
fragmentRefPathInResponse,
|
308
|
+
);
|
309
|
+
fragmentRef = refetchedFragmentRef;
|
310
|
+
|
311
|
+
if (__DEV__) {
|
312
|
+
// Validate that the id of the object we got back matches the id
|
313
|
+
// we queried for in the variables.
|
314
|
+
// We do this during render instead of onComplete to make sure we are
|
315
|
+
// only validating the most recent refetch.
|
316
|
+
debugFunctions.checkSameIDAfterRefetch(
|
317
|
+
debugPreviousIDAndTypename,
|
318
|
+
fragmentRef,
|
319
|
+
fragmentNode,
|
320
|
+
componentDisplayName,
|
321
|
+
);
|
322
|
+
}
|
323
|
+
}
|
324
|
+
|
325
|
+
// We read and subscribe to the fragment using useFragmentNode.
|
326
|
+
// If refetch was called, we read the fragment using the new computed
|
327
|
+
// fragment ref from the refetch query response; otherwise, we use the
|
328
|
+
// fragment ref passed by the caller as normal.
|
329
|
+
const fragmentData = useFragmentInternal(
|
330
|
+
fragmentNode,
|
331
|
+
fragmentRef,
|
332
|
+
componentDisplayName,
|
333
|
+
);
|
334
|
+
|
335
|
+
const refetch = useRefetchFunction<TQuery>(
|
336
|
+
componentDisplayName,
|
337
|
+
dispatch,
|
338
|
+
disposeQuery,
|
339
|
+
fragmentData,
|
340
|
+
fragmentIdentifier,
|
341
|
+
fragmentNode,
|
342
|
+
fragmentRefPathInResponse,
|
343
|
+
identifierField,
|
344
|
+
loadQuery,
|
345
|
+
parentFragmentRef,
|
346
|
+
refetchableRequest,
|
347
|
+
);
|
348
|
+
return {
|
349
|
+
fragmentData,
|
350
|
+
fragmentRef,
|
351
|
+
refetch,
|
352
|
+
};
|
353
|
+
}
|
354
|
+
|
355
|
+
function useRefetchFunction<TQuery: OperationType>(
|
356
|
+
componentDisplayName: string,
|
357
|
+
dispatch: (
|
358
|
+
| {
|
359
|
+
environment: IEnvironment,
|
360
|
+
fragmentIdentifier: string,
|
361
|
+
type: 'reset',
|
362
|
+
}
|
363
|
+
| {
|
364
|
+
fetchPolicy?: FetchPolicy,
|
365
|
+
onComplete?: (Error | null) => void,
|
366
|
+
refetchEnvironment: ?IEnvironment,
|
367
|
+
refetchQuery: OperationDescriptor,
|
368
|
+
renderPolicy?: RenderPolicy,
|
369
|
+
type: 'refetch',
|
370
|
+
},
|
371
|
+
) => void,
|
372
|
+
disposeQuery: () => void,
|
373
|
+
fragmentData: mixed,
|
374
|
+
fragmentIdentifier: string,
|
375
|
+
fragmentNode: ReaderFragment,
|
376
|
+
fragmentRefPathInResponse: $ReadOnlyArray<string | number>,
|
377
|
+
identifierField: ?string,
|
378
|
+
loadQuery: LoaderFn<TQuery>,
|
379
|
+
parentFragmentRef: mixed,
|
380
|
+
refetchableRequest: ConcreteRequest,
|
381
|
+
): RefetchFn<TQuery, InternalOptions> {
|
382
|
+
const isMountedRef = useIsMountedRef();
|
383
|
+
const identifierValue =
|
384
|
+
identifierField != null &&
|
385
|
+
fragmentData != null &&
|
386
|
+
typeof fragmentData === 'object'
|
387
|
+
? fragmentData[identifierField]
|
388
|
+
: null;
|
389
|
+
return useCallback(
|
390
|
+
(providedRefetchVariables, options) => {
|
391
|
+
// Bail out and warn if we're trying to refetch after the component
|
392
|
+
// has unmounted
|
393
|
+
if (isMountedRef.current !== true) {
|
394
|
+
warning(
|
395
|
+
false,
|
396
|
+
'Relay: Unexpected call to `refetch` on unmounted component for fragment ' +
|
397
|
+
'`%s` in `%s`. It looks like some instances of your component are ' +
|
398
|
+
'still trying to fetch data but they already unmounted. ' +
|
399
|
+
'Please make sure you clear all timers, intervals, ' +
|
400
|
+
'async calls, etc that may trigger a fetch.',
|
401
|
+
fragmentNode.name,
|
402
|
+
componentDisplayName,
|
403
|
+
);
|
404
|
+
return {dispose: () => {}};
|
405
|
+
}
|
406
|
+
if (parentFragmentRef == null) {
|
407
|
+
warning(
|
408
|
+
false,
|
409
|
+
'Relay: Unexpected call to `refetch` while using a null fragment ref ' +
|
410
|
+
'for fragment `%s` in `%s`. When calling `refetch`, we expect ' +
|
411
|
+
"initial fragment data to be non-null. Please make sure you're " +
|
412
|
+
'passing a valid fragment ref to `%s` before calling ' +
|
413
|
+
'`refetch`, or make sure you pass all required variables to `refetch`.',
|
414
|
+
fragmentNode.name,
|
415
|
+
componentDisplayName,
|
416
|
+
componentDisplayName,
|
417
|
+
);
|
418
|
+
}
|
419
|
+
|
420
|
+
const refetchEnvironment = options?.__environment;
|
421
|
+
const fetchPolicy = options?.fetchPolicy;
|
422
|
+
const renderPolicy = options?.UNSTABLE_renderPolicy;
|
423
|
+
const onComplete = options?.onComplete;
|
424
|
+
const fragmentSelector = getSelector(fragmentNode, parentFragmentRef);
|
425
|
+
let parentVariables: Variables;
|
426
|
+
let fragmentVariables: Variables;
|
427
|
+
if (fragmentSelector == null) {
|
428
|
+
parentVariables = {};
|
429
|
+
fragmentVariables = {};
|
430
|
+
} else if (fragmentSelector.kind === 'PluralReaderSelector') {
|
431
|
+
parentVariables = fragmentSelector.selectors[0]?.owner.variables ?? {};
|
432
|
+
fragmentVariables = fragmentSelector.selectors[0]?.variables ?? {};
|
433
|
+
} else {
|
434
|
+
parentVariables = fragmentSelector.owner.variables;
|
435
|
+
fragmentVariables = fragmentSelector.variables;
|
436
|
+
}
|
437
|
+
|
438
|
+
// A user of `useRefetchableFragment()` may pass a subset of
|
439
|
+
// all variables required by the fragment when calling `refetch()`.
|
440
|
+
// We fill in any variables not passed by the call to `refetch()` with the
|
441
|
+
// variables from the original parent fragment owner.
|
442
|
+
const refetchVariables: VariablesOf<TQuery> = {
|
443
|
+
...(parentVariables: $FlowFixMe),
|
444
|
+
...fragmentVariables,
|
445
|
+
...providedRefetchVariables,
|
446
|
+
};
|
447
|
+
|
448
|
+
// If the query needs an identifier value ('id' or similar) and one
|
449
|
+
// was not explicitly provided, read it from the fragment data.
|
450
|
+
if (
|
451
|
+
identifierField != null &&
|
452
|
+
!providedRefetchVariables.hasOwnProperty('id')
|
453
|
+
) {
|
454
|
+
// @refetchable fragments are guaranteed to have an `id` selection
|
455
|
+
// if the type is Node, implements Node, or is @fetchable. Double-check
|
456
|
+
// that there actually is a value at runtime.
|
457
|
+
if (typeof identifierValue !== 'string') {
|
458
|
+
warning(
|
459
|
+
false,
|
460
|
+
'Relay: Expected result to have a string ' +
|
461
|
+
'`%s` in order to refetch, got `%s`.',
|
462
|
+
identifierField,
|
463
|
+
identifierValue,
|
464
|
+
);
|
465
|
+
}
|
466
|
+
(refetchVariables: $FlowFixMe).id = identifierValue;
|
467
|
+
}
|
468
|
+
|
469
|
+
const refetchQuery = createOperationDescriptor(
|
470
|
+
refetchableRequest,
|
471
|
+
refetchVariables,
|
472
|
+
{force: true},
|
473
|
+
);
|
474
|
+
|
475
|
+
// We call loadQuery which will start a network request if necessary
|
476
|
+
// and update the querRef from useQueryLoader.
|
477
|
+
// Note the following:
|
478
|
+
// - loadQuery will dispose of any previously refetched queries.
|
479
|
+
// - We use the variables extracted off the OperationDescriptor
|
480
|
+
// so that they have been filtered out to include only the
|
481
|
+
// variables actually declared in the query.
|
482
|
+
loadQuery(refetchQuery.request.variables, {
|
483
|
+
fetchPolicy,
|
484
|
+
__environment: refetchEnvironment,
|
485
|
+
__nameForWarning: 'refetch',
|
486
|
+
});
|
487
|
+
|
488
|
+
dispatch({
|
489
|
+
type: 'refetch',
|
490
|
+
fetchPolicy,
|
491
|
+
onComplete,
|
492
|
+
refetchEnvironment,
|
493
|
+
refetchQuery,
|
494
|
+
renderPolicy,
|
495
|
+
});
|
496
|
+
return {dispose: disposeQuery};
|
497
|
+
},
|
498
|
+
// NOTE: We disable react-hooks-deps warning because:
|
499
|
+
// - We know fragmentRefPathInResponse is static, so it can be omitted from
|
500
|
+
// deps
|
501
|
+
// - We know fragmentNode is static, so it can be omitted from deps.
|
502
|
+
// - fragmentNode and parentFragmentRef are also captured by including
|
503
|
+
// fragmentIdentifier
|
504
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
505
|
+
[fragmentIdentifier, dispatch, disposeQuery, identifierValue, loadQuery],
|
506
|
+
);
|
507
|
+
}
|
508
|
+
|
509
|
+
let debugFunctions;
|
510
|
+
if (__DEV__) {
|
511
|
+
debugFunctions = {
|
512
|
+
getInitialIDAndType(
|
513
|
+
memoRefetchVariables: ?Variables,
|
514
|
+
fragmentRefPathInResponse: $ReadOnlyArray<string | number>,
|
515
|
+
environment: IEnvironment,
|
516
|
+
): ?DebugIDandTypename {
|
517
|
+
const {Record} = require('relay-runtime');
|
518
|
+
const id = memoRefetchVariables?.id;
|
519
|
+
if (
|
520
|
+
fragmentRefPathInResponse.length !== 1 ||
|
521
|
+
fragmentRefPathInResponse[0] !== 'node' ||
|
522
|
+
id == null
|
523
|
+
) {
|
524
|
+
return null;
|
525
|
+
}
|
526
|
+
const recordSource = environment.getStore().getSource();
|
527
|
+
const record = recordSource.get(id);
|
528
|
+
const typename = record && Record.getType(record);
|
529
|
+
if (typename == null) {
|
530
|
+
return null;
|
531
|
+
}
|
532
|
+
return {
|
533
|
+
id,
|
534
|
+
typename,
|
535
|
+
};
|
536
|
+
},
|
537
|
+
|
538
|
+
checkSameTypeAfterRefetch(
|
539
|
+
previousIDAndType: ?DebugIDandTypename,
|
540
|
+
environment: IEnvironment,
|
541
|
+
fragmentNode: ReaderFragment,
|
542
|
+
componentDisplayName: string,
|
543
|
+
): void {
|
544
|
+
const {Record} = require('relay-runtime');
|
545
|
+
if (!previousIDAndType) {
|
546
|
+
return;
|
547
|
+
}
|
548
|
+
const recordSource = environment.getStore().getSource();
|
549
|
+
const record = recordSource.get(previousIDAndType.id);
|
550
|
+
const typename = record && Record.getType(record);
|
551
|
+
if (typename !== previousIDAndType.typename) {
|
552
|
+
warning(
|
553
|
+
false,
|
554
|
+
'Relay: Call to `refetch` returned data with a different ' +
|
555
|
+
'__typename: was `%s`, now `%s`, on `%s` in `%s`. ' +
|
556
|
+
'Please make sure the server correctly implements' +
|
557
|
+
'unique id requirement.',
|
558
|
+
previousIDAndType.typename,
|
559
|
+
typename,
|
560
|
+
fragmentNode.name,
|
561
|
+
componentDisplayName,
|
562
|
+
);
|
563
|
+
}
|
564
|
+
},
|
565
|
+
|
566
|
+
checkSameIDAfterRefetch(
|
567
|
+
previousIDAndTypename: ?DebugIDandTypename,
|
568
|
+
refetchedFragmentRef: mixed,
|
569
|
+
fragmentNode: ReaderFragment,
|
570
|
+
componentDisplayName: string,
|
571
|
+
): void {
|
572
|
+
if (previousIDAndTypename == null) {
|
573
|
+
return;
|
574
|
+
}
|
575
|
+
const {ID_KEY} = require('relay-runtime');
|
576
|
+
// $FlowExpectedError[incompatible-use]
|
577
|
+
const resultID = refetchedFragmentRef[ID_KEY];
|
578
|
+
if (resultID != null && resultID !== previousIDAndTypename.id) {
|
579
|
+
warning(
|
580
|
+
false,
|
581
|
+
'Relay: Call to `refetch` returned a different id, expected ' +
|
582
|
+
'`%s`, got `%s`, on `%s` in `%s`. ' +
|
583
|
+
'Please make sure the server correctly implements ' +
|
584
|
+
'unique id requirement.',
|
585
|
+
resultID,
|
586
|
+
previousIDAndTypename.id,
|
587
|
+
fragmentNode.name,
|
588
|
+
componentDisplayName,
|
589
|
+
);
|
590
|
+
}
|
591
|
+
},
|
592
|
+
};
|
593
|
+
}
|
594
|
+
|
595
|
+
module.exports = useRefetchableFragmentNode;
|
@@ -0,0 +1,65 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*
|
7
|
+
* @emails oncall+relay
|
8
|
+
* @flow strict-local
|
9
|
+
* @format
|
10
|
+
*/
|
11
|
+
|
12
|
+
'use strict';
|
13
|
+
|
14
|
+
import type {RefetchFnDynamic} from './useRefetchableFragmentInternal_REACT_CACHE';
|
15
|
+
import type {
|
16
|
+
FragmentType,
|
17
|
+
GraphQLTaggedNode,
|
18
|
+
OperationType,
|
19
|
+
} from 'relay-runtime';
|
20
|
+
|
21
|
+
const useRefetchableFragmentInternal = require('./useRefetchableFragmentInternal_REACT_CACHE');
|
22
|
+
const useStaticFragmentNodeWarning = require('../useStaticFragmentNodeWarning');
|
23
|
+
const {useDebugValue} = require('react');
|
24
|
+
const {getFragment} = require('relay-runtime');
|
25
|
+
|
26
|
+
type ReturnType<TQuery: OperationType, TKey: ?{+$data?: mixed, ...}> = [
|
27
|
+
// NOTE: This $Call ensures that the type of the returned data is either:
|
28
|
+
// - nullable if the provided ref type is nullable
|
29
|
+
// - non-nullable if the provided ref type is non-nullable
|
30
|
+
// prettier-ignore
|
31
|
+
$Call<
|
32
|
+
& (<TFragmentData>( { +$data?: TFragmentData, ... }) => TFragmentData)
|
33
|
+
& (<TFragmentData>(?{ +$data?: TFragmentData, ... }) => ?TFragmentData),
|
34
|
+
TKey,
|
35
|
+
>,
|
36
|
+
RefetchFnDynamic<TQuery, TKey>,
|
37
|
+
];
|
38
|
+
|
39
|
+
function useRefetchableFragment<
|
40
|
+
TQuery: OperationType,
|
41
|
+
TKey: ?{+$data?: mixed, +$fragmentSpreads: FragmentType, ...},
|
42
|
+
>(
|
43
|
+
fragmentInput: GraphQLTaggedNode,
|
44
|
+
fragmentRef: TKey,
|
45
|
+
): ReturnType<TQuery, TKey> {
|
46
|
+
const fragmentNode = getFragment(fragmentInput);
|
47
|
+
useStaticFragmentNodeWarning(
|
48
|
+
fragmentNode,
|
49
|
+
'first argument of useRefetchableFragment()',
|
50
|
+
);
|
51
|
+
const {fragmentData, refetch} = useRefetchableFragmentInternal<TQuery, TKey>(
|
52
|
+
fragmentNode,
|
53
|
+
fragmentRef,
|
54
|
+
'useRefetchableFragment()',
|
55
|
+
);
|
56
|
+
if (__DEV__) {
|
57
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
58
|
+
useDebugValue({fragment: fragmentNode.name, data: fragmentData});
|
59
|
+
}
|
60
|
+
/* $FlowExpectedError[prop-missing] : Exposed options is a subset of internal
|
61
|
+
* options */
|
62
|
+
return [fragmentData, (refetch: RefetchFnDynamic<TQuery, TKey>)];
|
63
|
+
}
|
64
|
+
|
65
|
+
module.exports = useRefetchableFragment;
|
@@ -9,8 +9,6 @@
|
|
9
9
|
* @format
|
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';
|
@@ -34,14 +32,14 @@ const {
|
|
34
32
|
getPaginationMetadata,
|
35
33
|
} = require('relay-runtime');
|
36
34
|
|
37
|
-
export type ReturnType<TQuery: OperationType, TKey, TFragmentData> = {
|
35
|
+
export type ReturnType<TQuery: OperationType, TKey, TFragmentData> = {
|
38
36
|
data: TFragmentData,
|
39
37
|
loadNext: LoadMoreFn<TQuery>,
|
40
38
|
loadPrevious: LoadMoreFn<TQuery>,
|
41
39
|
hasNext: boolean,
|
42
40
|
hasPrevious: boolean,
|
43
41
|
refetch: RefetchFnDynamic<TQuery, TKey>,
|
44
|
-
|
42
|
+
};
|
45
43
|
|
46
44
|
function useBlockingPaginationFragment<
|
47
45
|
TQuery: OperationType,
|
@@ -148,7 +146,7 @@ function useBlockingPaginationFragment<
|
|
148
146
|
};
|
149
147
|
}
|
150
148
|
|
151
|
-
function useLoadMore<TQuery: OperationType>(args: {
|
149
|
+
function useLoadMore<TQuery: OperationType>(args: {
|
152
150
|
disableStoreUpdates: () => void,
|
153
151
|
enableStoreUpdates: () => void,
|
154
152
|
...$Exact<
|
@@ -161,7 +159,7 @@ function useLoadMore<TQuery: OperationType>(args: {|
|
|
161
159
|
},
|
162
160
|
>,
|
163
161
|
>,
|
164
|
-
|
162
|
+
}): [LoadMoreFn<TQuery>, boolean, () => void] {
|
165
163
|
const {disableStoreUpdates, enableStoreUpdates, ...loadMoreArgs} = args;
|
166
164
|
const [requestPromise, setRequestPromise] = useState(null);
|
167
165
|
const requestPromiseRef = useRef(null);
|