You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to determine baseUrl when make the actual api call.
baseUrl is just for example, in some cases, you may want to make the final decision when calling api, instead of at api definition time.
by reading #1335, I know we can use extra field, something like:
// customize a base queryexportconstmyBaseQuery=(args,{ dispatch, getState, extra },extraOptions,)=>{// determine baseUrl by `region`, which passed in via `extra`// we can also read config information from store via getState.constbaseUrl=getBaseUrlByRegion(extra.region);// do more things with baseUrl};// declare myApi and endpointsexportconstmyApi=createApi({reducerPath: 'myApi',baseQuery: myBaseQuery,endpoints: builder=>({// getPostById only cares about `id`,// so I do not want to pass `region` as additional args here. getPostById: builder.query<any,number>({query: id=>({url: `posts/${id}`,method: 'GET',}),}),}),});
Question
Its ok to define apis as above, but I cannot find a way to set the extra when use query hooks, below code is what I want, but does not work, any suggestion is appreciated!
// while inside a component// want to set `extra` when useQuery/useLazyQuery/useMutation,// so any further calls to `getPostById` will contains the `region` information,// but those hooks does not accept `extra` args.const[getPostById,{ data }]=myApi.endpoints.getPostById.useLazyQuery({region: 'us'},// does not work here);// expected clean callconstpostInUsRegion=getPostById(12);
More
We do able to do as below, but we need to add region to all my endpoints, even though those endpoints does not care about the parameter.
const[getPostById,{ data }]=myApi.endpoints.getPostById.useLazyQuery()// unexpected, because parameter `region` is not part of the api.constpostInSgRegion=getPostById({postId: 12,region: 'sg'})
Also tried dispatch, still not able to pass extra in.
Background
I'd like to determine
baseUrl
when make the actual api call.by reading #1335, I know we can use
extra
field, something like:Question
Its ok to define apis as above, but I cannot find a way to set the
extra
when use query hooks,below code is what I want, but does not work, any suggestion is appreciated!
More
region
to all my endpoints, even though those endpoints does not care about the parameter.dispatch
, still not able to passextra
in.extra
at middleware level globally, not per hook call.The text was updated successfully, but these errors were encountered: