I'm using Apollo Client's <Query>
within a component that is re-rendered when state is changed within a lifecycle method. I wish to have my <Query>
component re-run the query because I know that data has changed.
It appears that Query component is using a cache that needs to be invalidated before query is re-run.
I'm using a wonky workaround that caches the refetch
callback from the render prop in the parent component, but it feels wrong. I'll post my approach in the answers if anyone is interested.
My code looks something like this. I removed loading
and error
handling from query as well as some other detail for brevity.
class ParentComponent extends React.Component {
componentDidUpdate(prevProps) {
if (this.props.refetchId !== prevProps.refetchId) {
const otherData = this.processData() // do something
this.setState({otherData}) // this forces component to reload
}
}
render() {
const { otherData } = this.state
return (
<Query query={MY_QUERY}>
{({ data }) => {
return <ChildComponent gqlData={data} stateData={otherData} />
}}
</Query>
)
}
}
How do I force <Query>
to fetch new data to pass to <ChildComponent>
?
Even though ParentComponent
re-renders when props or state change, Query
doesn't re-run. ChildComponent
gets an updated stateData
prop, but has a stale gqlData
prop. As I understand Apollo's query cache need to be invalidated, but I'm not sure.
Please note that passing refetch
to ChildComponent is not the answer because it only displays information from GraphQL and wouldn't know when to refetch. I don't want to introduce timers or otherwise complicate ChildComponent
to solve this - it doesn't need to know about this complexity or data fetching concerns.