website/versioned_docs/version-v15.0.0/api-reference/hooks/use-refetchable-fragment.md
import DocsRating from '@site/src/core/DocsRating'; import {FbInternalOnly, OssOnly} from 'docusaurus-plugin-internaldocs-fb/internal';
useRefetchableFragmentYou can use useRefetchableFragment when you want to fetch and re-render a fragment with different data:
import type {CommentBody_comment$key} from 'CommentBody_comment.graphql';
const React = require('React');
const {graphql, useRefetchableFragment} = require('react-relay');
type Props = {
comment: CommentBody_comment$key,
};
function CommentBody(props: Props) {
const [data, refetch] = useRefetchableFragment(
graphql`
fragment CommentBody_comment on Comment
@refetchable(queryName: "CommentBodyRefetchQuery") {
body(lang: $lang) {
text
}
}
`,
props.comment,
);
return (
<>
<p>{data.body?.text}</p>
<Button
onClick={() => {
refetch({lang: 'SPANISH'}, {fetchPolicy: 'store-or-network'})
}}
>
Translate Comment
</Button>
</>
);
}
module.exports = CommentBody;
fragment: GraphQL fragment specified using a graphql template literal. This fragment must have a @refetchable directive, otherwise using it will throw an error. The @refetchable directive can only be added to fragments that are "refetchable", that is, on fragments that are declared on Viewer or Query types, or on a type that implements Node (i.e. a type that has an id).
@refetchable directive will autogenerate a query with the specified queryName. This will also generate Flow types for the query, available to import from the generated file: <queryName>.graphql.js.fragmentReference: The fragment reference is an opaque Relay object that Relay uses to read the data for the fragment from the store; more specifically, it contains information about which particular object instance the data should be read from.
<fragment_name>.graphql.js, and can be used to declare the type of your Props. The name of the fragment reference type will be: <fragment_name>$key. We use our lint rule to enforce that the type of the fragment reference prop is correctly declared.Tuple containing the following values
data: Object that contains data which has been read out from the Relay store; the object matches the shape of specified fragment.
refetch: Function used to refetch the fragment with a potentially new set of variables.
variables: Object containing the new set of variable values to be used to fetch the @refetchable query.
refetch({}).id value for the $id variable is optional, unless the fragment wants to be refetched with a different id. When refetching a @refetchable fragment, Relay will already know the id of the rendered object.options: [Optional] options object
fetchPolicy: Determines if cached data should be used, and when to send a network request based on cached data that is available. See the Fetch Policies section for full specification.onComplete: Function that will be called whenever the refetch request has completed, including any incremental data payloads.disposable: Object containing a dispose function. Calling disposable.dispose() will cancel the refetch request.refetch with a new set of variables will fetch the fragment again with the newly provided variables. Note that the variables you need to provide are only the ones referenced inside the fragment. In this example, it means fetching the translated body of the currently rendered Comment, by passing a new value to the lang variable.refetch will re-render your component and may cause it to suspend, depending on the specified fetchPolicy and whether cached data is available or if it needs to send and wait for a network request. If refetch causes the component to suspend, you'll need to make sure that there's a Suspense boundary wrapping this component.User is updated anywhere in the app (e.g. via fetching new data, or mutating existing data), the component will automatically re-render with the latest updated data.RefetchContainer@refetchable fragment.refetchVariables and renderVariables, which were previously vaguely defined concepts. Refetching will always correctly refetch and render the fragment with the variables you provide (any variables omitted in the input will fallback to using the original values from the parent query).RefetchContainer (it would depend on what you were querying for in the refetch query and if your fragment was defined on the right object type).