Skip to content

Honor all call options in callUnaryMethod #426

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

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/connect-query/src/call-unary-method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { CallOptions, Transport } from "@connectrpc/connect";
import type { MethodUnaryDescriptor } from "./method-unary-descriptor.js";

/**
* Call a unary method given it's signature and input.
* Call a unary method given its signature and input.
*/
export async function callUnaryMethod<
I extends Message<I>,
Expand All @@ -41,6 +41,9 @@ export async function callUnaryMethod<
callOptions?.timeoutMs,
callOptions?.headers,
input ?? {},
callOptions?.contextValues,
);
callOptions?.onHeader?.(result.header);
callOptions?.onTrailer?.(result.trailer);
return result.message;
}
17 changes: 6 additions & 11 deletions packages/connect-query/src/use-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type {
import { useMutation as tsUseMutation } from "@tanstack/react-query";
import { useCallback } from "react";

import { callUnaryMethod } from "./call-unary-method.js";
import type { MethodUnaryDescriptor } from "./method-unary-descriptor.js";
import { useTransport } from "./use-transport.js";

Expand Down Expand Up @@ -58,17 +59,11 @@ export function useMutation<
const transportFromCtx = useTransport();
const transportToUse = transport ?? transportFromCtx;
const mutationFn = useCallback(
async (input: PartialMessage<I>) => {
const result = await transportToUse.unary(
{ typeName: methodSig.service.typeName, methods: {} },
methodSig,
callOptions?.signal,
callOptions?.timeoutMs,
callOptions?.headers,
input,
);
return result.message;
},
async (input: PartialMessage<I>) =>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callUnaryMethod is used in other hooks. We can use it here as well.

callUnaryMethod(methodSig, input, {
transport: transportToUse,
callOptions,
}),
[transportToUse, callOptions, methodSig],
);
return tsUseMutation({
Expand Down