# Workflow Library

## Introduction

Library provides development kit for workflows.

## Sources

<https://github.com/milaboratory/platforma/blob/main/sdk/workflow-tengo/src/workflow/index.lib.tengo>

## API

### Import

```go
wf := import("@platforma-sdk/workflow-tengo:workflow")
```

### `body`

Defines main entry point for the current workflow.

#### Arguments:

- `bodyFn`: `func(args): {outputs: map, exports: map}` - a function that takes all arguments passed from UI and model and returns a map with outputs and exports. Outputs will be passed to model, and exports will be passed to the result pool.

#### Example:

```go
wf.body(func(args) {
    return {
        outputs: {
            message: "Hello, " + args.name + "!"
        },
        exports: {}
    }
})
```

### `setPreRun`

Defines an entry point for a current workflow. Pre-run will run every time a chain of blocks that comes before this block changes. For example, it can be used to call a program and load a list of settings.

#### Arguments:

- `template` - a pre-run template that can be imported using `assets.importTemplate` function.

#### Example:

```go
assets := import("@platforma-sdk/workflow-tengo:assets")
wf.setPreRun(assets.importTemplate(":prerun"))
```

### `resolve`

Query results pool to find spec & data for a given reference.

#### Arguments:

- `ref`: a ref object (`{blockId: string, name: string}`)

- `options?`: `optional map` - an optional map of options. The options:

  - `spec`: `boolean, default: true` - Return specs in the results
  - `data`: `boolean, default: true` - Return data in the results
  - `single`: `boolean, default: false` - Asserts that the resulting list has one element and returns it
  - `first`: `boolean, default: false` - Returns first available result of the query
  - `errIfMissing`: `boolean, default: false` - Throw error if no results found

#### Return:

- `result`: a `{ref: spec: data:}` object

#### Example:

```go
resolved := wf.resolve(ref)
spec := resolved.spec
data := resolved.data
```

### `prepare`

Prepare additional resources for the workflow body. The body function of the workflow will be executed only when the references will be returned by the prepare method will be resolved into a ready resources. The resulting resources will be passed as an input to the body function.

#### Arguments:

- `cb`: `func(args): map` - a callback function returning a map of references to be resolved

#### Example:

We need to resolve the ref into the actual sequence set resource in order to use it in the body:

```go
wf.prepare(func(args) {
    return {
        resolvedRef: wf.resolve(args.ref, { errIfMissing: true })
    }
})
```

Source: https://docs.platforma.bio/sdk-workflow-lib/
