# What a mutation starts

Audience: Developer

What a mutation returns about the events and workflows it starts, and how to follow them up

## Overview

A mutation commits its change and returns. The events it produced reach the workflows after that, in the background, so the response does not say which workflows started. Instead, the response of most mutations carries two fields that let you find out:

- `transactionID` names the transaction of the mutation.
- `eventCount` is the number of events the mutation published.

With these you can ask the workflow service what became of each event, and which workflow executions it started.

## The two fields

Both fields are non-null and sit next to the changed record in the payload of the mutation, for example in `InventoryItemOutput`:

```graphql
type InventoryItemOutput {
  inventoryItem: InventoryItem
  transactionID: ID!
  eventCount: Int!
}
```

Most mutations of the inventory, picking and receiving services return them. In the management service, so do the mutations for locations, devices and device locations, `checkInUserDevice`, `checkOutUserDevice` and `sendCustomEvent`. Some mutations return the record itself and have neither field, for example `createCustomer`. The [API reference](https://docs.pyck.cloud/dev/api.md) lists the return type of each mutation.

### `transactionID`

- It identifies the database transaction the mutation ran in, and is stamped on every event the mutation published.
- When the server retries a transaction after a conflict, each attempt gets a new ID, and the response carries the ID of the attempt that committed.
- A retry that carries the same `Idempotency-Key` returns the stored response, and with it the same `transactionID`. See [HTTP headers](https://docs.pyck.cloud/dev/developers/headers.md).
- Workflows started from the events carry the ID in the `pyck_transaction_id` search attribute.

### `eventCount`

- It is the number of events the transaction has published when the field is resolved. The server fills it after the mutation resolver has written its events and before the transaction commits.
- It is `0` when the mutation published no event, for example because the request carries the `suppressevents` feature, see [HTTP headers](https://docs.pyck.cloud/dev/developers/headers.md).
- All root fields of one operation share one transaction and one counter. Each field reports the running total at the point where it resolves, so the last field of the operation holds the total.

## Find out what the events started

Two queries of the workflow service answer the follow-up questions.

### `transactionRouting`

[`transactionRouting(transactionID: ID!)`](https://docs.pyck.cloud/dev/api/queries/transactionrouting.md) returns what the router has done so far for the events of one transaction. It only returns events of your tenants.

`TransactionRouting` has `transactionID` and `entries`, one entry per event the router has settled, oldest first. Each entry has these fields:

| Field | Meaning |
| --- | --- |
| `eventID` | The ID of the event. |
| `tenantID` | The tenant of the event. |
| `outcome` | `DONE` when the router finished with the event: every target was delivered or refused for good. `GAVE_UP` when it stopped retrying after the last delivery, and the `FAILED` targets say what is left undone. |
| `sequence` | The position of the event in the event stream. |
| `targets` | What the router did for the event, a list of targets. It is empty when nothing was subscribed to the event: the event is finished with nothing to do. |
| `recordedAt` | When the router recorded the entry. |

Each target has a `kind`, and the fields that belong to it:

| `kind` | Meaning |
| --- | --- |
| `STARTED` | A workflow was started, or the start returned the run an earlier delivery of the event started. Has `workflow`, `workflowID` and `runID`. |
| `ALREADY_RUNNING` | The start was refused because the workflow ID is taken by another event's run. |
| `SIGNALLED` | A running execution was signalled. Has `signal`. |
| `DROPPED` | Nothing was delivered, for the `reason` given. |
| `FAILED` | The delivery failed with the `error` given. |

### Is the routing done?

Compare the number of `entries` with the `eventCount` of the mutation:

| `eventCount` | `entries` | Meaning |
| --- | --- | --- |
| `0` | any | Nothing was published, so there is nothing to route. |
| `n` | fewer than `n` | Routing is not complete. Ask again. |
| `n` | `n` | Routing is complete. Each entry says what happened. |

The status is best effort. A status write can fail and events can be skipped, so an entry can be missing for good. Ask again with a deadline instead of waiting until `entries` equals `eventCount`. Entries are kept for 72 hours.

### `workflowExecutions`

When routing is complete, [`workflowExecutions(where: {transactionID: ...})`](https://docs.pyck.cloud/dev/api/queries/workflowexecutions.md) lists the executions the transaction started. The filter matches the exact ID. An empty result means that the workflows have not started or are not visible yet, or that the mutation triggered none.

## Example

Create an item and keep the handle:

```graphql
mutation CreateItem($input: CreateInventoryItemInput!) {
  createInventoryItem(input: $input) {
    transactionID
    eventCount
    inventoryItem {
      id
      sku
    }
  }
}
```

Ask for the routing of that transaction, with the `transactionID` from the response:

```graphql
query Routing($transactionID: ID!) {
  transactionRouting(transactionID: $transactionID) {
    entries {
      eventID
      outcome
      targets {
        kind
        workflow
        workflowID
        runID
        reason
        error
      }
    }
  }
}
```

When the number of entries equals `eventCount`, list the executions:

```graphql
query Started($transactionID: ID!) {
  workflowExecutions(where: { transactionID: $transactionID }) {
    edges {
      node {
        execution {
          workflowId
          id
        }
        type {
          name
        }
        status
      }
    }
  }
}
```
