Skip to content

Consume generated proxies

Use generated proxies when a React component needs to call an Arc command or query. Arc generates TypeScript command, query, and model types from discovered endpoints for the shared frontend runtime. You import those modules instead of maintaining request shapes and transport code yourself.

After a backend declaration changes, run its generator and type-check the frontend. A renamed property can then produce a compiler error at the component that still uses it. This feedback depends on regeneration; it does not check whether deployed client and server versions agree. Change backend declarations and regenerate rather than editing generated files.

Current generated command and query modules import both @cratis/arc and @cratis/arc.react, even when you call them imperatively. Install their dependencies in your React app:

Terminal window
npm install @cratis/fundamentals @cratis/arc @cratis/arc.react react react-dom

Mount Arc above hook consumers. For build configuration, namespace mapping, source-file grouping, and output ownership, use the C# backend proxy-generation reference, the JVM TypeScript proxies guide, or the TypeScript backend’s generator setup.

Arc is a standalone CQRS (Command Query Responsibility Segregation) framework: commands change state and queries read it. A command can call an ordinary service and return a response; it does not need events or an event log.

This backend fragment assumes your application implements and registers the account service. That interface is application-owned, not an Arc API:

using Cratis.Arc.Commands.ModelBound;
using Cratis.Concepts;
public record AccountName(string Value) : ConceptAs<string>(Value)
{
public static readonly AccountName NotSet = new(string.Empty);
}
public record AccountBalance(decimal Value) : ConceptAs<decimal>(Value)
{
public static readonly AccountBalance NotSet = new(0m);
}
public interface IAccountService
{
Task<Guid> Open(AccountName name, AccountBalance initialBalance);
}
[Command]
public record OpenDebitAccount(AccountName Name, AccountBalance InitialBalance)
{
public Task<Guid> Handle(IAccountService accounts) =>
accounts.Open(Name, InitialBalance);
}

View C# snippet source on GitHub

The named concepts keep account names and balances distinct in backend signatures. Their generated client properties use the underlying primitive — here string and number — so the form does not need to construct concept objects. Keep each concept in its own file when organizing your application.

After generation, import the proxy into a React component. This consumer fragment assumes its generated import path is ./generated/OpenDebitAccount. It supplies the account name and balance, executes the command from the button handler, and logs the response only on success:

import { OpenDebitAccount } from './generated/OpenDebitAccount';
export function OpenAccountButton() {
const [command] = OpenDebitAccount.use({
name: 'Primary account',
initialBalance: 500
});
async function open() {
const result = await command.execute();
if (result.isSuccess) {
console.log(String(result.response));
}
}
return <button onClick={() => void open()}>Open account</button>;
}

An ordinary Guid/UUID response remains a supported response and maps to Fundamentals Guid. The Chronicle integration adds optional event-sourcing behavior; it does not redefine generic Arc responses.

The command hook returns [command, setValues, clearValues]. Set required content explicitly, as the example does for name and initialBalance. The setter edits properties; it does not reset the change-tracking baseline. The button above demonstrates execution, not a complete form: add pending state and feedback for unsuccessful results before using it for account entry. See data binding and command results.

Command results expose isMissingHandler when no server handler was registered, so execution or validation could not run. The result still carries exceptions and HTTP 500; older servers that omit the flag are read as false.

Both controller-based and model-bound C# endpoints generate proxies; the JVM generates from model-bound @ReadModel query methods only — it has no controller-based discovery. TypeScript’s source generator reads @readModel() classes with static @query() methods; see its proxy generation overview for supported output. Parameterized queries emit a NameParameters interface. Query hooks return tuples, not model objects; read the first tuple element’s .data.

  • Ordinary .use() returns [result, perform, setSorting].
  • Observable enumerable .use() returns [result, setSorting].
  • Observable single-model .use() returns [result].
  • Only enumerable proxies expose .useWithPaging(), .useSuspenseWithPaging(), and, for observables, .useChangeStream().

See query usage for argument positions and the complete tuple matrix. Results expose isPerforming, isReady, isAuthorized, isValid, hasExceptions, and exceptionMessages; they do not expose an error or isLoading property.

Use observable proxies when the screen should follow changes from an emitting backend source. MongoDB observation is one option; Chronicle is another optional integration. A database write does not become a live notification without that source wiring. Both Server-Sent Events (SSE) and WebSocket support direct and shared-hub modes.

All generated queries expose .useSuspense(). Suspense handles initial waiting and selected failures, not every unsuccessful result: keep validation/readiness handling in the component. Read Suspense limitations before adding an error boundary or retry control.