Skip to content

Commit b019df3

Browse files
thomasballingerConvex, Inc.
authored and
Convex, Inc.
committed
Actions and queries accept both styles for args and returns (#27726)
Allow both styles of validator syntax (`v.whatever()` and `{ a: v.whatever() }`) in queries and actions, as well as mutations where both styles already worked for both `args` and `returns`. GitOrigin-RevId: 37677c68709d1223bea1867e78a9c40c645b1f46
1 parent ed70824 commit b019df3

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

npm-packages/convex/src/server/registration.test.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import { test, describe, expect } from "vitest";
22
import { assert, Equals } from "../test/type_testing.js";
33
import { v } from "../values/validator.js";
44
import {
5+
ActionBuilder,
56
ApiFromModules,
67
DefaultFunctionArgs,
8+
QueryBuilder,
9+
actionGeneric,
710
mutationGeneric,
11+
queryGeneric,
812
} from "./index.js";
913
import { EmptyObject, MutationBuilder } from "./registration.js";
1014

@@ -276,8 +280,10 @@ describe("argument inference", () => {
276280
});
277281

278282
describe("argument and return value validators can be objects or validators", () => {
279-
// Test with mutation, but all the wrappers work the same way.
283+
// Test with mutation, we aim for all the wrappers work the same way.
280284
const mutation: MutationBuilder<any, "public"> = mutationGeneric;
285+
const query: QueryBuilder<any, "public"> = queryGeneric;
286+
const action: ActionBuilder<any, "public"> = actionGeneric;
281287

282288
const module = {
283289
configArgsObject: mutation({
@@ -314,6 +320,27 @@ describe("argument and return value validators can be objects or validators", ()
314320
return { arg: "result" };
315321
},
316322
}),
323+
324+
// test queries and actions just a bit too
325+
q1: query({
326+
args: v.object({
327+
arg: v.string(),
328+
}),
329+
returns: { arg: v.string() },
330+
handler: (_, { arg }) => {
331+
return { arg: arg };
332+
},
333+
}),
334+
335+
a1: action({
336+
args: v.object({
337+
arg: v.string(),
338+
}),
339+
returns: { arg: v.string() },
340+
handler: (_, { arg }) => {
341+
return { arg: arg };
342+
},
343+
}),
317344
};
318345
type API = ApiFromModules<{ module: typeof module }>;
319346

@@ -372,4 +399,20 @@ describe("argument and return value validators can be objects or validators", ()
372399
const returnString = module.configOutputValidator.exportReturns();
373400
expect(JSON.parse(returnString)).toEqual(expectedReturnsExport);
374401
});
402+
403+
test("queries", () => {
404+
type ReturnType = API["module"]["q1"]["_returnType"];
405+
type Expected = { arg: string };
406+
assert<Equals<ReturnType, Expected>>;
407+
const returnString = module.configOutputValidator.exportReturns();
408+
expect(JSON.parse(returnString)).toEqual(expectedReturnsExport);
409+
});
410+
411+
test("actions", () => {
412+
type ReturnType = API["module"]["configOutputValidator"]["_returnType"];
413+
type Expected = { arg: string };
414+
assert<Equals<ReturnType, Expected>>;
415+
const returnString = module.configOutputValidator.exportReturns();
416+
expect(JSON.parse(returnString)).toEqual(expectedReturnsExport);
417+
});
375418
});

npm-packages/convex/src/server/registration.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,11 @@ export type QueryBuilder<
643643
Visibility extends FunctionVisibility,
644644
> = {
645645
<
646-
ArgsValidator extends PropertyValidators | void,
647-
ReturnsValidator extends Validator<any, any, any> | void,
646+
ArgsValidator extends PropertyValidators | Validator<any, any, any> | void,
647+
ReturnsValidator extends
648+
| PropertyValidators
649+
| Validator<any, any, any>
650+
| void,
648651
ReturnValue extends ReturnValueForOptionalValidator<ReturnsValidator> = any,
649652
OneOrZeroArgs extends
650653
ArgsArrayForOptionalValidator<ArgsValidator> = DefaultArgsForOptionalValidator<ArgsValidator>,
@@ -726,8 +729,11 @@ export type ActionBuilder<
726729
Visibility extends FunctionVisibility,
727730
> = {
728731
<
729-
ArgsValidator extends PropertyValidators | void,
730-
ReturnsValidator extends Validator<any, any, any> | void,
732+
ArgsValidator extends PropertyValidators | Validator<any, any, any> | void,
733+
ReturnsValidator extends
734+
| PropertyValidators
735+
| Validator<any, any, any>
736+
| void,
731737
ReturnValue extends ReturnValueForOptionalValidator<ReturnsValidator> = any,
732738
OneOrZeroArgs extends
733739
ArgsArrayForOptionalValidator<ArgsValidator> = DefaultArgsForOptionalValidator<ArgsValidator>,

0 commit comments

Comments
 (0)