react-relay 11.0.1 → 13.0.0-rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) 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 +6 -7
  6. package/ReactRelayPaginationContainer.js.flow +111 -58
  7. package/ReactRelayQueryFetcher.js.flow +9 -10
  8. package/ReactRelayQueryRenderer.js.flow +115 -81
  9. package/ReactRelayRefetchContainer.js.flow +41 -38
  10. package/ReactRelayTestMocker.js.flow +16 -14
  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 +11 -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 -4
  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 +96 -38
  46. package/lib/ReactRelayQueryFetcher.js +3 -3
  47. package/lib/ReactRelayQueryRenderer.js +86 -53
  48. package/lib/ReactRelayRefetchContainer.js +38 -25
  49. package/lib/ReactRelayTestMocker.js +8 -9
  50. package/lib/RelayContext.js +3 -2
  51. package/lib/assertFragmentMap.js +3 -2
  52. package/lib/buildReactRelayContainer.js +14 -11
  53. package/lib/getRootVariablesForFragments.js +1 -2
  54. package/lib/hooks.js +5 -5
  55. package/lib/index.js +7 -7
  56. package/lib/jest-react/enqueueTask.js +53 -0
  57. package/lib/jest-react/index.js +13 -0
  58. package/lib/jest-react/internalAct.js +116 -0
  59. package/lib/multi-actor/ActorChange.js +30 -0
  60. package/lib/multi-actor/index.js +11 -0
  61. package/lib/multi-actor/useRelayActorEnvironment.js +29 -0
  62. package/lib/relay-hooks/EntryPointContainer.react.js +3 -3
  63. package/lib/relay-hooks/FragmentResource.js +347 -92
  64. package/lib/relay-hooks/LRUCache.js +1 -1
  65. package/lib/relay-hooks/LazyLoadEntryPointContainer_DEPRECATED.react.js +4 -4
  66. package/lib/relay-hooks/MatchContainer.js +1 -1
  67. package/lib/relay-hooks/QueryResource.js +172 -29
  68. package/lib/relay-hooks/RelayEnvironmentProvider.js +5 -3
  69. package/lib/relay-hooks/SuspenseResource.js +130 -0
  70. package/lib/relay-hooks/loadQuery.js +42 -20
  71. package/lib/relay-hooks/preloadQuery_DEPRECATED.js +24 -15
  72. package/lib/relay-hooks/useBlockingPaginationFragment.js +4 -5
  73. package/lib/relay-hooks/useEntryPointLoader.js +2 -2
  74. package/lib/relay-hooks/useFetchTrackingRef.js +2 -1
  75. package/lib/relay-hooks/useFragment.js +8 -7
  76. package/lib/relay-hooks/useFragmentNode.js +4 -4
  77. package/lib/relay-hooks/useIsOperationNodeActive.js +3 -3
  78. package/lib/relay-hooks/useLazyLoadQuery.js +3 -3
  79. package/lib/relay-hooks/useLazyLoadQueryNode.js +10 -4
  80. package/lib/relay-hooks/useLoadMoreFunction.js +8 -12
  81. package/lib/relay-hooks/useMemoOperationDescriptor.js +2 -2
  82. package/lib/relay-hooks/useMemoVariables.js +2 -2
  83. package/lib/relay-hooks/useMutation.js +17 -6
  84. package/lib/relay-hooks/usePaginationFragment.js +2 -3
  85. package/lib/relay-hooks/usePreloadedQuery.js +8 -7
  86. package/lib/relay-hooks/useQueryLoader.js +30 -10
  87. package/lib/relay-hooks/useRefetchableFragmentNode.js +13 -17
  88. package/lib/relay-hooks/useRelayEnvironment.js +3 -3
  89. package/lib/relay-hooks/useStaticFragmentNodeWarning.js +2 -2
  90. package/lib/relay-hooks/useSubscribeToInvalidationState.js +2 -1
  91. package/lib/relay-hooks/useSubscription.js +10 -7
  92. package/multi-actor/ActorChange.js.flow +58 -0
  93. package/multi-actor/index.js.flow +14 -0
  94. package/multi-actor/useRelayActorEnvironment.js.flow +49 -0
  95. package/package.json +3 -2
  96. package/react-relay-hooks.js +2 -2
  97. package/react-relay-hooks.min.js +2 -2
  98. package/react-relay-legacy.js +2 -2
  99. package/react-relay-legacy.min.js +2 -2
  100. package/react-relay.js +2 -2
  101. package/react-relay.min.js +2 -2
  102. package/relay-hooks/EntryPointContainer.react.js.flow +8 -15
  103. package/relay-hooks/EntryPointTypes.flow.js.flow +24 -25
  104. package/relay-hooks/FragmentResource.js.flow +368 -94
  105. package/relay-hooks/LazyLoadEntryPointContainer_DEPRECATED.react.js.flow +32 -46
  106. package/relay-hooks/MatchContainer.js.flow +3 -2
  107. package/relay-hooks/QueryResource.js.flow +216 -25
  108. package/relay-hooks/RelayEnvironmentProvider.js.flow +14 -4
  109. package/relay-hooks/SuspenseResource.js.flow +115 -0
  110. package/relay-hooks/__flowtests__/EntryPointTypes/EntryPointElementConfig-flowtest.js.flow +4 -3
  111. package/relay-hooks/__flowtests__/EntryPointTypes/NestedEntrypoints-flowtest.js.flow +1 -1
  112. package/relay-hooks/__flowtests__/useBlockingPaginationFragment-flowtest.js.flow +10 -9
  113. package/relay-hooks/__flowtests__/useFragment-flowtest.js.flow +8 -7
  114. package/relay-hooks/__flowtests__/usePaginationFragment-flowtest.js.flow +10 -9
  115. package/relay-hooks/__flowtests__/useRefetchableFragment-flowtest.js.flow +10 -9
  116. package/relay-hooks/__flowtests__/utils.js.flow +8 -12
  117. package/relay-hooks/loadEntryPoint.js.flow +6 -12
  118. package/relay-hooks/loadQuery.js.flow +49 -31
  119. package/relay-hooks/preloadQuery_DEPRECATED.js.flow +30 -21
  120. package/relay-hooks/prepareEntryPoint_DEPRECATED.js.flow +6 -12
  121. package/relay-hooks/useBlockingPaginationFragment.js.flow +13 -11
  122. package/relay-hooks/useEntryPointLoader.js.flow +7 -10
  123. package/relay-hooks/useFetchTrackingRef.js.flow +2 -2
  124. package/relay-hooks/useFragment.js.flow +26 -46
  125. package/relay-hooks/useFragmentNode.js.flow +5 -7
  126. package/relay-hooks/useIsOperationNodeActive.js.flow +3 -5
  127. package/relay-hooks/useIsParentQueryActive.js.flow +3 -4
  128. package/relay-hooks/useLazyLoadQuery.js.flow +9 -10
  129. package/relay-hooks/useLazyLoadQueryNode.js.flow +19 -13
  130. package/relay-hooks/useLoadMoreFunction.js.flow +20 -29
  131. package/relay-hooks/useMemoOperationDescriptor.js.flow +5 -7
  132. package/relay-hooks/useMemoVariables.js.flow +6 -6
  133. package/relay-hooks/useMutation.js.flow +26 -26
  134. package/relay-hooks/usePaginationFragment.js.flow +38 -44
  135. package/relay-hooks/usePreloadedQuery.js.flow +18 -14
  136. package/relay-hooks/useQueryLoader.js.flow +41 -22
  137. package/relay-hooks/useRefetchableFragment.js.flow +7 -8
  138. package/relay-hooks/useRefetchableFragmentNode.js.flow +24 -32
  139. package/relay-hooks/useRelayEnvironment.js.flow +2 -4
  140. package/relay-hooks/useStaticFragmentNodeWarning.js.flow +2 -3
  141. package/relay-hooks/useSubscribeToInvalidationState.js.flow +3 -6
  142. package/relay-hooks/useSubscription.js.flow +20 -10
  143. package/lib/relay-hooks/getPaginationMetadata.js +0 -41
  144. package/lib/relay-hooks/getPaginationVariables.js +0 -67
  145. package/lib/relay-hooks/getRefetchMetadata.js +0 -36
  146. package/lib/relay-hooks/getValueAtPath.js +0 -51
  147. package/relay-hooks/getPaginationMetadata.js.flow +0 -74
  148. package/relay-hooks/getPaginationVariables.js.flow +0 -110
  149. package/relay-hooks/getRefetchMetadata.js.flow +0 -80
  150. package/relay-hooks/getValueAtPath.js.flow +0 -46
@@ -0,0 +1,115 @@
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
+ * @flow strict-local
8
+ * @emails oncall+relay
9
+ * @format
10
+ */
11
+
12
+ // flowlint ambiguous-object-type:error
13
+
14
+ 'use strict';
15
+
16
+ import type {Disposable, IEnvironment} from 'relay-runtime';
17
+
18
+ const invariant = require('invariant');
19
+
20
+ const TEMPORARY_RETAIN_DURATION_MS = 5 * 60 * 1000;
21
+
22
+ /**
23
+ * Allows you to retain a resource as part of a component lifecycle accounting
24
+ * for Suspense. You temporarily retain the resource during render, then
25
+ * permanently retain it during commit and release it during unmount.
26
+ */
27
+ class SuspenseResource {
28
+ _retainCount = 0;
29
+ _retainDisposable: ?Disposable = null;
30
+ _releaseTemporaryRetain: ?() => void = null;
31
+ _retain: IEnvironment => Disposable;
32
+
33
+ constructor(retain: (environment: IEnvironment) => Disposable) {
34
+ this._retain = (environment: IEnvironment): Disposable => {
35
+ this._retainCount++;
36
+ if (this._retainCount === 1) {
37
+ this._retainDisposable = retain(environment);
38
+ }
39
+ return {
40
+ dispose: () => {
41
+ this._retainCount = Math.max(0, this._retainCount - 1);
42
+ if (this._retainCount === 0) {
43
+ invariant(
44
+ this._retainDisposable != null,
45
+ 'Relay: Expected disposable to release query to be defined.' +
46
+ "If you're seeing this, this is likely a bug in Relay.",
47
+ );
48
+ this._retainDisposable.dispose();
49
+ this._retainDisposable = null;
50
+ }
51
+ },
52
+ };
53
+ };
54
+ }
55
+
56
+ temporaryRetain(environment: IEnvironment): Disposable {
57
+ // If we're executing in a server environment, there's no need
58
+ // to create temporary retains, since the component will never commit.
59
+ if (environment.isServer()) {
60
+ return {dispose: () => {}};
61
+ }
62
+
63
+ // temporaryRetain is called during the render phase. However,
64
+ // given that we can't tell if this render will eventually commit or not,
65
+ // we create a timer to autodispose of this retain in case the associated
66
+ // component never commits.
67
+ // If the component /does/ commit, permanentRetain will clear this timeout
68
+ // and permanently retain the data.
69
+ const retention = this._retain(environment);
70
+ let releaseQueryTimeout = null;
71
+ const releaseTemporaryRetain = () => {
72
+ clearTimeout(releaseQueryTimeout);
73
+ releaseQueryTimeout = null;
74
+ this._releaseTemporaryRetain = null;
75
+ retention.dispose();
76
+ };
77
+ releaseQueryTimeout = setTimeout(
78
+ releaseTemporaryRetain,
79
+ TEMPORARY_RETAIN_DURATION_MS,
80
+ );
81
+
82
+ // NOTE: Since temporaryRetain can be called multiple times, we release
83
+ // the previous temporary retain after we re-establish a new one, since
84
+ // we only ever need a single temporary retain until the permanent retain is
85
+ // established.
86
+ // temporaryRetain may be called multiple times by React during the render
87
+ // phase, as well as multiple times by other query components that are
88
+ // rendering the same query/variables.
89
+ this._releaseTemporaryRetain?.();
90
+ this._releaseTemporaryRetain = releaseTemporaryRetain;
91
+
92
+ return {
93
+ dispose: () => {
94
+ this._releaseTemporaryRetain?.();
95
+ },
96
+ };
97
+ }
98
+
99
+ permanentRetain(environment: IEnvironment): Disposable {
100
+ const disposable = this._retain(environment);
101
+ this.releaseTemporaryRetain();
102
+ return disposable;
103
+ }
104
+
105
+ releaseTemporaryRetain(): void {
106
+ this._releaseTemporaryRetain?.();
107
+ this._releaseTemporaryRetain = null;
108
+ }
109
+
110
+ getRetainCount(): number {
111
+ return this._retainCount;
112
+ }
113
+ }
114
+
115
+ module.exports = SuspenseResource;
@@ -13,8 +13,8 @@
13
13
 
14
14
  import type {
15
15
  EntryPoint,
16
- EntryPointProps,
17
16
  EntryPointElementConfig,
17
+ EntryPointProps,
18
18
  } from '../../EntryPointTypes.flow';
19
19
 
20
20
  type MyComponentOtherProps = $ReadOnly<{|
@@ -30,10 +30,11 @@ type PreloadParams = $ReadOnly<{||}>;
30
30
  type MyComponentEntryPointType = EntryPoint<PreloadParams, typeof MyComponent>;
31
31
 
32
32
  // This gets the "other props" of the component through the entrypoint's typing
33
- type MyComponentEntryPointProps = EntryPointElementConfig<MyComponentEntryPointType>;
33
+ type MyComponentEntryPointProps =
34
+ EntryPointElementConfig<MyComponentEntryPointType>;
34
35
 
35
36
  // This gets the "other props" directly from the component's prop typings
36
- type OtherProps = $PropertyType<MyComponentProps, 'props'>;
37
+ type OtherProps = MyComponentProps['props'];
37
38
 
38
39
  // We want to make sure that `OtherProps` and `MyComponentEntryPointProps` are exactly the same.
39
40
  opaque type __SUBTYPE_CHECK_1__: OtherProps = MyComponentEntryPointProps;
@@ -11,12 +11,12 @@
11
11
 
12
12
  'use strict';
13
13
 
14
+ import type {JSResourceReference} from 'JSResourceReference';
14
15
  import type {
15
16
  EntryPoint,
16
17
  EntryPointProps,
17
18
  PreloadedEntryPoint,
18
19
  } from '../../EntryPointTypes.flow';
19
- import type {JSResourceReference} from 'JSResourceReference';
20
20
 
21
21
  declare function mockJSResource<TModule>(
22
22
  module: TModule,
@@ -11,16 +11,7 @@
11
11
 
12
12
  // flowlint ambiguous-object-type:error
13
13
 
14
- import useBlockingPaginationFragment from '../useBlockingPaginationFragment';
15
14
  import type {LoadMoreFn} from '../useLoadMoreFunction';
16
- import {
17
- fragmentData,
18
- fragmentInput,
19
- keyAnotherNonNullable,
20
- keyAnotherNullable,
21
- keyNonNullable,
22
- keyNullable,
23
- } from './utils';
24
15
  import type {
25
16
  FetchFn,
26
17
  NonNullableData,
@@ -31,6 +22,16 @@ import type {
31
22
  } from './utils';
32
23
  import type {IEnvironment, OperationType} from 'relay-runtime';
33
24
 
25
+ import useBlockingPaginationFragment from '../useBlockingPaginationFragment';
26
+ import {
27
+ fragmentData,
28
+ fragmentInput,
29
+ keyAnotherNonNullable,
30
+ keyAnotherNullable,
31
+ keyNonNullable,
32
+ keyNullable,
33
+ } from './utils';
34
+
34
35
  type ExpectedReturnType<
35
36
  TQuery: OperationType,
36
37
  TQueryVariables,
@@ -11,8 +11,16 @@
11
11
 
12
12
  // flowlint ambiguous-object-type:error
13
13
 
14
+ import type {
15
+ NonNullableData,
16
+ NonNullablePluralData,
17
+ NullableData,
18
+ NullablePluralData,
19
+ } from './utils';
20
+
14
21
  import useFragment from '../useFragment';
15
22
  import {
23
+ fragmentData,
16
24
  fragmentInput,
17
25
  keyAnotherNonNullable,
18
26
  keyAnotherNullable,
@@ -20,13 +28,6 @@ import {
20
28
  keyNonNullablePlural,
21
29
  keyNullable,
22
30
  keyNullablePlural,
23
- fragmentData,
24
- } from './utils';
25
- import type {
26
- NonNullableData,
27
- NonNullablePluralData,
28
- NullableData,
29
- NullablePluralData,
30
31
  } from './utils';
31
32
 
32
33
  /* eslint-disable react-hooks/rules-of-hooks */
@@ -12,15 +12,6 @@
12
12
  // flowlint ambiguous-object-type:error
13
13
 
14
14
  import type {LoadMoreFn} from '../useLoadMoreFunction';
15
- import usePaginationFragment from '../usePaginationFragment';
16
- import {
17
- fragmentInput,
18
- fragmentData,
19
- keyAnotherNonNullable,
20
- keyAnotherNullable,
21
- keyNonNullable,
22
- keyNullable,
23
- } from './utils';
24
15
  import type {
25
16
  FetchFn,
26
17
  NonNullableData,
@@ -31,6 +22,16 @@ import type {
31
22
  } from './utils';
32
23
  import type {IEnvironment, OperationType} from 'relay-runtime';
33
24
 
25
+ import usePaginationFragment from '../usePaginationFragment';
26
+ import {
27
+ fragmentData,
28
+ fragmentInput,
29
+ keyAnotherNonNullable,
30
+ keyAnotherNullable,
31
+ keyNonNullable,
32
+ keyNullable,
33
+ } from './utils';
34
+
34
35
  type ExpectedReturnType<
35
36
  TQuery: OperationType,
36
37
  TQueryVariables,
@@ -11,15 +11,6 @@
11
11
 
12
12
  // flowlint ambiguous-object-type:error
13
13
 
14
- import useRefetchableFragment from '../useRefetchableFragment';
15
- import {
16
- fragmentInput,
17
- keyAnotherNonNullable,
18
- keyAnotherNullable,
19
- keyNonNullable,
20
- keyNullable,
21
- fragmentData,
22
- } from './utils';
23
14
  import type {
24
15
  FetchFn,
25
16
  NonNullableData,
@@ -30,6 +21,16 @@ import type {
30
21
  } from './utils';
31
22
  import type {IEnvironment} from 'relay-runtime';
32
23
 
24
+ import useRefetchableFragment from '../useRefetchableFragment';
25
+ import {
26
+ fragmentData,
27
+ fragmentInput,
28
+ keyAnotherNonNullable,
29
+ keyAnotherNullable,
30
+ keyNonNullable,
31
+ keyNullable,
32
+ } from './utils';
33
+
33
34
  /* eslint-disable react-hooks/rules-of-hooks */
34
35
 
35
36
  // Nullability of returned data type is correct
@@ -13,11 +13,7 @@
13
13
 
14
14
  'use strict';
15
15
 
16
- import type {
17
- Disposable,
18
- FragmentReference,
19
- GraphQLTaggedNode,
20
- } from 'relay-runtime';
16
+ import type {Disposable, FragmentType, GraphQLTaggedNode} from 'relay-runtime';
21
17
 
22
18
  declare export var fragmentInput: GraphQLTaggedNode;
23
19
 
@@ -38,42 +34,42 @@ export type AnotherNonNullableData = {|
38
34
 
39
35
  declare export var keyNonNullable: {
40
36
  +$data?: NonNullableData,
41
- +$fragmentRefs: FragmentReference,
37
+ +$fragmentSpreads: FragmentType,
42
38
  ...
43
39
  };
44
40
 
45
41
  declare export var keyNonNullablePlural: $ReadOnlyArray<{
46
42
  +$data?: NonNullablePluralData,
47
- +$fragmentRefs: FragmentReference,
43
+ +$fragmentSpreads: FragmentType,
48
44
  ...
49
45
  }>;
50
46
 
51
47
  declare export var keyNullablePlural: ?$ReadOnlyArray<{
52
48
  +$data?: NonNullablePluralData,
53
- +$fragmentRefs: FragmentReference,
49
+ +$fragmentSpreads: FragmentType,
54
50
  ...
55
51
  }>;
56
52
 
57
53
  declare export var keyNullable: ?{
58
54
  +$data?: NonNullableData,
59
- +$fragmentRefs: FragmentReference,
55
+ +$fragmentSpreads: FragmentType,
60
56
  ...
61
57
  };
62
58
 
63
59
  declare export var keyAnotherNonNullable: {
64
60
  +$data: AnotherNonNullableData,
65
- +$fragmentRefs: FragmentReference,
61
+ +$fragmentSpreads: FragmentType,
66
62
  ...
67
63
  };
68
64
 
69
65
  declare export var keyAnotherNullable: ?{
70
66
  +$data: AnotherNonNullableData,
71
- +$fragmentRefs: FragmentReference,
67
+ +$fragmentSpreads: FragmentType,
72
68
  ...
73
69
  };
74
70
 
75
71
  declare export var fragmentData: {
76
- +$refType: FragmentReference,
72
+ +$fragmentType: FragmentType,
77
73
  ...
78
74
  };
79
75
 
@@ -11,8 +11,6 @@
11
11
 
12
12
  'use strict';
13
13
 
14
- const {loadQuery} = require('./loadQuery');
15
-
16
14
  import type {
17
15
  EntryPoint,
18
16
  EntryPointComponent,
@@ -21,6 +19,8 @@ import type {
21
19
  PreloadedEntryPoint,
22
20
  } from './EntryPointTypes.flow';
23
21
 
22
+ const {loadQuery} = require('./loadQuery');
23
+
24
24
  function loadEntryPoint<
25
25
  TEntryPointParams: {...},
26
26
  TPreloadedQueries: {...},
@@ -51,12 +51,8 @@ function loadEntryPoint<
51
51
  if (queries != null) {
52
52
  const queriesPropNames = Object.keys(queries);
53
53
  queriesPropNames.forEach(queryPropName => {
54
- const {
55
- environmentProviderOptions,
56
- options,
57
- parameters,
58
- variables,
59
- } = queries[queryPropName];
54
+ const {environmentProviderOptions, options, parameters, variables} =
55
+ queries[queryPropName];
60
56
 
61
57
  const environment = environmentProvider.getEnvironment(
62
58
  environmentProviderOptions,
@@ -83,10 +79,8 @@ function loadEntryPoint<
83
79
  if (entryPointDescription == null) {
84
80
  return;
85
81
  }
86
- const {
87
- entryPoint: nestedEntryPoint,
88
- entryPointParams: nestedParams,
89
- } = entryPointDescription;
82
+ const {entryPoint: nestedEntryPoint, entryPointParams: nestedParams} =
83
+ entryPointDescription;
90
84
  preloadedEntryPoints[entryPointPropName] = loadEntryPoint(
91
85
  environmentProvider,
92
86
  nestedEntryPoint,
@@ -11,37 +11,35 @@
11
11
 
12
12
  'use strict';
13
13
 
14
- const React = require('react');
15
-
16
- const invariant = require('invariant');
17
- const warning = require('warning');
18
-
19
- const {
20
- PreloadableQueryRegistry,
21
- ReplaySubject,
22
- createOperationDescriptor,
23
- getRequest,
24
- getRequestIdentifier,
25
- Observable,
26
- RelayFeatureFlags,
27
- __internal: {fetchQueryDeduped},
28
- } = require('relay-runtime');
29
-
30
14
  import type {
15
+ LoadQueryOptions,
31
16
  PreloadableConcreteRequest,
32
17
  PreloadedQueryInner,
33
- LoadQueryOptions,
34
18
  } from './EntryPointTypes.flow';
35
19
  import type {
20
+ GraphQLResponse,
21
+ GraphQLTaggedNode,
36
22
  IEnvironment,
37
23
  OperationDescriptor,
38
24
  OperationType,
39
- GraphQLTaggedNode,
40
- GraphQLResponse,
41
25
  RequestIdentifier,
42
26
  RequestParameters,
43
27
  } from 'relay-runtime';
44
28
 
29
+ const invariant = require('invariant');
30
+ const React = require('react');
31
+ const {
32
+ Observable,
33
+ PreloadableQueryRegistry,
34
+ RelayFeatureFlags,
35
+ ReplaySubject,
36
+ __internal: {fetchQueryDeduped},
37
+ createOperationDescriptor,
38
+ getRequest,
39
+ getRequestIdentifier,
40
+ } = require('relay-runtime');
41
+ const warning = require('warning');
42
+
45
43
  let RenderDispatcher = null;
46
44
  let fetchKey = 100001;
47
45
 
@@ -59,7 +57,7 @@ function useTrackLoadQueryInRender() {
59
57
  function loadQuery<TQuery: OperationType, TEnvironmentProviderOptions>(
60
58
  environment: IEnvironment,
61
59
  preloadableRequest: GraphQLTaggedNode | PreloadableConcreteRequest<TQuery>,
62
- variables: $ElementType<TQuery, 'variables'>,
60
+ variables: TQuery['variables'],
63
61
  options?: ?LoadQueryOptions,
64
62
  environmentProviderOptions?: ?TEnvironmentProviderOptions,
65
63
  ): PreloadedQueryInner<TQuery, TEnvironmentProviderOptions> {
@@ -274,7 +272,8 @@ function loadQuery<TQuery: OperationType, TEnvironmentProviderOptions>(
274
272
  let cancelOnLoadCallback;
275
273
  let queryId;
276
274
  if (preloadableRequest.kind === 'PreloadableConcreteRequest') {
277
- const preloadableConcreteRequest: PreloadableConcreteRequest<TQuery> = (preloadableRequest: $FlowFixMe);
275
+ const preloadableConcreteRequest: PreloadableConcreteRequest<TQuery> =
276
+ (preloadableRequest: $FlowFixMe);
278
277
  ({params} = preloadableConcreteRequest);
279
278
 
280
279
  ({id: queryId} = params);
@@ -298,8 +297,8 @@ function loadQuery<TQuery: OperationType, TEnvironmentProviderOptions>(
298
297
  // store in the first place, so it couldn't have been cached.
299
298
  const networkObservable =
300
299
  fetchPolicy === 'store-only' ? null : makeNetworkRequest(params);
300
+ // $FlowFixMe[method-unbinding] added when improving typing for this parameters
301
301
  ({dispose: cancelOnLoadCallback} = PreloadableQueryRegistry.onLoad(
302
- // $FlowFixMe[incompatible-call]
303
302
  queryId,
304
303
  preloadedModule => {
305
304
  cancelOnLoadCallback();
@@ -318,7 +317,8 @@ function loadQuery<TQuery: OperationType, TEnvironmentProviderOptions>(
318
317
  ));
319
318
  }
320
319
  } else {
321
- const graphQlTaggedNode: GraphQLTaggedNode = (preloadableRequest: $FlowFixMe);
320
+ const graphQlTaggedNode: GraphQLTaggedNode =
321
+ (preloadableRequest: $FlowFixMe);
322
322
  const request = getRequest(graphQlTaggedNode);
323
323
  params = request.params;
324
324
  queryId = params.cacheID != null ? params.cacheID : params.id;
@@ -326,6 +326,27 @@ function loadQuery<TQuery: OperationType, TEnvironmentProviderOptions>(
326
326
  }
327
327
 
328
328
  let isDisposed = false;
329
+ let isReleased = false;
330
+ let isNetworkRequestCancelled = false;
331
+ const releaseQuery = () => {
332
+ if (isReleased) {
333
+ return;
334
+ }
335
+ retainReference && retainReference.dispose();
336
+ isReleased = true;
337
+ };
338
+ const cancelNetworkRequest = () => {
339
+ if (isNetworkRequestCancelled) {
340
+ return;
341
+ }
342
+ if (didExecuteNetworkSource) {
343
+ unsubscribeFromExecution && unsubscribeFromExecution();
344
+ } else {
345
+ unsubscribeFromNetworkRequest && unsubscribeFromNetworkRequest();
346
+ }
347
+ cancelOnLoadCallback && cancelOnLoadCallback();
348
+ isNetworkRequestCancelled = true;
349
+ };
329
350
  return {
330
351
  kind: 'PreloadedQuery',
331
352
  environment,
@@ -334,20 +355,17 @@ function loadQuery<TQuery: OperationType, TEnvironmentProviderOptions>(
334
355
  if (isDisposed) {
335
356
  return;
336
357
  }
337
- if (didExecuteNetworkSource) {
338
- unsubscribeFromExecution && unsubscribeFromExecution();
339
- } else {
340
- unsubscribeFromNetworkRequest && unsubscribeFromNetworkRequest();
341
- }
342
- retainReference && retainReference.dispose();
343
- cancelOnLoadCallback && cancelOnLoadCallback();
358
+ releaseQuery();
359
+ cancelNetworkRequest();
344
360
  isDisposed = true;
345
361
  },
362
+ releaseQuery,
363
+ cancelNetworkRequest,
346
364
  fetchKey,
347
365
  id: queryId,
348
366
  // $FlowFixMe[unsafe-getters-setters] - this has no side effects
349
367
  get isDisposed() {
350
- return isDisposed;
368
+ return isDisposed || isReleased;
351
369
  },
352
370
  // $FlowFixMe[unsafe-getters-setters] - this has no side effects
353
371
  get networkError() {
@@ -13,18 +13,6 @@
13
13
 
14
14
  'use strict';
15
15
 
16
- const invariant = require('invariant');
17
-
18
- const {
19
- createOperationDescriptor,
20
- Environment,
21
- getRequest,
22
- getRequestIdentifier,
23
- Observable,
24
- PreloadableQueryRegistry,
25
- ReplaySubject,
26
- } = require('relay-runtime');
27
-
28
16
  import type {
29
17
  PreloadableConcreteRequest,
30
18
  PreloadedQueryInner_DEPRECATED,
@@ -42,6 +30,16 @@ import type {
42
30
  Subscription,
43
31
  } from 'relay-runtime';
44
32
 
33
+ const {
34
+ Observable,
35
+ PreloadableQueryRegistry,
36
+ RelayFeatureFlags,
37
+ ReplaySubject,
38
+ createOperationDescriptor,
39
+ getRequest,
40
+ getRequestIdentifier,
41
+ } = require('relay-runtime');
42
+
45
43
  // Expire results by this delay after they resolve.
46
44
  const DEFAULT_PREFETCH_TIMEOUT = 30 * 1000; // 30 seconds
47
45
 
@@ -81,10 +79,6 @@ function preloadQuery<TQuery: OperationType, TEnvironmentProviderOptions>(
81
79
  options?: ?PreloadOptions,
82
80
  environmentProviderOptions?: ?TEnvironmentProviderOptions,
83
81
  ): PreloadedQueryInner_DEPRECATED<TQuery, TEnvironmentProviderOptions> {
84
- invariant(
85
- environment instanceof Environment,
86
- 'preloadQuery(): Expected a RelayModernEnvironment',
87
- );
88
82
  let _pendingQueries = pendingQueriesByEnvironment.get(environment);
89
83
  if (_pendingQueries == null) {
90
84
  _pendingQueries = new Map();
@@ -118,7 +112,23 @@ function preloadQuery<TQuery: OperationType, TEnvironmentProviderOptions>(
118
112
  }
119
113
  return () => {
120
114
  subscription?.unsubscribe();
121
- cleanup(pendingQueries, queryEntry);
115
+ if (environment.isServer()) {
116
+ return;
117
+ }
118
+ if (
119
+ RelayFeatureFlags.DELAY_CLEANUP_OF_PENDING_PRELOAD_QUERIES ===
120
+ true
121
+ ) {
122
+ setTimeout(() => {
123
+ // Clear the cache entry after the default timeout
124
+ // null-check for Flow
125
+ if (queryEntry != null) {
126
+ cleanup(pendingQueries, queryEntry);
127
+ }
128
+ }, DEFAULT_PREFETCH_TIMEOUT);
129
+ } else {
130
+ cleanup(pendingQueries, queryEntry);
131
+ }
122
132
  };
123
133
  })
124
134
  : null;
@@ -138,7 +148,7 @@ function preloadQuery<TQuery: OperationType, TEnvironmentProviderOptions>(
138
148
  }
139
149
 
140
150
  function preloadQueryDeduped<TQuery: OperationType>(
141
- environment: Environment,
151
+ environment: IEnvironment,
142
152
  pendingQueries: Map<string, PendingQueryEntry>,
143
153
  preloadableRequest: GraphQLTaggedNode | PreloadableConcreteRequest<TQuery>,
144
154
  variables: VariablesOf<TQuery>,
@@ -147,7 +157,8 @@ function preloadQueryDeduped<TQuery: OperationType>(
147
157
  let params;
148
158
  let query: ?ConcreteRequest;
149
159
  if (preloadableRequest.kind === 'PreloadableConcreteRequest') {
150
- const preloadableConcreteRequest: PreloadableConcreteRequest<TQuery> = (preloadableRequest: $FlowFixMe);
160
+ const preloadableConcreteRequest: PreloadableConcreteRequest<TQuery> =
161
+ (preloadableRequest: $FlowFixMe);
151
162
  params = preloadableConcreteRequest.params;
152
163
  query = params.id != null ? PreloadableQueryRegistry.get(params.id) : null;
153
164
  } else {
@@ -245,9 +256,7 @@ function preloadQueryDeduped<TQuery: OperationType>(
245
256
  } else {
246
257
  nextQueryEntry = prevQueryEntry;
247
258
  }
248
- // $FlowFixMe[incompatible-call]
249
259
  pendingQueries.set(cacheKey, nextQueryEntry);
250
- // $FlowFixMe[incompatible-return]
251
260
  return nextQueryEntry;
252
261
  }
253
262
 
@@ -13,8 +13,6 @@
13
13
 
14
14
  'use strict';
15
15
 
16
- const preloadQuery = require('./preloadQuery_DEPRECATED');
17
-
18
16
  import type {
19
17
  EntryPoint,
20
18
  EntryPointComponent,
@@ -22,6 +20,8 @@ import type {
22
20
  IEnvironmentProvider,
23
21
  } from './EntryPointTypes.flow';
24
22
 
23
+ const preloadQuery = require('./preloadQuery_DEPRECATED');
24
+
25
25
  function prepareEntryPoint<
26
26
  TEntryPointParams: {...},
27
27
  TPreloadedQueries: {...},
@@ -51,12 +51,8 @@ function prepareEntryPoint<
51
51
  if (queries != null) {
52
52
  const queriesPropNames = Object.keys(queries);
53
53
  queriesPropNames.forEach(queryPropName => {
54
- const {
55
- environmentProviderOptions,
56
- options,
57
- parameters,
58
- variables,
59
- } = queries[queryPropName];
54
+ const {environmentProviderOptions, options, parameters, variables} =
55
+ queries[queryPropName];
60
56
 
61
57
  const environment = environmentProvider.getEnvironment(
62
58
  environmentProviderOptions,
@@ -79,10 +75,8 @@ function prepareEntryPoint<
79
75
  if (entryPointDescription == null) {
80
76
  return;
81
77
  }
82
- const {
83
- entryPoint: nestedEntryPoint,
84
- entryPointParams: nestedParams,
85
- } = entryPointDescription;
78
+ const {entryPoint: nestedEntryPoint, entryPointParams: nestedParams} =
79
+ entryPointDescription;
86
80
  preloadedEntryPoints[entryPointPropName] = prepareEntryPoint(
87
81
  environmentProvider,
88
82
  nestedEntryPoint,