onQueryStarted -> updateQueryData: pass query parameter #2728
-
Hello guys! I have a simple mutation: const [updateTodo, result] = usePatchTodosByTodoIdMutation();
const update = (value: boolean) => {
updateTodo({todoId: id!, body: {completed: value}});
} And I want to implement optimistic updates, so I add async onQueryStarted(arg, {dispatch, queryFulfilled}) {
const patchResult = dispatch(
// Notice {_page: 1}? If I hardcode {_page: 1} and my updated todo item on page 1 – then it works.
// otherwise it's not.
todosApiSlice.util.updateQueryData('getTodos', {_page: 1}, (draft) => {
if (draft) {
todosAdapter.updateOne(draft, {
id: arg.todoId,
changes: {completed: arg.body.completed, title: arg.body.title, description: arg.body.description},
});
}
})
)
try {
await queryFulfilled;
} catch (_) {
dispatch(patchResult.undo);
} But my getTodos: build.query<GetTodosApiResponse, GetTodosApiArg>({
query: (queryArg) => ({
url: `/todos`,
params: {_page: queryArg._page},
}),
}), So my question is – Is there a way to pass additional parameter to my mutation query? I want to be able to pass Please note that I'm using TypeScript and my api is partially generated by Thanks a lot! ❤️ |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
That shifts the answer from a "yes" to a "no", I'm sorry. But you could use |
Beta Was this translation helpful? Give feedback.
That shifts the answer from a "yes" to a "no", I'm sorry. But you could use
api.utils.selectInvalidatedBy
to get all cache entries that contain your resource and update them accordingly.