#custom args with customQuery

7 messages · Page 1 of 1 (latest)

oak bladeBOT
#

Hi @teal wave. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To create a custom query with custom arguments using customQuery, you can follow the pattern shown in the knowledge sources. Here's how you can modify your queryWithAuth function to use customQuery:

import { customQuery, customCtx } from "convex-helpers/server/customFunctions";

export function queryWithAuth<ArgsValidator extends PropertyValidators, Output>({
    args,
    handler
}: {
    args: ArgsValidator;
    handler: (
        ctx: Omit<QueryCtx, 'auth'> & {
            auth: Auth;
            userSessionContext: UserSessionContext;
        },
        args: ObjectType<ArgsValidator>
    ) => Output;
}) {
    const authQuery = customQuery(
        query,
        customCtx(async (ctx) => {
            const auth = getAuth(ctx.db as DatabaseWriter);
            const userSessionContext = await getValidExistingSession(ctx, args.sessionId);
            return { ...ctx, userSessionContext, auth };
        })
    );

    return authQuery({
        args: {
            ...args,
            sessionId: v.union(v.null(), v.string())
        },
        handler
    });
}

In this code, customQuery is used to create a new query function authQuery that includes the custom context. The customCtx helper is used to modify the ctx argument to the function, adding the auth and userSessionContext fields. The handler function is then passed to authQuery as is, and it will receive the modified ctx when it's called.

Please note that you need to ensure that the getAuth and getValidExistingSession functions are properly defined and imported in your code.

#

@teal wave kapa.ai is still learning and improving, please let us know how it did by reacting below

teal wave
#

@oak blade how can i access args within customQuery

oak bladeBOT
#

Let me check the knowledge base for you. One moment! :books:

#

To access arguments within customQuery, you can do so within the handler function that you pass to the customQuery. The handler function receives two arguments: ctx and args. Here's an example:

const userQuery = customQuery(
  query,
  customCtx(async (ctx) => {
    // ctx modifications here
  })
);

export const myInfo = userQuery({
  args: { includeTeam: v.boolean() },
  handler: async (ctx, args) => {
    // You can access args here
    if (args.includeTeam) {
      // args.includeTeam is accessed here
    }
    // rest of the handler code
  }
});

In this example, args is accessed within the handler function of myInfo. The args object will contain the arguments passed to the myInfo function when it's called.

For more details, you can refer to the knowledge source (https://stack.convex.dev/custom-functions#modifying-the-ctx-argument-to-a-server-function-for-user-auth).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: