0

I need to update all formulas in cache that use the same raw ingredient after it has been changed using tanstack query. In Tanstack Query I newbie

My implementation but this code does not always work correctly. It updates once, it doesn't update the next time.

const { data: formulas } = useGetFormulas({
    queryParams: getParamsString(),
});

updateRelation(
                {
                    id,
                    ingredientId: `${subIngredient.id}`,
                    relationType: "subIngredients",
                    body: { value: weight_percent },
                },
                {
                    onSuccess() {
                        queryClient.refetchQueries({
                            queryKey: [queryKeys.rawIngredient, id],
                        });
                        queryClient.refetchQueries({
                            queryKey: [queryKeys.rawIngredients],
                        })
                        queryClient.refetchQueries({
                            queryKey: [queryKeys.formulas],
                        })
                        queryClient.refetchQueries({
                            queryKey: [queryKeys.lastUpdateFormules],
                        })
                        if (formulas) {
                            const formulaIds = formulas.rows
                                .filter((formula: IFormulaItem) => formula.ri_ids.includes(+id))
                                .map((formula: IFormulaItem) => String(formula.id));
                        
                            console.log({ formulaIds });
                        
                            formulaIds.forEach((formulaId) => {
                                queryClient.refetchQueries({
                                    queryKey: [queryKeys.formula, formulaId],
                                });
                            });
                        }
                    },
                    onError() {
                        toast.error(toastTexts.error, defaultErrorToastOptions);
                    },
                },
            );

This part of the code does not want to work correctly

if (formulas) {
   const formulaIds = formulas.rows
      .filter((formula: IFormulaItem) => formula.ri_ids.includes(+id))
      .map((formula: IFormulaItem) => String(formula.id));
                        
   formulaIds.forEach((formulaId) => {
      queryClient.refetchQueries({
      queryKey: [queryKeys.formula, formulaId],
      });
   })
}

Tell me how to solve this problem pleas

4
  • What exactly is being changed? Is it changed via a mutation? You should probably be looking to invalidate queries (so they refetch) instead of manually triggering them yourself. Please edit to clarify in better detail what exactly isn't working as expected and include a complete minimal reproducible example of the relevant code you are working with. Commented Mar 25 at 5:06
  • When I change a certain ingredient, its status changes. Based on this status, the status of formulas that use this ingredient changes. When I get detailed information about the formula, I see the ingredients used in it. When I change an ingredient, I need to update the data in the cache for each formula that uses the ingredient. In the above code, the data is updated once, and then only one formula may be updated, or it may not update the data at all. Commented Mar 25 at 8:21
  • if (formulas) { const formulaIds = formulas.rows .filter((formula: IFormulaItem) => formula.ri_ids.includes(+id)) .map((formula: IFormulaItem) => String(formula.id)); formulaIds.forEach((formulaId) => { queryClient.refetchQueries({ queryKey: [queryKeys.formula, formulaId], }); }) } This code updates the data for each formula, but once it updates the data, it will not update it a second time Commented Mar 25 at 8:22
  • Please edit your post to include any new/relevant details. Commented Mar 26 at 0:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.