how to do an optimistic update with injectEndpoints

Solution 1:

There is absolutely no difference between using createApi or injectEndpoints here, in both cases it is part of the builder.mutation call:


export const heartApi = api.injectEndpoints({
    endpoints: (builder) => ({
        createHeart: builder.mutation({
            query: (id: string) => ({ url: '/hearts', method: 'POST', body: { id } }),
            onQueryStarted(arg, {queryFulfilled}){
              // it would go here
            }
        }),
    }),
});