Skip to content

Commit c3b70bc

Browse files
tyao1facebook-github-bot
authored andcommitted
Handle args and returns, convert useLazyLoadQuery
Reviewed By: lynnshaoyu Differential Revision: D72883023 fbshipit-source-id: 13164fe99ec9f9bab58693349f0dd83b435aacf4
1 parent 9b7f643 commit c3b70bc

File tree

3 files changed

+91
-41
lines changed

3 files changed

+91
-41
lines changed

packages/react-relay/relay-hooks/useLazyLoadQuery.js

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,72 @@ const {
3232
export type UseLazyLoadQueryHookType = hook <TVariables: Variables, TData>(
3333
gqlQuery: Query<TVariables, TData>,
3434
variables: TVariables,
35-
options?: $ReadOnly<{
36-
fetchKey?: string | number,
37-
fetchPolicy?: FetchPolicy,
38-
networkCacheConfig?: CacheConfig,
39-
UNSTABLE_renderPolicy?: RenderPolicy,
40-
}>,
35+
options?: Options,
4136
) => TData;
4237

38+
type Options = {
39+
/**
40+
* Determines if cached data should be used, and when to send a network request based on the cached data that is currently available in the Relay store (for more details, see our [Fetch Policies](../../guided-tour/reusing-cached-data/fetch-policies) and [Garbage Collection](../../guided-tour/reusing-cached-data/presence-of-data) guides):
41+
* * "store-or-network": _*(default)*_ *will* reuse locally cached data and will *only* send a network request if any data for the query is missing. If the query is fully cached, a network request will *not* be made.
42+
* * "store-and-network": *will* reuse locally cached data and will *always* send a network request, regardless of whether any data was missing from the local cache or not.
43+
* * "network-only": *will* *not* reuse locally cached data, and will *always* send a network request to fetch the query, ignoring any data that might be locally cached in Relay.
44+
* * "store-only": *will* *only* reuse locally cached data, and will *never* send a network request to fetch the query. In this case, the responsibility of fetching the query falls to the caller, but this policy could also be used to read and operate on data that is entirely [local](../../guided-tour/updating-data/local-data-updates).
45+
*/
46+
+fetchPolicy?: FetchPolicy,
47+
/**
48+
* A `fetchKey` can be passed to force a re-evaluation of the current query and variables when the component re-renders, even if the variables didn't change, or even if the component isn't remounted (similarly to how passing a different `key` to a React component will cause it to remount). If the `fetchKey` is different from the one used in the previous render, the current query will be re-evaluated against the store, and it might be refetched depending on the current `fetchPolicy` and the state of the cache.
49+
*/
50+
+fetchKey?: string | number,
51+
/**
52+
* Default value: `{force: true}`. Object containing cache config options for the *network layer*. Note that the network layer may contain an *additional* query response cache which will reuse network responses for identical queries. If you want to bypass this cache completely (which is the default behavior), pass `{force: true}` as the value for this option.
53+
*/
54+
+networkCacheConfig?: CacheConfig,
55+
/**
56+
* Undocumented option.
57+
*/
58+
+UNSTABLE_renderPolicy?: RenderPolicy,
59+
};
60+
61+
/**
62+
* Hook used to fetch a GraphQL query during render. This hook can trigger multiple nested or waterfalling round trips if used without caution, and waits until render to start a data fetch (when it can usually start a lot sooner than render), thereby degrading performance. Instead, prefer [`usePreloadedQuery`](../use-preloaded-query).
63+
*
64+
* @example
65+
* const React = require('React');
66+
*
67+
* const {graphql, useLazyLoadQuery} = require('react-relay');
68+
*
69+
* function App() {
70+
* const data = useLazyLoadQuery(
71+
* graphql`
72+
* query AppQuery($id: ID!) {
73+
* user(id: $id) {
74+
* name
75+
* }
76+
* }
77+
* `,
78+
* {id: 4},
79+
* {fetchPolicy: 'store-or-network'},
80+
* );
81+
*
82+
* return <h1>{data.user?.name}</h1>;
83+
* }
84+
*
85+
* @returns - `data`: Object that contains data which has been read out from the Relay store; the object matches the shape of specified query.
86+
* - The Flow type for data will also match this shape, and contain types derived from the GraphQL Schema. For example, the type of `data` above is: `{| user: ?{| name: ?string |} |}`.
87+
*/
4388
hook useLazyLoadQuery<TVariables: Variables, TData>(
89+
/**
90+
* GraphQL query specified using a `graphql` template literal.
91+
*/
4492
gqlQuery: Query<TVariables, TData>,
93+
/**
94+
* Object containing the variable values to fetch the query. These variables need to match GraphQL variables declared inside the query.
95+
*/
4596
variables: TVariables,
46-
options?: $ReadOnly<{
47-
fetchKey?: string | number,
48-
fetchPolicy?: FetchPolicy,
49-
networkCacheConfig?: CacheConfig,
50-
UNSTABLE_renderPolicy?: RenderPolicy,
51-
}>,
97+
/**
98+
* options object
99+
*/
100+
options?: Options,
52101
): TData {
53102
const environment = useRelayEnvironment();
54103

@@ -71,4 +120,4 @@ hook useLazyLoadQuery<TVariables: Variables, TData>(
71120
}
72121

73122
// $FlowFixMe[react-rule-hook-incompatible]
74-
module.exports = (useLazyLoadQuery: UseLazyLoadQueryHookType);
123+
module.exports = useLazyLoadQuery;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
### Behavior
2+
3+
* It is expected for `useLazyLoadQuery` to have been rendered under a [`RelayEnvironmentProvider`](../relay-environment-provider), in order to access the correct Relay environment, otherwise an error will be thrown.
4+
* Calling `useLazyLoadQuery` will fetch and render the data for this query, and it may [*_suspend_*](../../guided-tour/rendering/loading-states) while the network request is in flight, depending on the specified `fetchPolicy`, and whether cached data is available, or if it needs to send and wait for a network request. If `useLazyLoadQuery` causes the component to suspend, you'll need to make sure that there's a `Suspense` ancestor wrapping this component in order to show the appropriate loading state.
5+
* For more details on Suspense, see our [Loading States with Suspense](../../guided-tour/rendering/loading-states/) guide.
6+
* The component is automatically subscribed to updates to the query data: if the data for this query is updated anywhere in the app, the component will automatically re-render with the latest updated data.
7+
* After a component using `useLazyLoadQuery` has committed, re-rendering/updating the component will not cause the query to be fetched again.
8+
* If the component is re-rendered with *different query variables,* that will cause the query to be fetched again with the new variables, and potentially re-render with different data.
9+
* If the component *unmounts and remounts*, that will cause the current query and variables to be refetched (depending on the `fetchPolicy` and the state of the cache).
10+
11+
### Differences with `QueryRenderer`
12+
13+
* `useLazyLoadQuery` no longer takes a Relay environment as a parameter, and thus no longer sets the environment in React Context, like `QueryRenderer` did. Instead, `useLazyLoadQuery` should be used as a descendant of a [`RelayEnvironmentProvider`](../relay-environment-provider), which now sets the Relay environment in Context. Usually, you should render a single `RelayEnvironmentProvider` at the very root of the application, to set a single Relay environment for the whole application.
14+
* `useLazyLoadQuery` will use [Suspense](../../guided-tour/rendering/loading-states) to allow developers to render loading states using Suspense boundaries, and will throw errors if network errors occur, which can be caught and rendered with Error Boundaries. This as opposed to providing error objects or null props to the `QueryRenderer` render function to indicate errors or loading states.
15+
* `useLazyLoadQuery` fully supports fetch policies in order to reuse data that is cached in the Relay store instead of solely relying on the network response cache.
16+
* `useLazyLoadQuery` has better type safety guarantees for the data it returns, which was not possible with QueryRenderer since we couldn't parametrize the type of the data with a renderer api.

website/docs/api-reference/hooks/use-lazy-load-query.md

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,39 +39,24 @@ function App() {
3939
4040
### Arguments
4141
42-
* `query`: GraphQL query specified using a `graphql` template literal.
42+
* `gqlQuery`: GraphQL query specified using a `graphql` template literal.
4343
* `variables`: Object containing the variable values to fetch the query. These variables need to match GraphQL variables declared inside the query.
4444
* `options`: _*[Optional]*_ options object
45-
* `fetchPolicy`: Determines if cached data should be used, and when to send a network request based on the cached data that is currently available in the Relay store (for more details, see our [Fetch Policies](../../guided-tour/reusing-cached-data/fetch-policies) and [Garbage Collection](../../guided-tour/reusing-cached-data/presence-of-data) guides):
46-
* "store-or-network": _*(default)*_ *will* reuse locally cached data and will *only* send a network request if any data for the query is missing. If the query is fully cached, a network request will *not* be made.
47-
* "store-and-network": *will* reuse locally cached data and will *always* send a network request, regardless of whether any data was missing from the local cache or not.
48-
* "network-only": *will* *not* reuse locally cached data, and will *always* send a network request to fetch the query, ignoring any data that might be locally cached in Relay.
49-
* "store-only": *will* *only* reuse locally cached data, and will *never* send a network request to fetch the query. In this case, the responsibility of fetching the query falls to the caller, but this policy could also be used to read and operate on data that is entirely [local](../../guided-tour/updating-data/local-data-updates).
50-
* `fetchKey`: A `fetchKey` can be passed to force a re-evaluation of the current query and variables when the component re-renders, even if the variables didn't change, or even if the component isn't remounted (similarly to how passing a different `key` to a React component will cause it to remount). If the `fetchKey` is different from the one used in the previous render, the current query will be re-evaluated against the store, and it might be refetched depending on the current `fetchPolicy` and the state of the cache.
51-
* `networkCacheConfig`: *_[Optional] _* Default value: `{force: true}`. Object containing cache config options for the *network layer*. Note that the network layer may contain an *additional* query response cache which will reuse network responses for identical queries. If you want to bypass this cache completely (which is the default behavior), pass `{force: true}` as the value for this option.
45+
* `fetchPolicy`: _*[Optional]*_ Determines if cached data should be used, and when to send a network request based on the cached data that is currently available in the Relay store (for more details, see our [Fetch Policies](../../guided-tour/reusing-cached-data/fetch-policies) and [Garbage Collection](../../guided-tour/reusing-cached-data/presence-of-data) guides):
46+
* "store-or-network": _*(default)*_ *will* reuse locally cached data and will *only* send a network request if any data for the query is missing. If the query is fully cached, a network request will *not* be made.
47+
* "store-and-network": *will* reuse locally cached data and will *always* send a network request, regardless of whether any data was missing from the local cache or not.
48+
* "network-only": *will* *not* reuse locally cached data, and will *always* send a network request to fetch the query, ignoring any data that might be locally cached in Relay.
49+
* "store-only": *will* *only* reuse locally cached data, and will *never* send a network request to fetch the query. In this case, the responsibility of fetching the query falls to the caller, but this policy could also be used to read and operate on data that is entirely [local](../../guided-tour/updating-data/local-data-updates).
50+
* `fetchKey`: _*[Optional]*_ A `fetchKey` can be passed to force a re-evaluation of the current query and variables when the component re-renders, even if the variables didn't change, or even if the component isn't remounted (similarly to how passing a different `key` to a React component will cause it to remount). If the `fetchKey` is different from the one used in the previous render, the current query will be re-evaluated against the store, and it might be refetched depending on the current `fetchPolicy` and the state of the cache.
51+
* `networkCacheConfig`: _*[Optional]*_ Default value: `{force: true}`. Object containing cache config options for the *network layer*. Note that the network layer may contain an *additional* query response cache which will reuse network responses for identical queries. If you want to bypass this cache completely (which is the default behavior), pass `{force: true}` as the value for this option.
52+
* `UNSTABLE_renderPolicy`: _*[Optional]*_ Undocumented option.
5253
5354
### Return Value
5455
55-
* `data`: Object that contains data which has been read out from the Relay store; the object matches the shape of specified query.
56-
* The Flow type for data will also match this shape, and contain types derived from the GraphQL Schema. For example, the type of `data` above is: `{| user: ?{| name: ?string |} |}`.
57-
58-
### Behavior
59-
60-
* It is expected for `useLazyLoadQuery` to have been rendered under a [`RelayEnvironmentProvider`](../relay-environment-provider), in order to access the correct Relay environment, otherwise an error will be thrown.
61-
* Calling `useLazyLoadQuery` will fetch and render the data for this query, and it may [*_suspend_*](../../guided-tour/rendering/loading-states) while the network request is in flight, depending on the specified `fetchPolicy`, and whether cached data is available, or if it needs to send and wait for a network request. If `useLazyLoadQuery` causes the component to suspend, you'll need to make sure that there's a `Suspense` ancestor wrapping this component in order to show the appropriate loading state.
62-
* For more details on Suspense, see our [Loading States with Suspense](../../guided-tour/rendering/loading-states/) guide.
63-
* The component is automatically subscribed to updates to the query data: if the data for this query is updated anywhere in the app, the component will automatically re-render with the latest updated data.
64-
* After a component using `useLazyLoadQuery` has committed, re-rendering/updating the component will not cause the query to be fetched again.
65-
* If the component is re-rendered with *different query variables,* that will cause the query to be fetched again with the new variables, and potentially re-render with different data.
66-
* If the component *unmounts and remounts*, that will cause the current query and variables to be refetched (depending on the `fetchPolicy` and the state of the cache).
67-
68-
### Differences with `QueryRenderer`
69-
70-
* `useLazyLoadQuery` no longer takes a Relay environment as a parameter, and thus no longer sets the environment in React Context, like `QueryRenderer` did. Instead, `useLazyLoadQuery` should be used as a descendant of a [`RelayEnvironmentProvider`](../relay-environment-provider), which now sets the Relay environment in Context. Usually, you should render a single `RelayEnvironmentProvider` at the very root of the application, to set a single Relay environment for the whole application.
71-
* `useLazyLoadQuery` will use [Suspense](../../guided-tour/rendering/loading-states) to allow developers to render loading states using Suspense boundaries, and will throw errors if network errors occur, which can be caught and rendered with Error Boundaries. This as opposed to providing error objects or null props to the `QueryRenderer` render function to indicate errors or loading states.
72-
* `useLazyLoadQuery` fully supports fetch policies in order to reuse data that is cached in the Relay store instead of solely relying on the network response cache.
73-
* `useLazyLoadQuery` has better type safety guarantees for the data it returns, which was not possible with QueryRenderer since we couldn't parametrize the type of the data with a renderer api.
74-
56+
- `data`: Object that contains data which has been read out from the Relay store; the object matches the shape of specified query.
57+
- The Flow type for data will also match this shape, and contain types derived from the GraphQL Schema. For example, the type of `data` above is: `{| user: ?{| name: ?string |} |}`.
7558
59+
import UseLazyLoadQueryExtra from './_use-lazy-load-query-extra.md';
7660
61+
<UseLazyLoadQueryExtra />
7762
<DocsRating />

0 commit comments

Comments
 (0)