I am trying to call updateQueryData() on mutation in redux-toolkit api but somehow it is not calling. I see args is printing fine when I try to update the data, it just prints args outside updateQueryData but data is never coming inside updateQueryData
Here is my code:
export const additionalInputsApi = createApi({
reducerPath: 'additionalInputsApi',
baseQuery: getBaseQuery(),
keepUnusedDataFor: 90,
endpoints: builder => ({
getAdditionalInputData: builder.query({
query: ({ scenarioType, sectorType, ccaVersion, companyId }) => ({
url: endpoints.GET_ADDITIONAL_INPUT,
params: {
ccaVersion,
scenarioType,
sectorType,
...(companyId && { companyId })
}
}),
}),
updateAdditionalInputs: builder.mutation({
query: data => utils.serviceLayerAPI.reduxPost(endpoints.SAVE_ADDITIONAL_INPUTS, data),
async onQueryStarted(args, { dispatch, queryFulfilled }) {
try {
const { data: updatedPost } = await queryFulfilled
dispatch(
additionalInputsApi.util.updateQueryData('getAdditionalInputData', args.key, (draft) => {
console.log('draft', draft); // Not printing anything
})
)
} catch { }
},
})
})
});
export const {
useGetAdditionalInputDataQuery,
useUpdateAdditionalInputsMutation,
} = additionalInputsApi;
Here is my store
const sagaMiddleware = createSagaMiddleware();
const middlewares = [
sagaMiddleware,
additionalInputsApi.middleware
];
const store = configureStore({
reducer: {
additionalInputs: additionalInputsReducer,
[additionalInputsApi.reducerPath]: additionalInputsApi.reducer
},
middleware: getDefaultMiddleware => getDefaultMiddleware({
serializableCheck: false
}).concat(middlewares)
});
globalStore.RegisterStore('cca', store);
export default store;
export type RootState = ReturnType<typeof store.getState>;
Can somebody look at my code and tell what I am missing.
If it doesn't print anything on dispatch, you don't have a cache entry for the getAdditionalInputData
endpoint with the argument args.key
.
That seems likely, since your getAdditionalInputData
endpoint takes an argument in the form { scenarioType, sectorType, ccaVersion, companyId }
and I assume that args.key
is probably very different from that object.