Search code examples
reactjsrtk-query

Is it possible to updateQueryData on a new nonexistent query - RTK Query?


I've an api created with RTK Query, and I have a create endpoint that pessimistically updates other queries:

create: builder.mutation<
      { id: string },
      Req
    >({
      query: (req) => ({
        url: "",
        method: "POST",
        body: req,
      }),
      async onQueryStarted(req, { dispatch, queryFulfilled }) {
        try {
          const {
            data: { id },
          } = await queryFulfilled;


          dispatch(
            apiSvc.util.updateQueryData(
              "getFoos",
              { specialId: req.specialId },
              (draft) => {
                draft.unshift({
                  ...req,
                  id
                });
              }
            )
          );
          dispatch(
            apiSvc.util.updateQueryData(
              "getSingleFoo",
              { specialId: req.specialId, otherSpecialId: req.otherSpecialId },
              (draft) => {
                Object.assign(draft, {
                  ...req,
                  id
                });
              }
            )
          );
        } catch (e) {
          console.error(e);
        }
      },
    }),

But updating the getSingleFoo doesn't update the cache value correctly, only getFoos is updated.

Is it possible to do this? If so how?


Solution

  • If anyone wonders here, I asked in the repo.