naystack - v1.9.9
    Preparing search index...

    Function useAuthMutation

    • Hook to run a GraphQL mutation with the current user's access token (sent as a Bearer header by the link chain, which also refreshes an expired token and retries once). Returns a function you call with the mutation input.

      The input is sent as variables.input to the GraphQL endpoint.

      Parameters

      • mutation: TypedDocumentNode<T, V>

        A TypedDocumentNode for the mutation (e.g. from codegen or a gql template).

      • Optionaloptions: MutationHookOptions<T, V, DefaultContext, ApolloCache<any>>

        Optional Apollo MutationHookOptions (e.g. onCompleted, onError, refetchQueries).

      Returns readonly [(input?: V["input"]) => Promise<FetchResult<T>>, { hasAuth: false }]

      Tuple: [mutate, result].

      • mutate(input) — runs the mutation with the given input (wrapped as variables.input). Returns a Promise with { data }.
      • result — { data, loading, error, hasAuth } from Apollo. hasAuth is true when an auth token is available.
      import { useAuthMutation } from "naystack/graphql/client";
      import { CREATE_DEAL } from "@/lib/gql/mutations";

      function CreateDealModal({ propertyId }: { propertyId: number }) {
      const [createDeal, { loading }] = useAuthMutation(CREATE_DEAL);

      const onSubmit = async (values: FormFields) => {
      const response = await createDeal({
      propertyId,
      share: Number(values.share),
      targetProfit: Number(values.targetProfit),
      });
      const dealId = response.data?.createDeal;
      if (dealId) router.push(`/deals/${dealId}`);
      };

      return <form onSubmit={handleSubmit(onSubmit)}>...</form>;
      }