Comments (6)
Thanks @TSMMark!
It's funny, I think we actually patched this in our application too. Hopefully one of us can get around to this soon :)
from apollo-refetch-queries.
Not sure if this helps but I hacked together a hook pretty quickly, it does not handle variables yet though
export const useRefetchByQueryNames = () => {
const client = useApolloClient()
const queries: Map<string, any> | undefined = R.path(
['queryManager', 'queries'],
client
)
return (queryNames: string[]) => {
if (queries) {
let refetchFns = []
queries.forEach(q => {
const name = R.path(['observableQuery', 'queryName'], q)
if (name && queryNames.includes(name as string)) {
// @ts-ignore
refetchFns.push(q.observableQuery.refetch.bind(q.observableQuery))
}
})
refetchFns.forEach((fn: Function) => {
fn()
})
}
}
}
from apollo-refetch-queries.
Yep, that's true, but it's the wanted behaviour in my case.
If you're looking for only invalidation without refetching, you could try this:
const client = useApolloClient();
function invalidateCache() {
client.queryManager.queries.forEach((query, queryId) => {
client.queryManager.invalidate(queryId);
});
}
No idea if that works properly, but it uses some things I found in the QueryManager class. Maybe it's helpful to you!
from apollo-refetch-queries.
AFAICT queryIdsByName
is no longer exposed by Apollo. So I reimplemented queryIdsByName
as a function. This fixes the internals of this library's refetchQueries
functions, while reducing the amount of code changes necessary in the other internal functions of the library.
At @Vydia, we never actually looped back and migrated our application code to use this package, so I'm working from my original issue comment code, but this is working for us now:
export const getObservableQueriesByName = (client: ApolloClient, queryName: string): Array<Object> => {
const queryIdsByName = getQueryIdsByName(client)
const { queryManager: { queries } } = client
const queryIds = queryIdsByName[queryName] || []
return map(queryIds, (queryId: string) => queries.get(queryId).observableQuery)
}
export const getAllObservableQueries = (client: ApolloClient): Array<Object> => {
const queryIdsByName = getQueryIdsByName(client)
const { queryManager: { queries } } = client
const queryIds = flatten(values(queryIdsByName))
return map(queryIds, (queryId: string) => queries.get(queryId).observableQuery)
}
export const getQueryIdsByName = (client: ApolloClient): $Shape<{| [string]: string[] |}> => {
const { queryManager: { queries } } = client
const queryIdsByName = {}
// `queries` contains a Map instance. I'm not sure the best way to transform a Map - is there a better way to do this loop?
queries.forEach((value, key) => {
(queryIdsByName[value.observableQuery.queryName] = (queryIdsByName[value.observableQuery.queryName] || [])).push(key)
})
return queryIdsByName
}
I'll try to find some time to submit a PR to this package, and to migrate our application codebase at @Vydia to use this package.
from apollo-refetch-queries.
@adamtysonsmith If I'm not mistaken, it does handle variables.
Previous variables are reused if you don't provide new ones:
I made a function that invalidates all currently active queries:
const client = useApolloClient();
function invalidateCache() {
client.queryManager.queries.forEach(query => {
if (!query.observableQuery) return;
query.observableQuery.refetch();
});
}
from apollo-refetch-queries.
@amannn That looks cool but that could be a very expensive operation as it looks like it actually refetches all the queries, not only invalidating them right? Do you know of a way to ONLY invalidate without refetching? And let queries hit the network later, on demand, as needed?
from apollo-refetch-queries.
Related Issues (1)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apollo-refetch-queries.