Skip to content

ExamplesBasic Requests

Basic Requests

Simple GET and POST requests using the proxy mode with React Query hooks.

proxy mode

GET — Fetch Users

useQuery fetches when the component mounts. What it fetched is cached and fetched again when it goes stale.

ts
import { q } from "@/lib/query"

// useQuery fetches on mount, and caches what it fetched
const { data, isLoading, error } = q.users.useQuery("getAll")
q.users.useQuery("getAll")SANDBOX
sending…
undefined
useQuery docs

POST — Create User

useMutation sends on demand. Run it to create a user.

ts
import { q } from "@/lib/query"

const { mutate, data, isPending } = q.users.useMutation("create")

mutate({ name: "Jane Doe", email: "jane@example.com", role: "user" })
q.users.useMutation("create")SANDBOX
idle
// run the request to see the response
useMutation docs

On Demand — Imperative Fetch

A query that waits: enabled: false holds it until refetch() runs it — here, when you run it.

ts
import { q } from "@/lib/query"

// enabled: false holds the query until refetch() runs it
const { data, isFetching, refetch } = q.users.useQuery("getById", {
  variables: { id: 1 },
  enabled: false,
})

refetch()
q.users.useQuery("getById", { enabled: false })SANDBOX
idle
// run the request to see the response