-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refetchQueries only refetches a single query #3540
Comments
Thanks for reporting this @wmertens. This definitely sounds like a bug. If you or anyone else that 👍'd this are able to put together a reproduction, that would definitely help get this resolved more quickly. With regards to where this might be happening in the codebase, this is where all queries should be refetched: apollo-client/packages/apollo-client/src/core/QueryManager.ts Lines 1157 to 1169 in eecaf22
|
I'm in a similar boat. We have multiple instances of |
So I remembered I wrote a version of @wmertens It might work for you too, feel free to try it: import { type ApolloClient } from 'apollo-client'
import { compact, flatten, map, values } from 'lodash'
export const refetchQueriesByName = (client: ApolloClient, queryNames: Array<string>) =>
Promise.all(map(queryNames, (queryName: string) => refetchQueryByName(client, queryName)))
// We're re-writing QueryManager refetchQueryByName to be less brittle:
// https://github.com/apollographql/apollo-client/blob/88a77511467b2735e841df86073ee3af51e88eec/src/core/QueryManager.ts#L1004
export const refetchQueryByName = (client: ApolloClient, queryName: string) => {
const refetchedQueries = getObservableQueriesByName(client, queryName)
return refetchObservableQueries(refetchedQueries)
}
export const refetchAllQueries = (client: ApolloClient) => {
const refetchedQueries = getAllObservableQueries(client)
return refetchObservableQueries(refetchedQueries)
}
export const refetchObservableQueries = (refetchedQueries: Array<Object>) => {
const promises = compact(map(refetchedQueries, (observableQuery: Object) => {
if (isObservableQueryRefetchable(observableQuery)) {
return observableQuery.refetch()
}
}))
return Promise.all(promises)
}
export const getObservableQueriesByName = (client: ApolloClient, queryName: string): Array<Object> => {
const { queryManager: { queries, queryIdsByName } } = client
const queryIds = queryIdsByName[queryName] || []
return map(queryIds, (queryId: string) => queries.get(queryId).observableQuery)
}
export const getAllObservableQueries = (client: ApolloClient): Array<Object> => {
const { queryManager: { queries, queryIdsByName } } = client
const queryIds = flatten(values(queryIdsByName))
return map(queryIds, (queryId: string) => queries.get(queryId).observableQuery)
}
export const isObservableQueryRefetchable = (observableQuery: Object): boolean => observableQuery.options.fetchPolicy !== 'cache-only' EDIT: Oct 2019 – the apollo internals have changed since posting this. It no longer works. |
Spent a while on this one. @hwillson there is a repro here: https://github.com/arist0tl3/apollo-client-test When When I know I've seen other |
Yeah like so many other things on Apollo that you expect to work like they should, or at least how it is explained in the docs. Sometimes the project feels abandoned. |
@TSMMark Thank you for the code. I think it should really be part of Apollo core; being able to call refetchQueries outside of a mutation is pretty essential. I might incorporate it as part of a library, and I'll be sure to include a link to this comment. |
@TSMMark I've created a package called apollo-refetch-queries with your code. I've added you as an author if you're comfortable with that. |
@asselstine No problem! Hope it works for you, thanks for bundling it into a package. Have you had any issues with that code in the wild so far? |
@TSMMark Just the ordering of the functions; I had to reorder them to get it to work, and also force the casting of the queryManager to |
FWIW: I thought we were experiencing this issue (which surfaced with either the |
Ok, great find! So the issue is really that non-active queries with the
same name need to be removed from cache?
…On Thu., Mar. 21, 2019, 9:48 p.m. Keith Gillette ***@***.***> wrote:
FWIW: I thought we were experiencing this issue (which surfaced with
either the refetchQueries: ['QueryName'] or refetchQueries: () =>
['QueryName'] syntax) but I think it's actually the case that
ApolloClient is only refetching queries with active subscriptions, not all
of those with matching names that still exist in the cache. In previous
testing, I had not paid attention to the subscription status of the queries
but realize now that only 1 was actively subscribed when refetchQueries
was invoked. In further testing, if I do not unsubscribe from a watchQuery
when the calling component is destroyed so that the subscription remains
active, then refetchQueries does indeed refetch all the queries of the
same name but different variables.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3540 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AADWlpuSoRBBdvbcxZMRI1k_Zy8JHsxQks5vY_AEgaJpZM4UY-Y0>
.
|
Well, I suppose if queries with no subscribers were removed from the cache, that would also force a fetch to occur, @wmertens, but then it wouldn't be much of a cache, as lots of queries will be fetched and re-read from the cache with a |
Err, no - only refetchqueries should invalidate the cache entries. |
Yes, invalidating the cache entries of queries with matching names on a |
The inconsistent behavior of refetching with apollo has resulted in a ton of extra I agree with @wmertens and @KeithGillette, an invalidation approach like that could work. |
Same problem for me. |
For posterity -- the other thing to check is the |
A lot of the Apollo Client internals have changed since v3 was launched. We recommend trying a more modern version of |
Intended outcome:
After a mutation, the
refetchQueries
prop is used and all queries that have matching names are refetched.Actual outcome:
After a mutation, the
refetchQueries
prop is used and the first query for each matching name is refetched.How to reproduce the issue:
I don't have time to create a reproduction right now, I'm hoping this rings a bell, if not I can create a repro.
Basically, we have several queries with the same name that have slightly different fields, eg.
query foo { foo { id bar } }
andquery foo { foo { id meep } }
. Since v2.1 of react-apollo, only one of those queries gets updated.Version
It used to work before.
(This is a copy of apollographql/react-apollo#1897 - apologies if that's bad form, but that one didn't get any replies and I thought that maybe it's more related to apollo-client anyway.)
The text was updated successfully, but these errors were encountered: