diff --git a/npm-packages/convex/src/browser/query_options.ts b/npm-packages/convex/src/browser/query_options.ts index 7ac46041e..621910a97 100644 --- a/npm-packages/convex/src/browser/query_options.ts +++ b/npm-packages/convex/src/browser/query_options.ts @@ -22,6 +22,6 @@ export type QueryOptions> = { // This helper helps more once we have more inference happening. export function convexQueryOptions>( options: QueryOptions, -) { +): QueryOptions { return options; } diff --git a/npm-packages/convex/src/react/client.ts b/npm-packages/convex/src/react/client.ts index 12e8b2b83..b60aa1ef8 100644 --- a/npm-packages/convex/src/react/client.ts +++ b/npm-packages/convex/src/react/client.ts @@ -808,20 +808,26 @@ export type OptionalRestArgsOrSkip> = * * @public */ -export type UseQueryOptions> = - QueryOptions & { - /** - * Whether to throw an error if the query fails. - * If false, the error will be returned in the `error` field. - * @defaultValue false - */ - throwOnError?: boolean; - /** - * An initial value to use before the query result is available. - * @defaultValue undefined - */ - initialValue?: Query["_returnType"]; - }; +export type UseQueryOptions> = ( + | (QueryOptions & { skip?: false }) + | { + query: Query; + args?: unknown; + skip: true; + } +) & { + /** + * Whether to throw an error if the query fails. + * If false, the error will be returned in the `error` field. + * @defaultValue false + */ + throwOnError?: boolean; + /** + * An initial value to use before the query result is available. + * @defaultValue undefined + */ + initialValue?: Query["_returnType"]; +}; /** * Options for the object-based {@link useQuery} overload with a preloaded query. @@ -941,7 +947,9 @@ export function useQuery>( typeof query === "string" ? (makeFunctionReference<"query", any, any>(query) as Query) : query; - argsObject = queryOrOptions.args ?? ({} as Record); + if (!queryOrOptions.skip && queryOrOptions.args) { + argsObject = queryOrOptions.args; + } throwOnError = queryOrOptions.throwOnError ?? false; initialValue = queryOrOptions.initialValue; } diff --git a/npm-packages/convex/src/react/use_query.test.ts b/npm-packages/convex/src/react/use_query.test.ts index 503dee2fb..7caa70fde 100644 --- a/npm-packages/convex/src/react/use_query.test.ts +++ b/npm-packages/convex/src/react/use_query.test.ts @@ -86,6 +86,14 @@ describe("useQuery types", () => { args: { _arg: "asdf" }, }); + const userId = undefined as string | undefined; + + useQuery({ + query: api.module.args, + args: { _arg: userId }, + skip: !userId, + }); + useQuery({ query: api.module.args, args: { _arg: "asdf" },