Tells graphitron to write a message to your transactional outbox every time a mutation field succeeds, inside the transaction that made the change, so the message exists if and only if the write committed. A transactional outbox is the pattern where a service records "this happened" in its own database, in the transaction that made it happen, and a separate relay (a poller, or change data capture such as Debezium) publishes it to a message broker afterwards.
A message is a business event: what happened, named by you (FilmCreated, RentalReturned), and a body you shape for the systems that consume it. It is not a record of which rows changed, and an event may mention any number of ids in its payload. Graphitron writes the event’s type and its payload; your table or routine gives it an id and a time, and your relay decides where it goes.
A message can also describe the field’s failure: an @outbox(on: ERROR) is written when the field fails, after its transaction has rolled back (On failure). A call that commits writes its success messages and no failure message, and a call that rolls back writes its failure messages and no success message.
The directive is repeatable: each @outbox on a field is one message, written once per call that ends in its outcome, in the order the directives appear. To publish something new, add another @outbox beside the ones you have; the published messages do not change, so their consumers are not affected. That includes a new version of an event: add @outbox(event: "FilmCreated.v2", …) beside the old one, and remove the old one when its last consumer has moved.
SDL signature
directive @outbox(event: String, argMapping: String, on: OutboxOutcome = SUCCESS) repeatable on FIELD_DEFINITION
enum OutboxOutcome { SUCCESS, ERROR }
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
|
|
the field’s coordinate ( |
The message’s event type, the name your downstream consumers subscribe to. Name it when the event should outlive the field’s name, since renaming a field without an |
|
|
(none) |
Builds the message’s flat JSON payload, |
|
|
|
The outcome the message is written on: |
Where a value comes from
| Source | Meaning |
|---|---|
|
A field argument, walking into nested input fields with dots, as in every |
|
The field’s success outcome: what the write produced. For an |
|
One member of the session handle your |
|
The field and its operation. |
|
On an |
Values appear in your API’s own representation: an ID as your clients see it, an enum by its GraphQL name.
Keys are flat, one JSON key per entry. An object or an array in the payload comes from its source, not from the key.
Example
type Mutation {
createFilm(in: FilmCreateInput!): Film
@mutation(typeName: INSERT)
@outbox(event: "FilmCreated", argMapping: "filmId: $result.id, title: in.title, languageId: in.languageId")
@outbox(event: "FilmCatalogued", argMapping: "filmId: $result.id, releaseYear: in.releaseYear")
}
type Film implements Node @table(name: "film") @node {
id: ID! @nodeId
}
input FilmCreateInput {
title: String
languageId: Int @field(name: "language_id")
releaseYear: Int @field(name: "release_year")
}
When
Right after the field’s write succeeds and before its payload is resolved, as one more statement in the field’s transaction. A field that failed writes no message: its transaction rolls back, and a failure an @error channel turned into a typed payload is a failure. If the emit fails, the field fails and rolls back, write included; the error is logged and redacted, and no @error channel can claim it, since an outbox fault is not business data.
A call that wrote no rows (an UPDATE that matched nothing, or one whose input sets no column) writes no message.
On a @service field the service runs inside the same transaction as its messages: graphitron opens it and hands the service a DSLContext on it. What the service writes through that DSLContext, nested transactions included, commits with the messages or rolls back with them. A write the service makes some other way, through a connection of its own or a call to another system, is outside the transaction, and no message can be made atomic with it.
A @service field that says transactionControl: SERVICE has no transaction for its messages to share: its service commits its own writes as it goes (transaction demarcation). Its messages follow the field’s outcome instead of what the service committed. When the field settles having succeeded, its payload included, graphitron writes its success messages in one transaction of their own on the field’s connection; when it fails, it writes its ERROR messages the same way, with nothing rolled back first, so what the service committed stands beside them. Both sides are then at most once, as failure messages always are (On failure): a process that dies between the service’s commit and the write loses the message, and a message never says more than the field’s outcome, so a field that failed after its service committed writes its ERROR messages and no success message.
On failure
On a field that returns a payload with an errors field, @outbox(on: ERROR, …) is written when the field fails instead:
type Mutation {
createFilm(in: FilmInput!): FilmInsertPayload
@mutation(typeName: INSERT, table: "film")
@outbox(event: "FilmCreated", argMapping: "title: in.title")
@outbox(on: ERROR, argMapping: "kind: $error.__typename, why: $error.message, title: in.title")
}
type FilmInsertPayload {
film: Film
errors: [FilmInsertError]
}
type Film implements Node @table(name: "film") @node {
id: ID! @nodeId
}
type FilmLanguageMissing @error(handlers: [{handler : DATABASE, sqlState : "23503"}]) {
message: String!
}
type FilmValueMissing @error(handlers: [{handler : DATABASE, sqlState : "23502"}]) {
message: String!
}
union FilmInsertError = FilmLanguageMissing | FilmValueMissing
input FilmInput {
title: String
}
Graphitron first rolls the field’s transaction back, so nothing the field wrote survives, its success messages included, and then writes the field’s ERROR messages, all of them in one transaction of their own. Every way a field’s transaction ends in a rollback writes them: a failure an @error channel claimed, an input a VALIDATION handler refused, an unexpected exception your clients see redacted, the field’s own success message failing to write, and a COMMIT the database refuses at the end (a deferred constraint, say). A failure no @error channel claimed still writes the message, with every $error key absent, so nothing internal reaches the event.
The guarantee is weaker than the success side’s, and has to be: the message is written at most once, after the rollback, and a process that dies between the rollback and the write loses it. A consumer sees failure messages from transactions that wrote nothing else.
Where the message is written depends on what the failure left of the field’s connection:
-
The connection survived the rollback, the common case: the message is written on it, with your session still mounted.
-
The transaction cost the connection: a tenant that timed out in a fanned-out part of the field, or the database ending the session over the transaction (on PostgreSQL
idle_in_transaction_session_timeout, andtransaction_timeoutfrom PostgreSQL 17; on Oracle a resource limit or a killed session). The old connection is aborted or evicted, and the message is written on a fresh connection graphitron acquires and mounts your session on for the purpose, then releases at once. Graphitron waits for that connection for at most the runtime’soutboxAcquisitionTimeout, five seconds unless you set it; past that the message is not written and the loss is logged, saying how much of the wait was spent queued for the executor and how much acquiring, so the next field is never held longer. -
The connection failed for a reason outside the transaction: the database is down or restarting, the network broke, a backend was terminated, no connection could be acquired. There is nothing to write on, and the lost message is logged with the field’s path, the event names and the cause. A
COMMITwhose answer was lost is in this case too, since it may have committed, and a failure message then would contradict the success messages that committed with it.
If your session mount fails, nothing is written, whether on the field’s own connection or on a fresh one, and the loss is logged: every owned statement runs on a mounted connection, and the mount may be what sets the database role or the row-level security context. If writing the messages fails, the failure is logged and the response is unchanged, since the field has already failed.
Failure messages are written on graphitron’s owned connections only. On the escape hatch (Graphitron.newGraphQL() with your own DSLContext) there is no point after the rollback where graphitron holds the connection, so the message is not written and a warning naming the field is logged. Under the rollback-everything dev mode (CommitPolicy.ROLLBACK_ONLY) the messages are written inside the operation’s transaction and discarded with everything else, on a fresh connection as on the field’s own.
An input a VALIDATION handler refuses is a failure like any other. The field answers with its typed error before it writes anything, the response is the same as without the message, and the field’s transaction rolls back and its ERROR messages are written. $error reads the first error the refusal produced: __typename is the @error type carrying the VALIDATION handler, and $error.message the message the client read for it.
Constraints
-
Valid on Mutation fields that write a table with
@mutation(INSERT,UPDATEorDELETE), and on Mutation fields a@serviceanswers; every other placement is a build error naming the field. -
An
ERRORmessage’s$errorkeys need the field to return a type with an errors field; without one, anERRORmessage carries arguments, session and scope only. -
On a
@servicefield, the field’s fetcher must be one graphitron generates from its model of the schema. A service taking a jOOQ record built from its input is not yet, and its@outboxis a build error naming why, until it is. -
Where the messages go is configured once, in the plugin’s
<outbox>block. A schema with an@outboxfield and no<outbox>block fails the build, naming the field.
What is yours, and what is the relay’s
The outbox makes a message exist exactly when its write commits. What happens after that is the relay’s and the consumers', and the mistakes outboxes are known for happen there:
-
Every message is delivered at least once. A relay that crashes after publishing and before recording it publishes again. Consumers must drop a message they have seen, by its id, which is why the build requires the table’s primary key.
-
Order holds per key, never globally. Two fields committing at once can publish in either order. Where order matters, have the relay key messages by something in the payload (
filmId), so one film’s events stay in order on one partition. -
The table is a conduit. Rows a relay has published can be deleted or archived; how long they stay is your relay’s setting, not graphitron’s.
-
A payload is a contract. Change it by adding a new
@outboxbeside the old one (see above), and put in it what a consumer needs, no more: the common payload goes on every message, so a secret or a personal detail there goes to everyone subscribed.
Relay outbox messages covers what a relay and its consumers see of failure messages.
See also
-
@mutationpicks the write the messages describe;@serviceruns a write of your own. -
Plugin configuration holds the
<outbox>block. -
Relay outbox messages: what a relay and its consumers see of success and failure messages.