# Intents

> Name the actions in your domain with intents, declare which operations perform them, and call them by name

Source: https://taxilang.org/docs/language/intents

An intent names an action, like "purchase a product" or "cancel an order".
It says what the action takes and what it returns, but not which system does it.

```taxi
type ProductSku inherits String
type CustomerId inherits String
type Quantity inherits Int
type OrderId inherits String
type TotalPrice inherits Decimal

concept ProductRequest       { ProductSku, CustomerId, Quantity }
concept PurchaseConfirmation { OrderId, ProductSku, Quantity, TotalPrice }

[[ Purchase a product on behalf of a customer. ]]
write intent Purchase(request: ProductRequest): PurchaseConfirmation
```

Operations then declare that they `satisfy` the intent, and the compiler checks that they fit.
In a query, you can call the intent by name, without naming the service or operation.

For how intents fit with concepts and relationships, see the [overview](https://taxilang.org/docs/language/ontology).

## Declaring an intent

An intent looks like an operation without a service:

```taxi
type ProductSku inherits String
type CustomerId inherits String
type Quantity inherits Int
type OrderId inherits String

[[ Get a quote for a product. ]]
read intent Quote(ProductSku, CustomerId?): Quantity

[[ Cancel an order. ]]
write intent Cancel(OrderId)

[[ Empty the current basket. ]]
write intent ResetBasket()
```

- `read` or `write` is optional. If you add it, operations that satisfy the intent must match. See [Read and write](#read-and-write).
- Parameters can be semantic types, enums or concepts. They can be optional (`T?`) or collections (`T[]`).
- The return type is optional. If there is one, it can be a semantic type, an enum or a concept.
- Parameter names are optional. You only need them to refer to a parameter in a contract.

An intent isn't a type. You can't use it as a field type or a parameter.

### What an intent can't contain

- Models, as parameters (`INTENT_INPUT_INVALID_TYPE`) or as the return type (`INTENT_OUTPUT_INVALID_TYPE`). Use a concept instead.
- Two parameters of the same type, or where one type inherits from the other (`INTENT_DUPLICATE_INPUT_TYPE`). This applies even if the parameters have different names.
- Lambda parameters, varargs, default values, or constraints on parameters (`INTENT_UNSUPPORTED_PARAMETER_FORM`).

### Contracts

An intent can have a return contract, using the same syntax as [operation contracts](https://taxilang.org/docs/language/services#operation-contracts):

```taxi
type ProductSku inherits String
type CustomerId inherits String
type Quantity inherits Int
type OrderId inherits String

concept ProductRequest       { ProductSku, CustomerId, Quantity }
concept PurchaseConfirmation { OrderId, ProductSku, Quantity }

write intent Purchase(request: ProductRequest): PurchaseConfirmation( ProductSku == request::ProductSku )
```

The compiler doesn't compare an intent's contract with the contracts of the operations that satisfy it.

## Operations that satisfy an intent

An operation declares that it performs an intent with `satisfies`, after its return type:

```taxi
type ProductSku inherits String
type CustomerId inherits String
type Quantity inherits Int
type OrderId inherits String
type TotalPrice inherits Decimal
type Note inherits String

concept ProductRequest       { ProductSku, CustomerId, Quantity }
concept PurchaseConfirmation { OrderId, ProductSku, Quantity, TotalPrice }

write intent Purchase(request: ProductRequest): PurchaseConfirmation

model PurchaseResponse satisfies PurchaseConfirmation {
   orderId: OrderId
   sku: ProductSku
   quantity: Quantity
   total: TotalPrice
}

parameter model Order {
   sku: ProductSku
   customer: CustomerId
   note: Note?
}

service PurchasingApi {
   write operation doPurchase(Order, Quantity): PurchaseResponse satisfies Purchase
}
```

The compiler checks three things.

**The inputs fit.** Every parameter of the operation must be filled from the intent's inputs.
The intent's inputs here are the members of `ProductRequest`: `ProductSku`, `CustomerId` and `Quantity`.

- `Order` is a `parameter` model, so the compiler checks each of its required fields. `sku` and `customer` are in the inputs. `note` is optional, so it's skipped.
- `Quantity` is in the inputs.

The operation doesn't have to use every input.

**The output fits.** The intent returns a `PurchaseConfirmation`, and the operation returns `PurchaseResponse`, which satisfies it.

**The scope fits.** Both are `write`.

If a check fails, the compiler reports `INTENT_NOT_SATISFIED`, saying exactly what's missing.
For example, if the operation took a `CustomerPurchaseOrder` parameter model with a required `shipTo: ShippingAddress` field:

```
Operation PurchasingApi::doPurchase does not satisfy intent Purchase:
parameter CustomerPurchaseOrder requires ShippingAddress (field shipTo),
not present in Purchase's inputs (ProductRequest: ProductSku, CustomerId, Quantity)
```

An operation can satisfy several intents: `satisfies Purchase, Reorder`.
An operation can only satisfy intents. Anything else after `satisfies` is an error (`SATISFIES_NON_INTENT`).

> **Note: Satisfaction must be declared**
>
> An operation only performs an intent if it declares `satisfies`. The compiler never works this out from an operation's signature.

### How inputs are matched

An intent's inputs are its parameters. A concept parameter is expanded into its members, including members of nested concepts.

For each parameter of the operation:

| Operation parameter | Rule |
|---|---|
| A semantic type or enum | Exactly one input must match it |
| A `parameter` model | Each required field must match exactly one input. Optional fields are skipped. Nested models are checked the same way, unless they're `closed`. |
| Any other model | Can't be checked. See [Skipping the check](#skipping-the-check). |

An input matches if its type is the same as the parameter's type, or inherits from it. Cardinality has to fit too:

- A required input fills a required or optional parameter.
- An optional input only fills an optional parameter.
- A collection input only fills a collection parameter.

If two inputs could fill the same parameter, the check fails as ambiguous.

### How the output is matched

| Intent returns | The operation must return |
|---|---|
| A semantic type or enum, like `TotalPrice` | That type, or a model with exactly one field of that type |
| A collection, like `OrderId[]` | A collection of that type, or a model with one or more fields of that type, which are collected together |
| A concept, like `PurchaseConfirmation` | A model that declares it satisfies that concept |
| Nothing | Anything |

### Read and write

| Intent | Operation must be |
|---|---|
| `write intent` | `write` |
| `read intent` | `read`, or have no scope |
| `intent` (no scope) | Anything |

A mismatch is an error (`INTENT_SCOPE_MISMATCH`).

## Skipping the check

Some operations can't be checked. For example, an operation that takes a model that isn't a `parameter` model, or needs a value the intent doesn't provide because the server fills in a default.

For these, add `@UncheckedContract` with a reason:

```taxi
type ProductSku inherits String
type CustomerId inherits String
type Quantity inherits Int
type OrderId inherits String
type TotalPrice inherits Decimal
type ShippingAddress inherits String

concept ProductRequest       { ProductSku, CustomerId, Quantity }
concept PurchaseConfirmation { OrderId, ProductSku, Quantity, TotalPrice }

write intent Purchase(request: ProductRequest): PurchaseConfirmation

model PurchaseResponse satisfies PurchaseConfirmation {
   orderId: OrderId
   sku: ProductSku
   quantity: Quantity
   total: TotalPrice
}

parameter model CustomerPurchaseOrder {
   sku: ProductSku
   customer: CustomerId
   shipTo: ShippingAddress
}

service PurchasingApi {
   @UncheckedContract(reason = "Legacy API; ShippingAddress is defaulted server-side")
   write operation doPurchase(CustomerPurchaseOrder, Quantity): PurchaseResponse satisfies Purchase
}
```

This turns off the input and output checks for that operation. The compiler still:

- checks `read` and `write`
- warns once for each intent it doesn't check (`UNCHECKED_CONTRACT_WARNING`), so it's visible in your build

The rules for `@UncheckedContract`:

- `reason` is required and can't be blank (`UNCHECKED_CONTRACT_REASON_REQUIRED`).
- It's only valid on an operation that declares `satisfies` (`UNCHECKED_CONTRACT_WITHOUT_SATISFIES`).

## Calling an intent

In a query, `call` an intent by name, rather than naming a service and operation:

```taxi
type ProductSku inherits String
type OrderId inherits String

model Confirmation {
   orderId: OrderId
}

write intent Purchase(ProductSku): OrderId

service PurchasingApi {
   write operation buy(ProductSku): Confirmation satisfies Purchase
}
```

```taxi
given { ProductSku = 'ABC-123' }
call Purchase
```

The compiler finds the operation that satisfies `Purchase`, and compiles the query as if you had written `call PurchasingApi::buy`.
The result is the operation's return type, here `Confirmation`. You can project it with `as`:

```taxi
given { ProductSku = 'ABC-123' }
call Purchase as {
   id: OrderId
}
```

The compiler reports:

- `QUERY_INTENT_NO_PROVIDER` if no operation satisfies the intent.
- `QUERY_INTENT_AMBIGUOUS` if more than one operation does. The message lists them. To choose one, call it directly with `call Service::operation`.
- `QUERY_CALL_TARGET_NOT_INTENT` if the name after `call` isn't an intent or a `Service::operation`.

As with any `call`, the operation must be a `write` operation. So in practice, only `write` intents can be called.

Intents only add a name you can `call`. They don't change how `find` works: `find { OrderId }` finds data exactly as it did before.

> **Warning: Current limits**

## Next steps

- [Concepts](https://taxilang.org/docs/language/concepts): declaring the concepts that intents take and return
- [Mutations](https://taxilang.org/docs/taxiql/mutations): more on `call` and write operations
