#how can I call this ?

12 messages · Page 1 of 1 (latest)

last fjord
#
export default query({
  handler: async (ctx) => {
    const allUserss = await ctx.runQuery(
      api.users.getAllUsers
    )
  },
})
heady edgeBOT
#

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

#

You call that query from a client (or from another server function) using the generated api reference for the file it’s defined in.

Assuming this code lives in convex/users.ts, the default export’s name is api.users.default. [Query names]

From React

import { useQuery } from "convex/react";
import { api } from "../convex/_generated/api";

export function MyComponent() {
  const data = useQuery(api.users.default, {});
  // use `data`
}

Queries are called via useQuery(api.myFunctions.sum, { ...args }); you just swap in api.users.default. [Calling queries]

From Node.js (HTTP client)

import { ConvexHttpClient } from "convex/browser";
import { api } from "./convex/_generated/api.js";

const client = new ConvexHttpClient(process.env.CONVEX_URL);

const result = await client.query(api.users.default, {});

[Node.js quickstart; Node client]

Note: Inside your default query you’re calling another query with ctx.runQuery(api.users.getAllUsers). That’s allowed, but the docs recommend often calling the helper function directly instead of runQuery to avoid the extra overhead. [GenericQueryCtx.runQuery]


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

#

@last fjord Kapa.ai is still learning and improving, please let me know how I did by reacting below.

last fjord
#

@heady edge I want to run this inside a custom query in convex dashboard, how can I do that ?

heady edgeBOT
#

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

#

You can’t directly call api.users.getAllUsers from a “Custom test query” in the dashboard, because that custom query is just a single query function whose source you type inline.

Instead, rewrite your logic directly in the custom query’s handler using the ctx it provides.

Your original query:

export default query({
  handler: async (ctx) => {
    const allUserss = await ctx.runQuery(
      api.users.getAllUsers
    )
  },
})

In the dashboard “Custom test query” editor, you’d enter something like:

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

export default query({
  args: {},
  handler: async (ctx) => {
    // Inline whatever `getAllUsers` does, e.g.:
    const allUsers = await ctx.db.query("users").collect();
    return allUsers;
  },
});

The dashboard runs exactly the code you put in that editor as a single query function; it doesn’t have access to the generated api helper or to ctx.runQuery because runQuery is only on ActionCtx / MutationCtx, not QueryCtx. [Query constructor; Custom test queries; GenericQueryCtx]

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

#

@last fjord Kapa.ai is still learning and improving, please let me know how I did by reacting below.

last fjord
#

@heady edge so if I want to run a query which, how can I do it ?

heady edgeBOT
#

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

#

In the Convex dashboard you don’t need to wrap an existing query to run it. You just run that query directly from the Functions page.

1. Run your existing query from the Functions page

  1. Go to Dashboard → Deployments → Functions.
  2. In the left list, find the query you want to run (e.g. users:getAllUsers or users:default). [Functions page]
  3. Click it, then click “Run function”.
  4. Fill in any args the function expects.
  5. Click Run. The result and logs show in the panel. [Functions – running]

That’s the normal way to “run a query” in the dashboard.

2. When to use a Custom test query instead

“Custom test query” on the Data page is only for ad‑hoc one‑off code you type inline. [Custom queries] It doesn’t give you the generated api object, so you can’t do:

await ctx.runQuery(api.users.getAllUsers)

inside it.

If you do need the same logic there, you:

  • Either copy the body of getAllUsers into the custom query, or
  • Better: move logic into a helper function (e.g. in convex/model/users.ts) and call that from both your real query and your custom test query. [Helper functions; Best-practices helpers]

So to “run a query” from the dashboard, prefer the Functions page and run the real query directly. Use Custom test query only for quick, inline experiments.

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

#

@last fjord Kapa.ai is still learning and improving, please let me know how I did by reacting below.