I am just curious to know the way to populate extra
and extraOptions
, and from where the arguments must be passed in order so that they can get populated and can be used then in creating some condition based scenario. Well queryFn
itself is used for creating custom query function in the Redux-Toolkit (RTK) query (RTKQ).
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
const shiprocketApiSlice = createApi({
baseQuery: fetchBaseQuery({
baseUrl: "https://apiv2.shiprocket.in/v1/external",
prepareHeaders: (headers, { getState, extra, type, endpoint }) => {
const token =
"...someToken";
token && headers.set("authorization", `Bearer ${token}`);
console.log(
"the extra value of the ====> ",
extra,
getState(),
type,
endpoint,
headers
);
return headers;
},
}),
endpoints: (builder) => ({
updateUser: builder.mutation({
query: (user) => {
return {
url: `/users/${user.queryArg}`,
headers: {
"X-Scope": "1234567890",
},
};
},
}),
createOrderShiprocket: builder.mutation({
queryFn: async (args, api, extraOptions, baseQuery) => {
console.log(
"printin all the agrs of the qrfn to see what exactly it is ===> ",
args,
api,
"extraoptions==>",
extraOptions,
"base==>",
baseQuery
);
const response = await baseQuery(args, api, extraOptions);
console.log("the response ==>", response);
return { error: { message: "error occured" } };
},
}),
}),
});
export const { useCreateOrderShiprocketMutation, useUpdateUserMutation } =
shiprocketApiSlice;
I have used the hooks in this file
"use client";
import React from "react";
import {
useCreateOrderShiprocketMutation,
useUpdateUserMutation,
} from "@/store/features/shiprocket/shiprocketApiSlice";
const TestFolderPage = () => {
const [createOrderShiprocket, { data: TheOrderList }] =
useCreateOrderShiprocketMutation();
const [updateUser, { data: updatedData }] = useUpdateUserMutation();
const handleClick = () => {
console.log("the click");
updateUser({ queryArg: 12344 });
createOrderShiprocket({ userId: 123 });
};
return <div onClick={handleClick}>TestFolderPage</div>;
};
export default TestFolderPage;
2
Answers
Modifying
fetchBaseQuery
to Useextra
:Currently, the
extra
parameter inprepareHeaders
isn’t being utilized because it’s not set anywhere. If you want to use this, you need to provide a value forextra
when callingfetchBaseQuery
. However, typicallyextra
is used for more advanced scenarios, like when you have a middleware setting it.Passing and Using
extraOptions
inqueryFn
:To demonstrate how to pass and use
extraOptions
, I’ll modify thecreateOrderShiprocket
mutation’s usage in your component.And then:
extra
Provided as
thunk.extraArgument
to the configureStore getDefaultMiddleware option.BaseQueryApi
extra
is the extra argument used with Redux-Toolkit’s Thunk middleware when configuring the Redux store. See Customizing the included Middleware for details. The basic example is a follows:This
extraArgument
is exposed out in thethunkApi
ofcreateAsyncThunk
actions, and apparently as part of the Redux-Toolkit Query’sprepareHeaders
function signature.extraOptions
See
extraOptions
in the docs:Quite simply,
extraOptions
is an optional object that is passed as the third argument to the suppliedbaseQuery
function, e.g. the base query function returned byfetchBaseQuery
in your code example. SeefetchBaseQuery
signature.I’ve already covered above where and how to specify/add the
extra
value when configuring the store. TheextraOptions
can specified per endpoint.Example:
This alone isn’t particularly useful until you have the need to customize your base query function. See Customizing Queries.
For example, a fairly trivial customization is to add automatic retries for failed queries.
It’s really up to you and your app’s specific use cases and needs what
extra
andextraOptions
are relevant and/or helpful.