0

Hello I'm trying to manualy update Posts in caching when creating now post that I don't want to use invalidatesTags: ['Posts'] because it make many lots of requests but when using updateQueryData to update my state doesn't work even console.log('Does it work ?') not printed ,I Suppose dispatch is not excuted ,after searching I founded that id must be string so I added id.toString() but also didn't work ,How can I fix it

        createPost: builder.mutation({
            query: (data) => ({
                url: '/api/post/new',
                method: 'POST',
                body: data,
            }),
            // invalidatesTags: ['Posts'],
            async onQueryStarted({ id }, { queryFulfilled, dispatch }) {
                try {
    
                    const { data } = await queryFulfilled;
                    console.log(data)
                    // debugger
                    const result = dispatch(
                        apiSlice.util.updateQueryData("getFollowersPosts", id, (draft) => {
                            console.log('Does it work ?') //Not Printed, why ?
                            draft?.push(data)
                        })
                    )
                    console.log(result)
                } catch (err) {
                    console.log(err)
                }
            },
        }),

Here is endpoint for get posts

import { apiSlice } from '../ApiSlice';
export const PostsApi = apiSlice.injectEndpoints({
    endpoints: (builder) => ({
        getPosts: builder.query({
            query: () => ({
                url: '/api/post/get',
                method: 'GET',
            }),
        }),
    }),
});

0 Answers0