Today I Learned

hashrocket A Hashrocket project

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.

Docs

See More #react TILs
Looking for help? At Hashrocket, our JavaScript experts launch scalable, performant apps on the Web, Android and iOS. Contact us and find out how we can help you.