Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions samples/StateMachine/Trax.Samples.StateMachine.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Trax State Machine sample — a GraphQL host over two fluent machines.
//
// Two machines, authored fluently in Trax.Samples.StateMachine, are discovered with one line
// (AddTraxStateMachines) and driven through the four generic `stateMachine` mutations:
// (AddStateMachines) and driven through the four generic `stateMachine` mutations:
// turnstile Locked ⇄ Unlocked (no effect)
// checkout Cart → Review → Paid (Paid committed, one exactly-once charge on Pay)
//
Expand Down Expand Up @@ -32,15 +32,13 @@
// }) { output { snapshot problem { code } } } } } }
// ─────────────────────────────────────────────────────────────────────────────

using Microsoft.EntityFrameworkCore;
using Trax.Api.Auth.ApiKey;
using Trax.Api.Extensions;
using Trax.Api.GraphQL.Extensions;
using Trax.Effect.Data.Postgres.Extensions;
using Trax.Effect.Extensions;
using Trax.Effect.Provider.Json.Extensions;
using Trax.Effect.StateMachine.Persistence;
using Trax.Effect.StateMachine.Persistence.Mutations;
using Trax.Mediator.Extensions;
using Trax.Samples.StateMachine;
using Trax.Samples.StateMachine.Api;
Expand All @@ -59,24 +57,21 @@
);
builder.Services.AddAuthorization();

// Trax + the state machine. The mediator scan includes StateMachineMutations.Assembly so the four generic
// mutations route by input type; AddTraxStateMachines discovers the machines in the sample library and
// wires the store, the effect-claim ledger, the exactly-once runner, and the registry.
// Trax + the state machine, in one builder chain. AddStateMachines discovers the machines in the sample
// library, wires the store / effect-claim ledger / exactly-once runner / registry, AUTO-registers the
// SnapshotDbContext against the Postgres provider above, and contributes the four generic `stateMachine`
// mutations to the mediator scan. The host names neither SnapshotDbContext nor the mutations' assembly.
// (Call AddStateMachines before AddMediator: the mediator builds its route registry when it runs.)
builder.Services.AddTrax(trax =>
trax.AddEffects(effects => effects.UsePostgres(connectionString).AddJson())
.AddMediator(typeof(TurnstileMachine).Assembly, StateMachineMutations.Assembly)
.AddStateMachines(typeof(TurnstileMachine).Assembly)
.AddMediator(typeof(TurnstileMachine).Assembly)
);
builder.Services.AddTraxStateMachines(typeof(TurnstileMachine).Assembly);

// The two host-supplied bindings a machine can't know: map auth to a user key, and the charge impl.
builder.Services.AddScoped<ISnapshotPrincipal, TraxCallerSnapshotPrincipal>();
builder.Services.AddScoped<ICharge, LoggingCharge>();

// The snapshot store's DbContext. The snapshot_draft + effect_claim tables are created by the
// Trax migration set: UsePostgres above runs DbUp, which applies 040_state_machine_snapshots.sql.
// Nothing here creates tables — `docker compose up -d` (a `trax` database) is all that's needed.
builder.Services.AddDbContext<SnapshotDbContext>(options => options.UseNpgsql(connectionString));

builder.Services.AddTraxGraphQL(graphql => graphql);
builder.Services.AddHealthChecks().AddTraxHealthCheck();

Expand Down
2 changes: 1 addition & 1 deletion samples/StateMachine/Trax.Samples.StateMachine/Machines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Trax.Samples.StateMachine;

// Two worked-example machines, authored with the fluent API. A host discovers them with one line
// (AddTraxStateMachines) and drives them through the four generic `stateMachine` GraphQL mutations. The
// (AddStateMachines) and drives them through the four generic `stateMachine` GraphQL mutations. The
// turnstile is the pure-structure proof (no effect); the checkout is the effectful proof (a committed state
// and one irreversible charge, fired exactly once).

Expand Down
Loading