Skip to content

Fix types on customFunctions #638

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
49 changes: 49 additions & 0 deletions packages/convex-helpers/server/customFunctions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,34 @@
return { ctxInner: ctx["inner"], ctxOuter: ctx["outer"], ...args };
},
});
// Extend the previous ctx with new args
export const outerExtender = customQuery(inner, {
args: { outer: v.string() },
input: async (ctx, args) => {
return { ctx: { outer: [args.outer, ctx.inner] }, args: {} };

Check failure on line 365 in packages/convex-helpers/server/customFunctions.test.ts

View workflow job for this annotation

GitHub Actions / Test and lint

Property 'inner' does not exist on type 'GenericQueryCtx<{ users: { document: { _id: Id<"users">; _creationTime: number; tokenIdentifier: string; }; fieldPaths: "_id" | ExtractFieldPaths<VObject<{ tokenIdentifier: string; }, { tokenIdentifier: VString<string, "required">; }, "required", "tokenIdentifier">>; indexes: { ...; }; searchIndexes: {}; vectorIndex...'.
},
});
export const outerExtends = outerExtender({
args: { a: v.string() },
handler: async (ctx, args) => {
return { outer: ctx.outer, inner: ctx.inner, ...args };

Check failure on line 371 in packages/convex-helpers/server/customFunctions.test.ts

View workflow job for this annotation

GitHub Actions / Test and lint

Property 'inner' does not exist on type 'Overwrite<GenericQueryCtx<{ users: { document: { _id: Id<"users">; _creationTime: number; tokenIdentifier: string; }; fieldPaths: "_id" | ExtractFieldPaths<VObject<{ tokenIdentifier: string; }, { tokenIdentifier: VString<string, "required">; }, "required", "tokenIdentifier">>; indexes: { ...; }; searchIndexes: {}; v...'.
},
});
// Deleted ctx is correctly undefined on nested functions
export const dbRemover = customQuery(
query,
customCtx(() => ({ db: undefined })),
);
export const outerDBRemover = customQuery(
dbRemover,

Check failure on line 380 in packages/convex-helpers/server/customFunctions.test.ts

View workflow job for this annotation

GitHub Actions / Test and lint

Argument of type 'CustomBuilder<"query", {}, { db: undefined; }, {}, GenericQueryCtx<{ users: { document: { _id: Id<"users">; _creationTime: number; tokenIdentifier: string; }; fieldPaths: "_id" | ExtractFieldPaths<VObject<{ tokenIdentifier: string; }, { ...; }, "required", "tokenIdentifier">>; indexes: { ...; }; searchIndexes: {}; v...' is not assignable to parameter of type 'QueryBuilder<GenericDataModel, "public">'.
customCtx(() => ({ outer: "outer" })),
);
export const outerRemovesDB = outerDBRemover({
args: {},
handler: async (ctx, args) => {
return { db: ctx?.db ? "present" : "missing", outer: ctx.outer, ...args };
},
});

/**
* Test helpers
Expand Down Expand Up @@ -388,6 +416,8 @@
create: typeof create;
outerAdds: typeof outerAdds;
outerRemoves: typeof outerRemoves;
outerExtends: typeof outerExtends;
outerRemovesDB: typeof outerRemovesDB;
};
}>["fns"] = anyApi["customFunctions.test"] as any;

Expand Down Expand Up @@ -568,4 +598,23 @@
t.query(testApi.outerAdds, { a: 3 as any, outer: "" }),
).rejects.toThrow("Validator error: Expected `string`");
});

test("extends prev ctx", async () => {
const t = convexTest(schema, modules);
expect(
await t.query(testApi.outerExtends, { a: "hi", outer: "extended" }),
).toMatchObject({
inner: "inner",
outer: ["extended", "inner"],
a: "hi",
});
});

test("keeps deleted ctx", async () => {
const t = convexTest(schema, modules);
expect(await t.query(testApi.outerRemovesDB, {})).toMatchObject({
db: "missing",
outer: "outer",
});
});
});
Loading