Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to add extra arguments when using custom query #1335

Closed
ideffix opened this issue Jul 21, 2021 · 8 comments
Closed

Possibility to add extra arguments when using custom query #1335

ideffix opened this issue Jul 21, 2021 · 8 comments

Comments

@ideffix
Copy link

ideffix commented Jul 21, 2021

I've been playing with RTK Query for a few days and I don't know if I'm missing something or just missunderstood the concept. In my project Im using custom query. I would like to have a possibility to pass extra arguments from place where my hook is being used. I think it would be great to have something like this:

const customBaseQuery = (
  args,
  { signal, dispatch, getState },
  extraOptions,
  hookOptions // <-- new param
) => {
  if (hookOptions.someFlag) {
    doSomeStuff()
  }
  if (Math.random() > 0.5) return { error: 'Too high!' }
  return { data: 'All good!' }
}

And then when using hook created by createSlice:

useMyApiQuery(myArgs, queryOptions, {someFlag: true})

or

useMyApiQuery(myArgs, queryOptions, {someFlag: false})

I would like to decide about my flag value in a place where Im using my hook.

I know I can just add it as another field in myArgs object but then Ill need to type it in every place where build.mutation/build.query is used.

@JulienKode
Copy link
Contributor

JulienKode commented Oct 25, 2021

I face the same issue I think it could be nice to retrieve what we injected at this level

const customBaseQuery = (
  args,
  { signal, dispatch, getState, extraArgs },
  extraOptions
) => {

}

@phryneas
Copy link
Member

You can pass in things there by making it part of the args.

Wanting to pass things in there and not making it part of args is like omitting something from a useEffect dependency array:
something you should never do as it will always run into some synchronization problems. (when to run a new query and when not to do so when something in there changes is completely undefined)

You have an escape hatch to store stuff into state and access that from the baseQuery, but even with that you should be careful.

@JulienKode
Copy link
Contributor

JulienKode commented Oct 25, 2021

Thanks for your quick reply and all of theses informations @phryneas

But even if it's a class that I've put in the extraArgs of redux thunk, does this still must be pass as args ?

The only way I currently see to retrieve it without having to pass them as args is by using the return of a dispatch

const customBaseQuery = (
  args,
  { signal, dispatch, getState },
  extraOptions
) => {
   const myClassInstance = await dispatch(({extraArgs}) => extraArgs.myClassInstanceThatWasInjected)
}

This might be a bad practice or seems to be really hacky no ?

@phryneas
Copy link
Member

That is perfectly reasonable, although I think you might be able to directly access extra as part of the BaseQueryApi.

@JulienKode
Copy link
Contributor

I ended up trying this because this parameters was not mention in the docs https://redux-toolkit.js.org/rtk-query/usage/customizing-queries#basequery-function-arguments

After trying to get it, it seems missing
Screenshot 2021-10-25 at 16 08 43

@JulienKode
Copy link
Contributor

JulienKode commented Oct 25, 2021

This seems fixed by #1378 I just need to update this library

Maybe we can add the info with #1649

To clarify the situation we can get the extraArgs with

const customBaseQuery = (
  args,
  { signal, dispatch, getState, extra },
  extraOptions
) => {
   const myClassInstance = extra.myClassInstanceThatWasInjected
}

@hichemfantar
Copy link

So should this be closed?
Apparently It's fixed according to the previous comment.

@markerikson
Copy link
Collaborator

I think so, yeah. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants