Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race conditions when using updateQueryData #4271

Open
yankinx opened this issue Mar 13, 2024 · 0 comments
Open

Race conditions when using updateQueryData #4271

yankinx opened this issue Mar 13, 2024 · 0 comments

Comments

@yankinx
Copy link

yankinx commented Mar 13, 2024

A similar issue with "invalidate tags" was fixed in #3116

  1. If request Q has started
  2. Mutation M started and finished, there should be an pessimistic update of the cached data.
  3. Once request Q finishes, it should have an pessimistic update.

If you perform updateQueryData on request Q which is in a pending status, then no changes will be made.

If you perform updateQueryData on request Q which is in a pending status, then after the completion of request Q, no changes will be made.

In my opinion, this is not correct, as it limits pessimistic updates which encourages the use of invalidatesTags, an additional request I do not want to make, because I already have the necessary data in the response from the mutation.

an example of how I try to perform an pessimistic update

const ridesApi = createApi({
  baseQuery,
  endpoints: builder => ({
    getRides: builder.query({
      query: () => 'rides'
    }),
    postRidePrice: builder.mutation({
      query: id => ({
        url: `ride/${id}`,
        method: 'POST'
      }),
      async onQueryStarted(arg, { dispatch, queryFulfilled }) {
        try {
          const { data } = await queryFulfilled;

          /* 
          this is a part of the code that helps to fix the problems described above, but it has a flaw in the expectation
          const res = dispatch(ridesApi.util.getRunningQueryThunk('getRides', undefined));

          if (res) {
            await res;
          }
          */

          dispatch(
            ridesApi.util.updateQueryData('getRides', undefined, rides => {
              const ride = rides.find(ride => ride.id === data.id);

              if (ride) {
                Object.assign(ride, data);
              }
            })
          );
        } catch (e) {
          console.error(e);
        }
      }
    })
  })
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant