/
ReactRelayQueryRenderer.js
315 lines (280 loc) · 8.57 KB
/
ReactRelayQueryRenderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
const React = require('React');
const ReactRelayQueryFetcher = require('./ReactRelayQueryFetcher');
const RelayPropTypes = require('../classic/container/RelayPropTypes');
const areEqual = require('areEqual');
const deepFreeze = require('deepFreeze');
const polyfill = require('react-lifecycles-compat');
import type {RelayEnvironmentInterface as ClassicEnvironment} from '../classic/store/RelayEnvironment';
import type {
CacheConfig,
GraphQLTaggedNode,
IEnvironment,
RelayContext,
Snapshot,
Variables,
} from 'RelayRuntime';
type RetryCallbacks = {
handleDataChange: ({
error?: Error,
snapshot?: Snapshot,
}) => void,
handleRetryAfterError: (error: Error) => void,
};
export type RenderProps = {
error: ?Error,
props: ?Object,
retry: ?() => void,
};
const NETWORK_ONLY = 'NETWORK_ONLY';
const STORE_THEN_NETWORK = 'STORE_THEN_NETWORK';
const DataFromEnum = {
NETWORK_ONLY,
STORE_THEN_NETWORK,
};
type DataFrom = $Keys<typeof DataFromEnum>;
export type Props = {
cacheConfig?: ?CacheConfig,
dataFrom?: DataFrom,
environment: IEnvironment | ClassicEnvironment,
query: ?GraphQLTaggedNode,
render: (renderProps: RenderProps) => React.Node,
variables: Variables,
};
type State = {
prevPropsEnvironment: IEnvironment | ClassicEnvironment,
prevPropsVariables: Variables,
prevQuery: ?GraphQLTaggedNode,
queryFetcher: ReactRelayQueryFetcher,
relayContextEnvironment: IEnvironment | ClassicEnvironment,
relayContextVariables: Variables,
renderProps: RenderProps,
retryCallbacks: RetryCallbacks,
};
/**
* @public
*
* Orchestrates fetching and rendering data for a single view or view hierarchy:
* - Fetches the query/variables using the given network implementation.
* - Normalizes the response(s) to that query, publishing them to the given
* store.
* - Renders the pending/fail/success states with the provided render function.
* - Subscribes for updates to the root data and re-renders with any changes.
*/
class ReactRelayQueryRenderer extends React.Component<Props, State> {
// TODO T25783053 Update this component to use the new React context API,
// Once we have confirmed that it's okay to raise min React version to 16.3.
static childContextTypes = {
relay: RelayPropTypes.Relay,
};
_relayContext: RelayContext = {
// $FlowFixMe TODO t16225453 QueryRenderer works with old+new environment.
environment: (this.props.environment: IEnvironment),
variables: this.props.variables,
};
constructor(props: Props, context: Object) {
super(props, context);
const handleDataChange = ({
error,
snapshot,
}: {
error?: Error,
snapshot?: Snapshot,
}): void => {
this.setState({
renderProps: getRenderProps(
error,
snapshot,
queryFetcher,
retryCallbacks,
),
});
};
const handleRetryAfterError = (error: Error) =>
this.setState({renderProps: getLoadingRenderProps()});
const retryCallbacks = {
handleDataChange,
handleRetryAfterError,
};
const queryFetcher = new ReactRelayQueryFetcher();
this.state = {
prevPropsEnvironment: props.environment,
prevPropsVariables: props.variables,
prevQuery: props.query,
queryFetcher,
retryCallbacks,
...fetchQueryAndComputeStateFromProps(
props,
queryFetcher,
retryCallbacks,
),
};
}
static getDerivedStateFromProps(
nextProps: Props,
prevState: State,
): $Shape<State> | null {
if (
prevState.prevQuery !== nextProps.query ||
prevState.prevPropsEnvironment !== nextProps.environment ||
!areEqual(prevState.prevPropsVariables, nextProps.variables)
) {
return {
prevQuery: nextProps.query,
prevPropsEnvironment: nextProps.environment,
prevPropsVariables: nextProps.variables,
...fetchQueryAndComputeStateFromProps(
nextProps,
prevState.queryFetcher,
prevState.retryCallbacks,
),
};
}
return null;
}
componentWillUnmount(): void {
this.state.queryFetcher.dispose();
}
shouldComponentUpdate(nextProps: Props, nextState: State): boolean {
return (
nextProps.render !== this.props.render ||
nextState.renderProps !== this.state.renderProps
);
}
getChildContext(): Object {
return {
relay: this._relayContext,
};
}
render() {
const {
relayContextEnvironment,
relayContextVariables,
renderProps,
} = this.state;
// HACK Mutate the context.relay object before updating children,
// To account for any changes made by static gDSFP.
// Updating this value in gDSFP would be less safe, since props changes
// could be interrupted and we might re-render based on a setState call.
// Child containers rely on context.relay being mutated (also for gDSFP).
// $FlowFixMe TODO t16225453 QueryRenderer works with old+new environment.
this._relayContext.environment = (relayContextEnvironment: IEnvironment);
this._relayContext.variables = relayContextVariables;
// Note that the root fragment results in `renderProps.props` is already
// frozen by the store; this call is to freeze the renderProps object and
// error property if set.
if (__DEV__) {
deepFreeze(renderProps);
}
return this.props.render(renderProps);
}
}
function getLoadingRenderProps(): RenderProps {
return {
error: null,
props: null, // `props: null` indicates that the data is being fetched (i.e. loading)
retry: null,
};
}
function getEmptyRenderProps(): RenderProps {
return {
error: null,
props: {}, // `props: {}` indicates no data available
retry: null,
};
}
function getRenderProps(
error: ?Error,
snapshot: ?Snapshot,
queryFetcher: ReactRelayQueryFetcher,
retryCallbacks: RetryCallbacks,
): RenderProps {
return {
error: error ? error : null,
props: snapshot ? snapshot.data : null,
retry: () => {
const syncSnapshot = queryFetcher.retry();
if (syncSnapshot) {
retryCallbacks.handleDataChange({snapshot: syncSnapshot});
} else if (error) {
// If retrying after an error and no synchronous result available,
// reset the render props
retryCallbacks.handleRetryAfterError(error);
}
},
};
}
function fetchQueryAndComputeStateFromProps(
props: Props,
queryFetcher: ReactRelayQueryFetcher,
retryCallbacks: RetryCallbacks,
): $Shape<State> {
const {environment, query, variables} = props;
if (query) {
// $FlowFixMe TODO t16225453 QueryRenderer works with old+new environment.
const genericEnvironment = (environment: IEnvironment);
const {
createOperationSelector,
getRequest,
} = genericEnvironment.unstable_internal;
const request = getRequest(query);
const operation = createOperationSelector(request, variables);
try {
const storeSnapshot =
props.dataFrom === STORE_THEN_NETWORK
? queryFetcher.lookupInStore(genericEnvironment, operation)
: null;
const querySnapshot = queryFetcher.fetch({
cacheConfig: props.cacheConfig,
dataFrom: props.dataFrom,
environment: genericEnvironment,
onDataChange: retryCallbacks.handleDataChange,
operation,
});
// Use network data first, since it may be fresher
const snapshot = querySnapshot || storeSnapshot;
if (!snapshot) {
return {
relayContextEnvironment: environment,
relayContextVariables: operation.variables,
renderProps: getLoadingRenderProps(),
};
}
return {
relayContextEnvironment: environment,
relayContextVariables: operation.variables,
renderProps: getRenderProps(
null,
snapshot,
queryFetcher,
retryCallbacks,
),
};
} catch (error) {
return {
relayContextEnvironment: environment,
relayContextVariables: operation.variables,
renderProps: getRenderProps(error, null, queryFetcher, retryCallbacks),
};
}
} else {
queryFetcher.dispose();
return {
relayContextEnvironment: environment,
relayContextVariables: variables,
renderProps: getEmptyRenderProps(),
};
}
}
// Make static getDerivedStateFromProps work with older React versions:
polyfill(ReactRelayQueryRenderer);
module.exports = ReactRelayQueryRenderer;