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

Question: How can I send parameters to the mutations ? #96

Closed
lavocatt opened this issue Apr 29, 2024 · 3 comments
Closed

Question: How can I send parameters to the mutations ? #96

lavocatt opened this issue Apr 29, 2024 · 3 comments
Labels
question Further information is requested

Comments

@lavocatt
Copy link

The generated code creates a mutation for me:

export const useSecurityServiceLogin = (
  options?: Omit<
    UseMutationOptions<
      Awaited<ReturnType<typeof SecurityService.login>>,
      unknown,
      {
        requestBody: {
          brokerName: string;
          userName: string;
          password: string;
          jolokiaHost: string;
          scheme: string;
          port: string;
        };
      },
      unknown
    >,
    'mutationFn'
  >,
) =>
  useMutation(({ requestBody }) => SecurityService.login(requestBody), options);

However, I don't know how to use it, because if in the code I'm doing something like:

  const { data: dataToken, isSucces: tokenSuccess } = useSecurityServiceLogin({
    options: {
      brokerName: getBrokerKey(broker, ordinal),
      userName: userName,
      password: password,
      jolokiaHost: jolokiaHost,
      port: jolokiaPort,
      scheme: protocol,
    },
  });

I'm getting errors from the linter

image

I can make it work like this:

  const { data, isSuccess, isLoading } = useQuery({
    queryKey: ['useSecurityServiceLogin'],
    queryFn: () => {
      return SecurityService.login({
        brokerName: getBrokerKey(broker, ordinal),
        userName: userName,
        password: password,
        jolokiaHost: jolokiaHost,
        port: jolokiaPort,
        scheme: protocol,
      });
    },
    enabled: fireRequest,
    onSuccess: () => {
      const receivedToken = isSuccess ? data['jolokia-session-id'] : '';
      sessionStorage.setItem(getBrokerKey(broker, ordinal), receivedToken);
    },
  });

But I would prefer to use the mutation hook. Any idea on what I'm doing wrong?

Thanks

@seriouslag
Copy link
Collaborator

seriouslag commented Apr 29, 2024

Typescript is reporting there are no 'options' property.

I recommend this vscode extension to help you understand typescript errors.
https://marketplace.visualstudio.com/items?itemName=yoavbls.pretty-ts-errors

useSecurityServiceLogin is a mutation, not a query; your example shows you calling it like a query.
Mutations are not like queries.

Check out this guide to mutations: https://tanstack.com/query/latest/docs/framework/react/guides/mutations

Right now, only GET requests are generated as queries in this library.
POST, PUT, DELETE are all generated as mutations.

I have a PR open that expands the documentation on using mutations. #97

Your component should look something like the following:

const LoginButton = ({
  userName,
  password,
  jolokiaHost,
  jolokiaPort,
  protocol,
  broker,
  ordinal,
}: LoginProps) => {
  const { mutate: loginMutation, isSuccess, isError, isPending } = useSecurityServiceLogin({
    onSuccess: (value) => {
      console.log('Login result', value);
    },
    onError: (error) => {
      console.error('Failed to login', error);
    },
  });
  
  const login = async () => {
    console.log('Attempting to login...');
    await loginMutation({
      brokerName: getBrokerKey(broker, ordinal),
      userName,
      password,
      jolokiaHost,
      port: jolokiaPort,
      scheme: protocol,
    });
  };

  const buttonText = isPending ? 'Logging in...' : 'Login';

  return (<button disabled={pending} onClick={() => login()}>{buttonText}</button>);
}

@seriouslag seriouslag added the question Further information is requested label Apr 30, 2024
@lavocatt
Copy link
Author

Thanks a lot, I'm going to give it a try.

@lavocatt
Copy link
Author

I'm closing the issue as your answer resolved my question. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants