#Custom functions <> return validators

13 messages · Page 1 of 1 (latest)

compact gust
#

If I have something like this:

export const authenticatedQuery = customQuery(
  queryRaw, // The base function we're extending
  {
    args: {
      clerkOrgId: v.optional(v.string()),
    },
    input: async (ctx, args) => {
      const { user, workspace, role } = await enhanceAuthenticatedContext(
        ctx,
        args.clerkOrgId
      );

      return { ctx: { user, workspace, role }, args: {} };
    },
  }
);

How do I update it so that I don't get errors when doing this:

export const query = authenticatedQuery({
  args: {
...
  },
  returns: someValidator,
...
compact gust
#

follow up question about custom functions: what's the recommended way to export a custom Context type that replaces MutationCtx and QueryCtx?

#

something like this?

export type AuthQueryCtx = QueryCtx & {
  user: Doc<"users">;
  workspace: Doc<"workspaces">;
  role: Doc<"roles"> | null;
};
marsh fern
astral river
#

If you're getting type errors, (1) make sure you're using the latest version of convex-helpers + convex (in particular, convex 1.13 requires a newer version of convex-helpers) (2) sharing the error here would be helpful

compact gust
#

updating convex-helpers fixed it, thanks @astral river !

compact gust
#

Related to the above context, where I have a custom query (authenticatedQuery), which takes in an additional argument, I was wondering if there's a recommended way to extend the useQuery hook to do avoid having to do something like this on every component that uses useQuery

  const organization = useOrganization();
  const clerkOrgId = organization?.organization?.id ?? null;
const foo = useQuery(api.bar, { ...args, clerkOrgId }
warm meteor
compact gust
#

these examples are just what I needed, thank you!! 👌

compact gust
#

do you have similar examples for fetchSessionQuery, etc? @warm meteor

compact gust
#

ideally there's also a nice way to inject the token inside the fetchAuthQuery function...

compact gust
#

Related: any guides on extending useQueryWithStatus and the useQuery from the caching library with similar purpose?