@@ -31,6 +31,7 @@ import type {
31
31
import type { OperationAvailability } from 'relay-runtime/store/RelayStoreTypes' ;
32
32
33
33
const invariant = require ( 'invariant' ) ;
34
+ const React = require ( 'react' ) ;
34
35
const {
35
36
__internal : { fetchQueryDeduped} ,
36
37
Observable,
@@ -40,9 +41,21 @@ const {
40
41
getRequest,
41
42
getRequestIdentifier,
42
43
} = require ( 'relay-runtime' ) ;
44
+ const warning = require ( 'warning' ) ;
43
45
46
+ let RenderDispatcher = null ;
44
47
let fetchKey = 100001 ;
45
48
49
+ hook useTrackLoadQueryInRender ( ) {
50
+ if ( RenderDispatcher === null ) {
51
+ // Flow does not know of React internals (rightly so), but we need to
52
+ // ensure here that this function isn't called inside render.
53
+ RenderDispatcher =
54
+ // $FlowFixMe[prop-missing]
55
+ React . __CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE ?. H ;
56
+ }
57
+ }
58
+
46
59
type QueryType < T > =
47
60
T extends Query < infer V , infer D , infer RR >
48
61
? {
@@ -75,6 +88,17 @@ function loadQuery<
75
88
options?: ?LoadQueryOptions,
76
89
environmentProviderOptions?: ?TEnvironmentProviderOptions,
77
90
): PreloadedQueryInner< TQuery , TEnvironmentProviderOptions > {
91
+ // This code ensures that we don't call loadQuery during render.
92
+ const CurrentDispatcher =
93
+ // $FlowFixMe[prop-missing]
94
+ React . __CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE ?. H ;
95
+
96
+ warning (
97
+ RenderDispatcher == null || CurrentDispatcher !== RenderDispatcher ,
98
+ 'Relay: `%s` should not be called inside a React render function.' ,
99
+ options ?. __nameForWarning ?? 'loadQuery' ,
100
+ ) ;
101
+
78
102
// Every time you call loadQuery, we will generate a new fetchKey.
79
103
// This will ensure that every query reference that is created and
80
104
// passed to usePreloadedQuery is independently evaluated,
@@ -392,4 +416,5 @@ function loadQuery<
392
416
393
417
module . exports = {
394
418
loadQuery ,
419
+ useTrackLoadQueryInRender ,
395
420
} ;
0 commit comments