useRefetchableFragment
useRefetchableFragment
You can use useRefetchableFragment when you want to fetch and re-render a fragment with different data:
import * as React from "react";
import {
graphql,
useRefetchableFragment,
} from "@graphitation/apollo-react-relay-duct-tape";
import { CommentBodyRefetchQuery } from "./__generated__/CommentBodyRefetchQuery.graphql";
import { CommentBody_comment$key } from "./__generated__/CommentBody_comment.graphql";
interface Props {
comment: CommentBody_comment$key;
}
const CommentBody: React.FC<Props> = (props) => {
const [data, refetch] = useRefetchableFragment<CommentBodyRefetchQuery>(
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>
</>
);
};
export default CommentBody;
Arguments
fragment: GraphQL fragment specified using agraphqltemplate literal.- This fragment must have a
@refetchabledirective, otherwise using it will throw an error. The@refetchabledirective can only be added to fragments that are "refetchable", that is, on fragments that are declared on theQuerytype, or on a type that implementsNode(i.e. a type that has anid). - Note that you do not need to manually specify a refetch query yourself. The
@refetchabledirective will autogenerate a query with the specifiedqueryName. This will also generate TypeScript types for the query, available to import from the generated file:<queryName>.graphql.ts.
- This fragment must have a
fragmentReference: The fragment reference is an opaque object that Apollo React/Relay Duct-Tape 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.- The type of the fragment reference can be imported from the generated TypeScript types, from the file
<fragment_name>.graphql.ts, and can be used to declare the type of yourProps. The name of the fragment reference type will be:<fragment_name>$key.
- The type of the fragment reference can be imported from the generated TypeScript types, from the file
TypeScript Parameters
TQuery: Type parameter that should corresponds the TypeScript type for the@refetchablequery. This type is available to import from the the auto-generated file:<queryName>.graphql.ts.TFragmentRef: Type parameter corresponds to the type of the fragment reference argument (i.e.<fragment_name>$key). This type usually does not need to be explicitly specified, and can be omitted to let TypeScript infer the concrete type.
Return Value
Tuple containing the following values
- [0]
data: Object that contains data which has been read out from the store; the object matches the shape of specified fragment.- The TypeScript type for data will also match this shape, and contain types derived from the GraphQL Schema.
- [1]
refetch: Function used to refetch the fragment with a potentially new set of variables.- Arguments:
variables: Object containing the new set of variable values to be used to fetch the@refetchablequery.- These variables need to match GraphQL variables referenced inside the fragment.
- However, only the variables that are intended to change for the refetch request need to be specified; any variables referenced by the fragment that are omitted from this input will fall back to using the value specified in the original parent query. So for example, to refetch the fragment with the exact same variables as it was originally fetched, you can call
refetch({}). - Similarly, passing an
idvalue for the$idvariable is optional, unless the fragment wants to be refetched with a differentid. When refetching a@refetchablefragment, Apollo React/Relay Duct-Tape will already know the id of the rendered object.
options: [Optional] options objectfetchPolicy: 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.
- Return value:
disposable: Object containing adisposefunction. Callingdisposable.dispose()will cancel the refetch request.
- Behavior:
- Calling
refetchwith 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 thelangvariable.
- Calling
- Arguments:
Behavior
- The component is automatically subscribed to updates to the fragment data: if the data for this particular
Commentis 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. - An in-flight refetch request will automatically be disposed when the component unmounts.