T
TanStack•3y ago
optimistic-gold

set default `exact` option

Hey! for most of my use-cases, I wanna do
refetchQueries({ queryKey: [ /* ... */ ], exact: true });
refetchQueries({ queryKey: [ /* ... */ ], exact: true });
is there a way for me to set exact: true by default across the board? instead of setting separately? couldn't find a way to do it by myself
10 Replies
reduced-jade
reduced-jade•3y ago
nope. How do your queryKeys look like? A little restructure might help
optimistic-gold
optimistic-goldOP•3y ago
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])
}
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 });
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 })
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 😰
reduced-jade
reduced-jade•3y ago
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])
}
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()
queryKey: todoKeys.list()
and then no "exact" is needed
optimistic-gold
optimistic-goldOP•3y ago
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,
}
{
[{ 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
reduced-jade
reduced-jade•3y ago
none of them would match 🙃 let's take a step back
optimistic-gold
optimistic-goldOP•3y ago
updated the filter 🥴 typo
reduced-jade
reduced-jade•3y ago
and say this is your cache:
{
[{ orgId }, 'todos', 'list']: ...all todos,
[{ orgId }, 'todos', 'detail', { id: 1 }]: ...todo1,
[{ orgId }, 'todos', 'detail', { id: 2 }]: ...todo2,
}
{
[{ 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 }]
optimistic-gold
optimistic-goldOP•3y ago
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
reduced-jade
reduced-jade•3y ago
it's about forming a hierarchy, depending on how you want to target them with query invalidation / refetches
optimistic-gold
optimistic-goldOP•3y ago
I see thanks a lot @TkDodo 🔮 !

Did you find this page helpful?