Hi, I’m using useBackgroundQuery to aggregate data into a single query.
I have a structure like the following:
const FRAGMENT = gql`
fragment ComponentXFragment on Query {
myList {
id
}
}
`
const ComponentX = ({ queryRef }) => {
const { data } = useReadQuery(queryRef)
const { data: { myList } } = useSuspenseFragment({
fragment: FRAGMENT,
from: data,
})
return <ul>{myList.map(({ id }) => <li>{id}</li>)}</ul>
}
const QUERY = gql`
query PageQuery {
__typename
...ComponentXFragment
}
`
const Page = () => {
const [queryRef] = useBackgroundQuery(QUERY)
return (
<Suspense fallback={null}>
<ComponentX queryRef={queryRef} />
</Suspense>
)
}
Then I have another component that executes a mutation and invalidates the cache like this:
useMutation(MUTATION, {
update(cache) {
cache.evict({ id: 'ROOT_QUERY', fieldName: 'myList' })
cache.gc()
},
})
cache.evict correctly remove the field from the cache, but the query from useBackgroundQuery is not being re-executed. Using refetchQueries instead of cache.evict works.
Does useBackgroundQuery only execute the request on first render and then never trigger it again? But I’ve also tried with useQuery and the behaviour is the same.
How should I handle this? I don’t want to use refetchQueries because it requires to know every query name that uses myList. I just want to invalidate fields and let subscribed queries automatically refetch.