Robert
Robert
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
Now it is working! Sharing a zMutationWithUser example in case someone encounters a similar issue in the future
import { getAuthUserId } from "@convex-dev/auth/server";
import { ConvexError } from "convex/values";
import { zCustomMutation } from "convex-helpers/server/zod";

import { mutation } from "../_generated/server";

export const zMutationWithUser = zCustomMutation(mutation, {
args: {},
input: async (ctx, args) => {
const userId = await getAuthUserId(ctx);

if (!userId) {
throw new ConvexError({
message: "Unauthorized",
code: "INTERNAL_SERVER_ERROR",
statusCode: 500,
severity: "error",
});
}

const user = await ctx.db.get(userId);

if (!user) {
throw new ConvexError({
message: "User not found",
code: "INTERNAL_SERVER_ERROR",
statusCode: 500,
severity: "error",
});
}

return { ctx: { user }, args };
},
});
import { getAuthUserId } from "@convex-dev/auth/server";
import { ConvexError } from "convex/values";
import { zCustomMutation } from "convex-helpers/server/zod";

import { mutation } from "../_generated/server";

export const zMutationWithUser = zCustomMutation(mutation, {
args: {},
input: async (ctx, args) => {
const userId = await getAuthUserId(ctx);

if (!userId) {
throw new ConvexError({
message: "Unauthorized",
code: "INTERNAL_SERVER_ERROR",
statusCode: 500,
severity: "error",
});
}

const user = await ctx.db.get(userId);

if (!user) {
throw new ConvexError({
message: "User not found",
code: "INTERNAL_SERVER_ERROR",
statusCode: 500,
severity: "error",
});
}

return { ctx: { user }, args };
},
});
Thanks for help!
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
That's good to know, will try
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
ok, i see. I will try with custom functions, i think this will work perfectly. Thanks for help and getting through my messy code 😄
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
Ok i will recreate the withUser using custom functions and see if this will work
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
I forgot to say that this is on dev branch, sorry
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
based on this screen the line 14
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
I see that you are showing the userId type which is fine with displaying but i am talking about
any
any
in
handler: withUser(async (ctx, args) =>
handler: withUser(async (ctx, args) =>
- args are type
any
any
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
No description
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
I use yarn
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
line 16
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
convex/PROJECT_FUNCTIONS/applications.ts the gameId was an example that this happends even if i do it on the 1:1 example from fast5 repo
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
I've sent an invite
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
ok i will add you in a sec
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
Sure, I can share the code, but not publicly. Let me know how you'd like to proceed—DM with link or adding you to a private repo?
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
So I updated dependencies and cleared cache and the problem is no longer appearing but the args still have type any
export const applyToOffer = zMutation({
args: { data: applicationValidator },
handler: withUser(async (ctx, args) => {
const userId = await getAuthUserId(ctx);

const { projectId } = args.data;

const project = await ctx.db.get(projectId);

if (!project) {
throw new ConvexError({
message: "Project not found",
code: "INTERNAL_SERVER_ERROR",
statusCode: 500,
severity: "error",
});
}

await ctx.db.insert("applications", {
...args.data,
status: "PENDING",
applicantId: userId,
});
}),
});
export const applyToOffer = zMutation({
args: { data: applicationValidator },
handler: withUser(async (ctx, args) => {
const userId = await getAuthUserId(ctx);

const { projectId } = args.data;

const project = await ctx.db.get(projectId);

if (!project) {
throw new ConvexError({
message: "Project not found",
code: "INTERNAL_SERVER_ERROR",
statusCode: 500,
severity: "error",
});
}

await ctx.db.insert("applications", {
...args.data,
status: "PENDING",
applicantId: userId,
});
}),
});
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
My next.js version is 14.2.7 and node 22.13.1
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
Build with the downlevelIteration as true is still failing: my convex/tsconfig.ts
{
"compilerOptions": {
/* These settings are not required by Convex and can be modified. */
"allowJs": true,
"strict": false,
"moduleResolution": "Bundler",
"jsx": "react-jsx",
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,

/* These compiler options are required by Convex */
"target": "ESNext",
"lib": ["ES2021", "dom"],
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"downlevelIteration": true,
"noEmit": true,

"isolatedModules": true
},
"include": ["./**/*"],
"exclude": ["_generated"]
}
{
"compilerOptions": {
/* These settings are not required by Convex and can be modified. */
"allowJs": true,
"strict": false,
"moduleResolution": "Bundler",
"jsx": "react-jsx",
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,

/* These compiler options are required by Convex */
"target": "ESNext",
"lib": ["ES2021", "dom"],
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"downlevelIteration": true,
"noEmit": true,

"isolatedModules": true
},
"include": ["./**/*"],
"exclude": ["_generated"]
}
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
The withUser is copied from fast5 repo:
import { QueryCtx, MutationCtx, mutation, query } from '../_generated/server';
import { Doc } from '../_generated/dataModel';

/**
* Wrapper for a Convex query or mutation function that provides a user in ctx.
*
* Throws an exception if there isn't a user logged in.
* Pass this to `query`, `mutation`, or another wrapper. E.g.:
* export default mutation({
* handler: withUser(async ({ db, auth, user }, {args}) => {...})
* });
* @param func - Your function that can now take in a `user` in the first param.
* @returns A function to be passed to `query` or `mutation`.
*/
export const withUser = <Ctx extends QueryCtx, Args extends [any] | [], Output>(
func: (ctx: Ctx & { user: Doc<'users'> }, ...args: Args) => Promise<Output>
): ((ctx: Ctx, ...args: Args) => Promise<Output>) => {
return async (ctx: Ctx, ...args: Args) => {
const identity = await ctx.auth.getUserIdentity();
if (!identity) {
throw new Error(
'Unauthenticated call to function requiring authentication'
);
}
// Note: If you don't want to define an index right away, you can use
// db.query("users")
// .filter(q => q.eq(q.field("tokenIdentifier"), identity.tokenIdentifier))
// .unique();
const user = await ctx.db
.query('users')
.withIndex('by_token', (q) =>
q.eq('tokenIdentifier', identity.tokenIdentifier)
)
.unique();
if (!user) throw new Error('User not found');
return func({ ...ctx, user }, ...args);
};
};
import { QueryCtx, MutationCtx, mutation, query } from '../_generated/server';
import { Doc } from '../_generated/dataModel';

/**
* Wrapper for a Convex query or mutation function that provides a user in ctx.
*
* Throws an exception if there isn't a user logged in.
* Pass this to `query`, `mutation`, or another wrapper. E.g.:
* export default mutation({
* handler: withUser(async ({ db, auth, user }, {args}) => {...})
* });
* @param func - Your function that can now take in a `user` in the first param.
* @returns A function to be passed to `query` or `mutation`.
*/
export const withUser = <Ctx extends QueryCtx, Args extends [any] | [], Output>(
func: (ctx: Ctx & { user: Doc<'users'> }, ...args: Args) => Promise<Output>
): ((ctx: Ctx, ...args: Args) => Promise<Output>) => {
return async (ctx: Ctx, ...args: Args) => {
const identity = await ctx.auth.getUserIdentity();
if (!identity) {
throw new Error(
'Unauthenticated call to function requiring authentication'
);
}
// Note: If you don't want to define an index right away, you can use
// db.query("users")
// .filter(q => q.eq(q.field("tokenIdentifier"), identity.tokenIdentifier))
// .unique();
const user = await ctx.db
.query('users')
.withIndex('by_token', (q) =>
q.eq('tokenIdentifier', identity.tokenIdentifier)
)
.unique();
if (!user) throw new Error('User not found');
return func({ ...ctx, user }, ...args);
};
};
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
Example function with wrapper:
const zMutation = zCustomMutation(mutation, NoOp);

export const applyToOffer = zMutation({
args: { data: applicationValidator },
handler: withUser(async (ctx, args) => {
const { projectId } = args.data;

const project = await ctx.db.get(projectId);

if (!project) {
throw new ConvexError({
message: "Project not found",
code: "INTERNAL_SERVER_ERROR",
statusCode: 500,
severity: "error",
});
}

await ctx.db.insert("applications", {
...args.data,
status: "PENDING",
});
}),
});
const zMutation = zCustomMutation(mutation, NoOp);

export const applyToOffer = zMutation({
args: { data: applicationValidator },
handler: withUser(async (ctx, args) => {
const { projectId } = args.data;

const project = await ctx.db.get(projectId);

if (!project) {
throw new ConvexError({
message: "Project not found",
code: "INTERNAL_SERVER_ERROR",
statusCode: 500,
severity: "error",
});
}

await ctx.db.insert("applications", {
...args.data,
status: "PENDING",
});
}),
});
export const applicationValidator = z.object({
applicantId: zid("users"),
projectId: zid("projects"),
offerId: zid("offers"),
status: applicationStatusValidator,
position: z.string().max(MAX_OFFER_POSITION_INPUT_LENGTH),
message: z.string().max(MAX_APPLICATION_MESSAGE_INPUT_LENGTH),
resume: z.string(),
skills: z.array(z.string()),
experience: z.string(),
updatedAt: z.string(),
});
export const applicationValidator = z.object({
applicantId: zid("users"),
projectId: zid("projects"),
offerId: zid("offers"),
status: applicationStatusValidator,
position: z.string().max(MAX_OFFER_POSITION_INPUT_LENGTH),
message: z.string().max(MAX_APPLICATION_MESSAGE_INPUT_LENGTH),
resume: z.string(),
skills: z.array(z.string()),
experience: z.string(),
updatedAt: z.string(),
});
67 replies
CCConvex Community
Created by Robert on 2/7/2025 in #support-community
Type Issue with gameId in Convex Query
Ok, will try and get back to you.
67 replies