Skip to content

Calling await on an unwrapped lazy query trigger does not actually wait for the request #3662

Closed

Description

Redux toolkit version

"@reduxjs/toolkit": "^1.9.3",

I tried looking at existing issues for a similar problem but I did not find anything. I found #3515 but that seems to be more stylistic in nature. What I'm experiencing is either a bug or I am misunderstanding the documentation.

I'm trying to manually trigger a query to the server (i.e. don't use any cached data). This seems like a great use case for useLazyQuery. It looks like I should be able to call .unwrap() after calling the trigger and receive the data or have the exception thrown once the promise is resolved or rejected. However that's not what is happening for me. Interestingly, this same pattern works fine for mutations.

I have some code like the following:

export const AuthProvider = ({ children }: { children: ReactNode; }) => {
  const [triggerUserSignin] = usePostUserSigninMutation();
  const [getUserLazy] = useLazyGetUserQuery();

  const login = async (...) => {
    try {
      const signin = await triggerUserSignin(...).unwrap();
      console.log(signin);
      const userTriggerResult = await getUserLazy().unwrap();
      console.log(userTriggerResult);
      const user = await getUser();
      console.log(user);

      ...

    } catch (err) {
      ...
    }
  };
  
};

export const getUser = (): Promise<AxiosResponse<User>> => {
  return axios({
    method: 'GET',
    url: `/user`,
    responseType: 'json',
  });
};

What I'm noticing is that userTriggerResult is undefined when I print it and user is a resolved user. useLazyGetUserQuery and getUser are hitting the same endpoint - I copied getUser to demonstrate that it's just an axios query and that works fine. For now I am getting around this issue by using a raw axios query but I'd like to keep everything uniform. And like I said the mutation works like a charm - in the above snippet the console correctly prints out the signin response.

Am I misunderstanding or mis-using something? If I can provide any more information please just let me know what to provide.

Thanks in advance!

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions