-2

Is it possible to update multiple caches with a single query?

For example, I want to add an expense to an expense array, the following query updates both the DB and the cache so optimistic updates are working as expected:

addExpense: build.mutation<Expense[], {price: number | null, category: string, subCategory: string, dates: {date:string, id:string}[] | undefined}>({
            query: ({...expense}) => ({
                url: 'expense/expenses/',
                method: 'POST',
                body: expense
            }),
            async onQueryStarted({...expense}, {dispatch, queryFulfilled}) {
                const addExpense = dispatch(
                    api.util.updateQueryData(
                        'getExpenses',
                        undefined,
                        (
                            draft: Array<Omit<Expense, "userId"> & {id: string}>
                        ) => {
                            if (expense) {
                                expense.dates?.forEach((date) => {
                                    draft.push(
                                        {
                                            id: date.id,
                                            category: expense.category,
                                            subCategory: expense.subCategory,
                                            date: date.date,
                                            price: expense.price ?? 0,
                                        }
                                    )
                                })
                            }
                        },
                    )
                );
                try {
                    await queryFulfilled
                } catch (error) {
                    addExpense.undo();
                }
            }
        }),

But what if this query also affects data elsewhere? For example, I also have a category model and it has an expense array inside it so any expense added to the expense array must also be added to the category array.

In hindsight, I didn't design my data models in the best way and I'll definitely keep this in mind for future projects but for now the question is

Is it possible to update multiple caches with a single query?

Or will I have to write a separate query to update the category cache and ONLY the category cache because my API already handles adding the expense to the category in the call above.

To clarify, I have another data model for Categories that has an expense array as one of its props. Every time I add an expense, I'd also like to add that same expense to the corresponding category.

There's no point adding another endpoint because addExpense will take care of adding the expenses to the expense collection as well as the Category collection in my mongoDB.

What I'm asking is, is it possible to update multiple caches within one onQueryStarted function.

Can I edit or modify another endpoints cache via addExpense?

Mathew
  • 318
  • 11
  • *What* is this other "thing" that you need updated as well? If it's another endpoint you could apply another optimistic update using `updateQueryData`. Can you [edit] and clarify what you are trying to accomplish in a bit more detail? – Drew Reese Aug 08 '23 at 17:13
  • Yea definitely, sorry about that – Mathew Aug 09 '23 at 11:36

0 Answers0