# Flows

![](https://2037905333-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2DsGLcoxYyMrm5HF6D%2F-MBhbHgJ2EDHiprXiGOR%2F-MBhbZZmGihDkhzcYMr0%2FAsset%205%401500x-100.jpg?alt=media\&token=a3728dd8-da2a-44de-abcb-02ee958d95e2)

## Introduction

A flow contains a collection of resources which is executed when a endpoint gets triggered. If an error occurs during the execution of the flow are all rollbacks of executed calls called.

```bash
flow "todo" {
    resource "query" {
        request "service" "GetTodos" {}
    }
}
```

{% hint style="info" %}
Services are defined inside the schema definitions
{% endhint %}

You could expose flows by defining an endpoint. An endpoint defines the protocol to be used and protocol-specific options.

```bash
endpoint "todo" "http" {
    method = "GET"
    endpoint = "/todos"
}
```

## Resources

### Requests and rollbacks

A resource inside a flow could contain a request and/or a rollback. Requests are called when a flow is triggered. If any of the defined resources returns an unexpected error the rollbacks of all executed resources executed. Errors returned by rollbacks are ignored.

```bash
resource "create" {
    request "service" "CreateUser" {
        username = "{{ input:username }}"
    }

    rollback "service" "DeleteUser" {
        id = "{{ create:id }}"
    }
}
```

![sample SAGA pattern](https://2037905333-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2DsGLcoxYyMrm5HF6D%2F-M3Nuxs68yOI4g7mhQx_%2F-M3NvAUCMvFWwGFmZ767%2FScreenshot%202020-03-26%20at%2023.09.54.png?alt=media\&token=5cd1f2eb-eea6-410b-bce9-3b2e86455411)

#### Headers

Headers contain keys and values which define constants or references. Headers are passed to the called and send to the service. Headers could be defined in both requests and rollbacks.

```bash
input "schema.Object" {
    header = ["Authorization"]
}

resource "create" {
    request "service" "Authenticate" {
        header {
            Authorization = "{{ input.header:Authorization }}"
        }
    }
}
```

{% hint style="success" %}
When referencing header properties inside other resources remember that they have to be defined.
{% endhint %}

## Flow resource branches

Nodes (resources) are executed concurrently from one another. When a node is executed a check is performed to check whether the dependencies have been met. Only if all of the dependencies have been met is the node executed.

```css
+------------+
|            |
|    Node    +------------+
|            |            |
+------+-----+            |
       |                  |
       |                  |
+------v-----+     +------v-----+
|            |     |            |
|    Node    |     |    Node    |
|            |     |            |
+------+-----+     +------+-----+
       |                  |
       |                  |
+------v-----+            |
|            |            |
|    Node    <------------+
|            |
+------------+
```
