#set default `exact` option
19 messages ยท Page 1 of 1 (latest)
nope. How do your queryKeys look like? A little restructure might help
well, generally the http call itself really inspires the key.
but generally speaking, most of the time I have a
const todosKeys = {
all: [{ orgId }, 'todos'],
single: (id: string) => ([...todosKeys.all, id])
}
The orgId is sort of a prefix for context,
while using my app, a user can be assocaited to several "organizations", and move between them (and they're completely unrelated with completely different data, that's why it felt important to contextualize).
So say, when I create a new todo
I wanna
queryClient.setQueryData(single(newTodo.id), newTodo);
queryClient.refetchQueries({ queryKey: all, exact: true });
or when I remove a todo, I only want to do
onSettled: () => queryClient.refetchQueries({ queryKey: all, exact: true })
I don't want all todos in the world to be refetched
does this make sense? am I going with a "correct" approach?
Also - it doesn't really help that there're no examples / calrification as to what does exact: false match with ๐ฐ
So, if you refetch queryKey: todoKeys.all, I'd expect everything todo-related to be refetched (the list and the details). At least from reading the code. So I'd probably structure a bit more, like:
const todosKeys = {
all: [{ orgId }, 'todos'],
list: () => ([...todosKeys.all, 'list'])
single: (id: string) => ([...todosKeys.all, 'single', id])
}
If I want to refetch the list, I'd do:
queryKey: todoKeys.list()
and then no "exact" is needed
but then every "single" todo would be refetched, no?
since that's what "inclusive" means, or am I misunderstanding?
if my filter is [{ orgId }, 'todos'], and say this is my cache (figuratively)
{
[{ orgId }, 'todos']: ...all todos,
[{ orgId }, 'todos', { id: 1 }]: ...todo1,
[{ orgId }, 'todos', { id: 2 }]: ...todo2,
}
all of these would match the given filter, right?
also - I didn't get why separating between all and list gives any benefit ๐
sorry for my cluelessness
and say this is your cache:
{
[{ orgId }, 'todos', 'list']: ...all todos,
[{ orgId }, 'todos', 'detail', { id: 1 }]: ...todo1,
[{ orgId }, 'todos', 'detail', { id: 2 }]: ...todo2,
}
you have one todo list, where all your todos are in, and two detail entries.
Now you can:
- target all 3 with:
[{ orgId }, 'todos'](that's the ALL key) - target just the list with
[{ orgId }, 'todos', 'list'] - target both details with
[{ orgId }, 'todos', 'detail'] - target one detail by id with
[{ orgId }, 'todos', 'detail', { id }]
ah ha,
so we in advance, notate our keys explicitly
I thought that "telling the story" of the data with just the params is good enough
interesting
it's about forming a hierarchy, depending on how you want to target them with query invalidation / refetches
I see