Inject State

Parameter
Description
Type
Required

name

Unique State name. Must follow the Serverless Workflow Naming Convention

string

yes

type

State type

string

yes

data

JSON object which can be set as state's data input and can be manipulated via filter

object

yes

stateDataFilter

State data filter

object

no

transition

Next transition of the workflow after injection has completed

string or object

yes (if end is not defined)

compensatedBy

Unique name of a workflow state which is responsible for compensation of this state

string

no

usedForCompensation

If true, this state is used to compensate another state. Default is false

boolean

no

metadata

Metadata information

object

no

end

Is this state an end state

boolean or object

yes (if transition is not defined)

JSON
YAML
{
     "name":"hello",
     "type":"inject",
     "data": {
        "result": "Hello"
     },
     "transition": "world"
}
name: hello
type: inject
data:
  result: Hello
transition: world

Inject state can be used to inject static data into state data input. Inject state does not perform any actions. It is very useful for debugging, for example, as you can test/simulate workflow execution with pre-set data that would typically be dynamic in nature (e.g., function calls, events).

The inject state data property allows you to statically define a JSON object which gets added to the states data input. You can use the filter property to control the states data output to the transition state.

Here is a typical example of how to use the inject state to add static data into its states data input, which then is passed as data output to the transition state:

JSON
YAML
{
 "name":"simple-inject-state",
 "type":"inject",
 "data": {
    "person": {
      "fname": "John",
      "lname": "Doe",
      "address": "1234 SomeStreet",
      "age": 40
    }
 },
 "transition": "greet-person-state"
}
  name: simple-inject-state
  type: inject
  data:
    person:
      fname: John
      lname: Doe
      address: 1234 SomeStreet
      age: 40
  transition: greet-person-state

The data output of the "SimpleInjectState" which then is passed as input to the transition state would be:

{
 "person": {
      "fname": "John",
      "lname": "Doe",
      "address": "1234 SomeStreet",
      "age": 40
 }
}

If the inject state already receives a data input from the previous transition state, the inject data should be merged with its data input.

You can also use the filter property to filter the state data after data is injected. Let's say we have:

JSON
YAML
  {
     "name":"simple-inject-state",
     "type":"inject",
     "data": {
        "people": [
          {
             "fname": "John",
             "lname": "Doe",
             "address": "1234 SomeStreet",
             "age": 40
          },
          {
             "fname": "Marry",
             "lname": "Allice",
             "address": "1234 SomeStreet",
             "age": 25
          },
          {
             "fname": "Kelly",
             "lname": "Mill",
             "address": "1234 SomeStreet",
             "age": 30
          }
        ]
     },
     "stateDataFilter": {
        "output": "${ {people: [.people[] | select(.age < 40)]} }"
     },
     "transition": "greet-person-state"
    }
  name: simple-inject-state
  type: inject
  data:
    people:
    - fname: John
      lname: Doe
      address: 1234 SomeStreet
      age: 40
    - fname: Marry
      lname: Allice
      address: 1234 SomeStreet
      age: 25
    - fname: Kelly
      lname: Mill
      address: 1234 SomeStreet
      age: 30
  stateDataFilter:
    output: "${ {people: [.people[] | select(.age < 40)]} }"
  transition: greet-person-state

In which case the states data output would include only people whose age is less than 40:

{
  "people": [
    {
      "fname": "Marry",
      "lname": "Allice",
      "address": "1234 SomeStreet",
      "age": 25
    },
    {
      "fname": "Kelly",
      "lname": "Mill",
      "address": "1234 SomeStreet",
      "age": 30
    }
  ]
}

You can change your output path easily during testing, for example change the expression to:

${ {people: [.people[] | select(.age >= 40)]} }

This allows you to test if your workflow behaves properly for cases when there are people whose age is greater or equal 40.

Note that transition and end properties are mutually exclusive, meaning that you cannot define both of them at the same time.

Last updated