David AlonsoD
Convex Communityโ€ข17mo agoโ€ข
9 replies
David Alonso

`withOptimisticUpdate` with custom (auth) mutations

I have the following custom mutation:
export function useAuthMutation<Mutation extends AuthFunction<"mutation">>(
  name: Mutation
) {
  const organization = useOrganization();
  const clerkOrgId = organization?.organization?.id;
  const originalMutation = useMutation(name);

  return useCallback(
    async (
      ...args: AuthArgsArray<Mutation>
    ): Promise<FunctionReturnType<Mutation>> => {
      const newArgs = {
        ...(args[0] ?? {}),
        clerkOrgId,
      } as FunctionArgs<Mutation>;

      return originalMutation(...([newArgs] as OptionalRestArgs<Mutation>));
    },
    [clerkOrgId, originalMutation]
  );
}


which automatically injects the clerkOrgId into the args. I'm unfortunately not able to do useAuthMutation().withOptimisticUpdate().

Can anyone who has worked on convex-helpers offer suggestions? ๐Ÿ™
Was this page helpful?