InboundTranslationSlice
For a short summary of InboundTranslationSlice, see Reventless Components Overview.
This component follows the Reventless Component Structure Pattern, using separate files for interface definitions (InboundTranslationSlice.res), builder logic (InboundTranslationSlice_Builder.res), and callback/handler logic (InboundTranslationSlice_Callback.res).
Overview
The InboundTranslationSlice implements the Event Modeling Translation pattern for inbound external communication. It receives external input in the sender's vocabulary, validates and transforms it through an anti-corruption layer, and publishes domain commands. The framework gives it a generated GraphQL mutation as its door, and, when its spec asks for one, a webhook URL beside it.
Event Modeling: The Inbound Translation Pattern
In Event Modeling, an Inbound Translation handles communication from external services into the system:
External Input --> Anti-Corruption Layer (translate) --> Command --> Event(s)
The anti-corruption layer protects the domain from external data formats and validates input before it enters the system.
Purpose and Responsibilities
- Responsibility: Receive external input; validate against a schema; translate to domain commands via an anti-corruption layer; publish commands to the shared CommandTopic; maintain an audit log of all translation attempts, except on a door open to visitors, whose requests are counted instead
- In: External JSON input (via
operations.receive) - Out: Commands to CommandTopic (via
publishJsons); audit records to QueryDb - Key Feature: Unlike other slices, InboundTranslationSlice is triggered externally via
operations.receiverather than by subscribing to domain events
Comparison with Task
| Aspect | Task | InboundTranslationSlice |
|---|---|---|
| Trigger | S3 object / schedule | A call to the slice's generated GraphQL mutation |
| Input validation | None -- raw JSON | Anti-corruption layer with schema validation |
| Translation | Ad-hoc | Structured translate function with typed input/output |
| Error handling | Lambda error | Structured error response to caller |
| Observability | CloudWatch only | QueryDb audit log of all translations |
Choose Task when you need S3 triggers, scheduled jobs, or provider-specific integrations.
Choose InboundTranslationSlice when you need validated, audited external input processing with a clean anti-corruption layer.
Component Spec
An InboundTranslationSlice is split into two files:
<Name>.res— the spec (@@reventless.spec): theexternalInputandcommand@schematypes, plustargetName(the aggregate or StateChangeSlice that receives the produced command).<Name>_Translation.res— the translation (@@reventless.translation): the synchronoustranslatefunction (the anti-corruption layer).
The spec module type the framework expects:
module type Spec = {
// name and moduleUrl are injected by @@reventless.spec — you never write them
@schema type externalInput
@schema type command
let targetName: string
type role
let authorizationOf: string => Authorization.rule<role>
let endpoint: option<endpointConfig<role>>
let capabilityNeeds: array<CapabilityNeed.t>
}
translate lives on the Translation module.
There is no DcbEventLogSpec reference. @@reventless.spec injects name,
moduleUrl, a default authorizationOf (AllowAuthenticated), let endpoint = None
when the spec declares no webhook URL, and type role, the plugin's Roles.t when it declares one (see
Authorization).
Spec Fields Explained
| Field | Type | Description |
|---|---|---|
externalInput | @schema type | The external data format received from the outside world |
command | @schema type | The domain command produced by the anti-corruption layer |
targetName | string | Name of the aggregate or StateChangeSlice that receives the produced command |
endpoint | option<endpointConfig<role>> | The slice's webhook URL, if it has one |
capabilityNeeds | array<CapabilityNeed.t> | The platform capabilities translate reaches for; [] for a slice that only maps input. See Reaching a capability |
The translate Function
The translate function lives in the _Translation.res file and is the
anti-corruption layer -- it protects the domain from external data formats. It
takes the input, ~capabilities and ~caller, and returns a
promise<result<array<(string, command)>, string>>:
Ok([(targetId, command), ...])-- Input is valid; publish one or more commands to the target entitiesOk([])-- Idempotent no-op; nothing to publishError(msg)-- Input is invalid or cannot be translated; return error to caller
A slice that only maps input ignores ~capabilities and ~caller and does no
awaiting; it is still declared async, so every translation has one shape.
Reading the caller
~caller is who called the door, as its transport identified them: an
Identity.t with the caller's userId and groups, Identity.anonymous for a
caller who is not signed in, or None where the platform acts for itself. A door
that must look at the caller before it decides — an invitation only some roles may
send — refuses here, before it reaches any capability:
let translate = async (input, ~capabilities as _, ~caller: option<Reventless.Identity.t>) =>
switch caller {
| Some(identity) if identity->Reventless.Identity.hasGroup("Finance") => Ok([/* … */])
| _ => Error("only Finance marks a payment paid by hand")
}
The refusal is recorded like any other, and the audit row names the caller's
userId. Each command translate returns is still checked against the caller's
rule afterwards, so reading the caller widens nothing. In a scenario, name the
caller with whenInput(input, ~caller=identity) or whenReceived(json, ~caller=…);
left out, the platform is acting for itself.
Reaching a capability
Some input has to reach a provider before any command exists, and must not reach
the log at all: a password on a set-a-password step is the case this was built for.
translate is handed the platform's Capabilities.t — the same record an outbound
slice's translate gets — so it can create a principal with the password and
publish only a command that names the result.
Two declarations make that safe:
capabilityNeedsnames whattranslatereaches for. It reaches the plugin'srequiredCapabilities, so a platform that provisions none of it is told.@sensitiveon anexternalInputfield keeps its value out of the slice's audit row. Every request writes one, success or refusal, and operators can read them; a marked field is stored as[withheld], and a webhook body too malformed to redact field by field is not stored at all.
On AWS an inbound slice runs in the shared DCB command Lambda, which is handed no
capabilities, so a slice declaring any is refused at deploy rather than left to
answer Unavailable at run time. Host it on the local platform for now.
Usage Pattern
Example 1: Payment Webhook
The spec file. @@reventless.spec injects name, module Id, and
moduleUrl from the filename; inside a slice folder it auto-applies DCB tags
to *Id fields — never write @s.matches(...) by hand:
@@reventless.spec
@schema
type externalInput = {
paymentId: string,
orderId: string,
status: string,
amount: float,
}
@schema
type command = ConfirmPayment({
orderId: string,
paymentId: string,
amount: float,
})
let targetName = "ConfirmPayment"
let capabilityNeeds = []
The translation file (@@reventless.translation) holds translate, returning
an array of (targetId, command) pairs:
@@reventless.translation
let translate = async (input: externalInput, ~capabilities as _, ~caller as _) =>
switch input.status {
| "completed" =>
Ok([
(
input.orderId,
ConfirmPayment({
orderId: input.orderId,
paymentId: input.paymentId,
amount: input.amount,
}),
),
])
| "refunded" =>
// Could map to a different command variant
Error("Refund handling not yet implemented")
| status => Error("Unknown payment status: " ++ status)
}
Example 2: Shipping Update Webhook
@@reventless.spec
@schema
type externalInput = {
trackingId: string,
orderId: string,
event: string,
timestamp: string,
}
@schema
type command = UpdateShipmentStatus({
orderId: string,
trackingId: string,
status: string,
})
let targetName = "UpdateShipmentStatus"
let capabilityNeeds = []
@@reventless.translation
let translate = async (input: externalInput, ~capabilities as _, ~caller as _) =>
switch input.event {
| "picked_up" | "in_transit" | "delivered" =>
Ok([
(
input.orderId,
UpdateShipmentStatus({
orderId: input.orderId,
trackingId: input.trackingId,
status: input.event,
}),
),
])
| event => Error("Unrecognized shipping event: " ++ event)
}
Plugin Wiring
You never register or wire InboundTranslationSlices by hand. The plugin generator
scans the InboundTranslation/ folder and emits the wiring into the
generated Plugin.res using the two-arg factory
Platform.InboundTranslationSlice.Make(Spec, Translation):
module Make = (Platform: ReventlessInfra.Platform.T) => {
// InboundTranslationSlices
module PaymentWebhookSlice = Platform.InboundTranslationSlice.Make(PaymentWebhook, PaymentWebhook_Translation)
module ShippingUpdateSlice = Platform.InboundTranslationSlice.Make(ShippingUpdate, ShippingUpdate_Translation)
let make = () =>
Platform.Plugin.make(
~name="Ordering",
~inboundTranslationSlices=[module(PaymentWebhookSlice), module(ShippingUpdateSlice)],
// ... other components
)
}
At run time
Receive Flow
Unlike other slices, InboundTranslationSlice is triggered externally via operations.receive:
Receive processing steps:
receive(inputJson):
1. Parse inputJson against Spec.externalInputSchema
-> Error: record audit failure, return Error(msg)
2. Call Translation.translate(input) -- anti-corruption layer
-> Error(msg): record audit failure, return Error(msg)
3. Encode command via Spec.commandSchema
-> Error: record audit failure, return Error(msg)
4. Publish command via publishJsons
-> Error: record audit failure, return Error(msg)
5. Record audit success, return Ok(acceptedResult)
Integration: The Generated Mutation
You do not wire a transport. Declaring the slice declares a GraphQL mutation, and both platforms create it for you:
| Platform | What is created |
|---|---|
| AWS | An AppSync DataSource + Resolver per slice, pointing at the shared DCB CommandTopic Lambda. The request template carries __inboundTranslation: true, which routes the invocation to the slice's receive instead of the regular SQS command path. |
| Local | The mutation field registered on the local GraphQL server. Registration is two-phase, so a caller can invoke receive before the deploy-time Output resolves — early calls park in a queue and drain once the real receive is bound. |
The field is named <Plugin>_<Slice>, its arguments come from @schema type externalInput (not the internal command), and it returns CommandResult.
For the PaymentWebhook slice above, in a plugin named Ordering:
Ordering_PaymentWebhook(amount: Float!, orderId: ID!, paymentId: ID!, status: String!): CommandResult!
Arguments are emitted alphabetically, and orderId / paymentId render as ID!
because the PPX auto-tags *Id fields inside a slice folder. See the
GraphQL API guide
for the full mapping.
There is no opt-out. Unlike a StateChangeSlice command, an inbound slice honors
no @noApi — every declared slice gets its mutation.
The field answers under the slice's authorizationOf, which defaults to
AllowAuthenticated (see Authorization). A caller that
must reach it without signing in needs AllowAnonymous written on the spec.
A webhook URL
A sender that can only post JSON to an address — a payment provider, a carrier —
does not speak GraphQL and cannot sign in. For it, the spec declares an endpoint,
and the slice gets a URL beside its mutation. Both doors lead to the same
receive, so the same input produces the same commands whichever way it came.
@@reventless.spec
@@reventless.authorize(AllowRoles([Admin]))
// … externalInput, command, targetName …
let endpoint = Some({
Reventless.InboundTranslationSlice.path: "supplier-feed",
auth: SignedBody({
header: "X-Signature-256",
prefix: "sha256=",
timestampHeader: "X-Signature-Timestamp",
secret: "supplierFeed",
}),
role: Roles.Admin,
})
| Field | Meaning |
|---|---|
path | The slice's segment under the plugin's webhook base URL |
method | Post (the default) or Put |
auth | Who may call: SignedBody(signature) or Open — required, so the choice is always written down |
role | The role a request that passes auth acts as; none when absent |
transport | FunctionUrl, the default and, for now, the only one |
Signed requests
SignedBody is what webhook providers do: the sender computes an HMAC-SHA256 of
the raw body with a secret you share, hex-encodes it and sends it in header,
after prefix if there is one. With timestampHeader the signed text is
<timestamp>.<body>, the timestamp in seconds, and a request more than
toleranceSeconds (default 300) from now is refused, so a captured request cannot
be replayed later. The check runs on the bytes as sent, before the body is parsed.
secret names the secret; its value never appears in source. The platform reads it
from REVENTLESS_WEBHOOK_SECRET_<SECRET> — the name upper-cased, other characters
as _. That variable may list several, comma-separated: while you rotate, put the
new and the old one there, and either passes.
- AWS: set it in the plugin stack's config,
pulumi config set --secret webhook:supplierFeed <value>. The deploy (andpulumi preview) fails while it is missing. - Locally: nothing to do. At start the local platform makes a secret for each
signed endpoint whose variable is not set, keeps it in
.reventless/webhook-secrets.json(the same one on every start) and sets the variable. ExportREVENTLESS_WEBHOOK_SECRET_SUPPLIERFEEDto pin one instead. The file also lists each endpoint's path, header and secret name, so a caller on the same machine, such as a stand-in or a tool, signs as the real sender will; the running-platform entry in.reventless/running/names the file underwebhooks.secrets. It is local state: never commit it.
A request the platform has no secret for is answered 500 and logged; it is never
treated as open.
Who the request acts as
A signed request acts as a signed-in caller holding the endpoint's role. Each
command its input translates into is then checked against that command's own rule,
exactly as on the mutation, and one refused refuses the whole input. An Open URL
identifies nobody: it acts as an anonymous caller, so only commands written
AllowAnonymous get through it. Every request to an open URL still runs the
handler and writes an audit row, so an open URL costs you for every stranger's
request.
The signature protects the URL, not the mutation. If the commands keep the
default AllowAuthenticated, any signed-in user can send a forged input through
the mutation. Give the commands a rule that admits only the sender's role, as the
example does with Admin, so the mutation becomes an operator's replay door. Or
withhold the mutation altogether.
A webhook-only slice
@noApi on externalInput withholds the mutation, which leaves the URL as the
slice's only door:
@noApi @schema
type externalInput = {paymentId: string, status: string}
no endpoint | endpoint declared | |
|---|---|---|
| default | mutation only | mutation and URL |
@noApi | refused at build — nothing could reach it | URL only |
Where the URL is
On AWS the plugin's command handler gets one public Lambda Function URL, and
each slice's endpoint is its path under it. Read it in the plugin's deploy
program after deployPlugin, to export it and register it with the sender:
let webhookUrl = Platform.webhookUrl() // "" when no slice declares an endpoint
Locally the domain server serves each endpoint at /webhooks/<path>, beside
/graphql: http://localhost:4000/webhooks/supplier-feed. The running-platform
entry names the base under webhooks.base. The route hands the raw body and headers
to LocalWebhookRegistry, which runs the same signature check and dispatch as the
Function URL. A test can post through the registry directly:
let response = await LocalWebhookRegistry.post(~path="supplier-feed", ~headers, body)
Responses
The status tells a sender whether to try again: 2xx done, 5xx try again, any
other 4xx do not.
| Status | When |
|---|---|
200 | Translated and published, {requestId, commandCount} |
400 | The body is not JSON |
401 | The signature is missing, wrong, or outside the time window |
403 | The endpoint's role may not send a command the input translates into, or the target refused one for lack of permission |
404 / 405 | No slice at that path / the wrong method |
422 | The input was refused: by its schema, by translate, or by the target's decision |
500 | The secret is not configured |
503 | The commands could not be published, or the target's platform failed |
Every answer from 400 to 422, and 503, writes an audit row first; 404, 405
and 500 do not, because no slice was reached or no secret was there to check with.
Audit Log
All translation attempts are recorded in a QueryDb for observability:
type auditStatus = Success | Failure
type door = Mutation | Webhook
type auditRow = {
input: string,
status: auditStatus,
targetIds?: array<string>,
commandCount?: int,
error?: string,
receivedAt: string,
door?: door,
}
The audit log records every input, whether successful or not, providing a complete
history of all external input processed by the slice. door says which way it
came in, so the log can answer "did the provider actually call us?"; it is absent
on rows written before the slice had a second door. A webhook request refused
before it could be read keeps its raw body as input.
Error Handling
Input Parsing Errors:
- Invalid JSON or schema mismatch returns
Errorto the caller - Audit log records the failure with the raw input
Translation Errors:
translatereturningError(msg)is the normal rejection path (e.g., unknown status code)- Audit log records the failure with the parsed input
Publishing Errors:
- Command encoding failures return
Errorto the caller publishJsonsfailures returnErrorto the caller- Audit log records the failure
The target's refusal:
- Where the target is a sync slice of the same plugin,
receivewaits for its answer. The first command it refuses answers the caller instead of an acceptance: the target's error code, its detail, and the cause (business, permission, or platform). The audit row records it as a failure. - An async target answers nothing inline, so the caller hears an acceptance, as before.
- A door open to anyone hears the target's reasons too. Write the target so a
refusal tells a stranger nothing it should not know: a sign-up for an address
that already has an account answers
Ok([]), never "already registered".
All errors are returned to the caller as Error(msg), allowing the external system to retry or handle the failure.
Pulumi Outputs
type outputs = {
resources: array<Adapter.resource>,
queryDb: QueryDb.outputs,
}
type operations = {
receive: (JSON.t, ~caller: Identity.t=?, ~door: door=?) => promise<receiveResult>,
refuse: (~body: string, ~errorCode: string, string) => promise<receiveResult>,
}
Resource Naming:
- Component type:
reventless:InboundTranslationSlice - Audit log QueryDb:
{name}Audit
Dependencies:
- CommandTopic (via
publishJsonsfor command publishing) - QueryDb (for audit log persistence)
Related Components
- OutboundTranslationSlice -- Complementary component for calling external services
- Stand-ins -- Play the things and services that post to a door, on a local platform
- StateChangeSlice -- Processes the commands InboundTranslationSlice produces
- DcbEventLog -- Shared event log that receives events from processed commands
- CommandTopic -- Receives commands from the translator
- QueryDb -- Stores the audit log
- Task -- Alternative for S3/schedule-triggered external processing
- Plugin -- Hosts InboundTranslationSlice via DcbSpec