Refetch Query with Apollo GraphQL Client
The Apollo GraphQL client allows you to easily refetch query results, which can come in super handy if you need to refresh data after some user action on the page. You can do this with the refetch
function from useQuery
:
const { data, loading, refetch } = useQuery(QUERY);
Then if you want to refetch data after a button click, you can:
<button onClick={() => refetch()}>
Refetch the data!!
</button>
If you defined any variables in the initial useQuery
, refetching will re-use those variables, but you can override them as well in the call to refetch
:
refetch({ param: "new value" });
If you need to refetch data after a mutation, you can do that too.
Tweet