Setting up an Event-based Trigger

Triggered based on the user defined event such as Tailor DB record creation EventType: "tailordb.type_record.created".

Tutorial steps

To create an event based trigger, you'll need to:

  1. Activate the Executor service
  2. Add the trigger in executor configuration file
  3. Enable PublishRecordEvents settings
  4. Apply the change
  5. Verify the trigger

1. Activate the Executor service

To activate the Executor service, add an executor.tf file to the root folder of the application. Define the trigger as shown below.

executor.tf

resource "tailor_executor" "webhook_starship" {
  workspace_id = tailor_workspace.ims.id
  name         = "<trigger-name>"

  trigger = {
	event = {}
  }

  operation = {}
}

Learn more about executor properties in the Tailor Platform Provider documentation.

To activate the Executor service, add executor service to the list of services in the workspace.cue file as the following.

workspace.cue

package v2

import (
	...
	"tailor.build/template/services/executor"
)

v2.#Workspace & {
	Apps: [
		application
	]
	Services: [tailordb, pipeline, auth, executor]
}

2. Add the trigger in executor configuration file

executor.tf

resource "tailor_executor" "event_based_executor" {
  workspace_id = tailor_workspace.ims.id
  name         = "event-based-executor"
  description  = "Execute query based on an event"

  trigger = {
    event = {
  	type      = "tailordb.type_record.created"
  	condition = <<EOF
  	  args.namespaceName == "ims" && args.typeName == "Category"
  	EOF
    }
  }

  operation = {
    tailor_graphql = {
  	app_name = tailor_application.ims.name
  	invoker = {
  	  event_user = true
  	}
  	query = <<EOF
  	  mutation createProduct($categoryID: ID!, $title: String!) {
  		createProduct(input: {
  		  categoryID: $categoryID
  		  title: $title
  		}) {
  		  id
  		}
  	  }
  	EOF
  	variables = <<EOF
  	  ({
  		"categoryID": args.newRecord.id,
  		"title": args.newRecord.name + "Product"
  	  })
  	EOF
    }
  }
}

To add the triggerCreateShipmentWithEvent trigger, define the trigger in the executor.cue manifest file.

executor.cue

package executor

import (
	"github.com/tailor-platform/tailorctl/schema/v2/executor"
	"github.com/tailor-platform/tailorctl/schema/v2/common"
	"tailor.build/template/environment"
)

executor.#Spec & {
	Executors: [
		#triggerCreateShipmentWithEvent,
	]
}

#triggerCreateShipmentWithEvent: executor.#Executor & {
	Name:        "eventbased-executor"
	Description: "Create a data based on the event"
	Trigger: executor.#TriggerEvent & {
		EventType: "tailordb.type_record.created"
		Condition: common.#Script & {
			Expr: """
				args.namespaceName == "my-tailordb" && args.typeName == "SalesOrder"
				"""
		}
	}
	Target: executor.#TargetTailorGraphql & {
		AppName: "my-app"
		Query: """
		mutation ($input: createShipmentFromSalesOrderInput!) {
			createShipmentFromSalesOrder(input: $input)
		}"""
		Variables: common.#Script & {
			Expr: """
			({
				"input": {
					"markAsShipped": true,
					"salesOrderID": args.newRecord.id,
				}
			})"""
		}
	}
}

3. Enable PublishRecordEvents settings

To ensure that a Product record is created through the trigger for every Category type record created, you'll need to enable PublishRecordEvents in the settings.

tailordb_category.tf

resource "tailor_tailordb_type" "category" {
  workspace_id = tailor_workspace.ims.id
  namespace    = tailor_tailordb.ims.namespace
  name         = "Category"
  description  = "Contains information about categories of products."

  settings = {
	publish_record_events = true,
  }

  fields = {
	...
  }

  type_permission = local.permission_everyone
}

To ensure that a PutAway record is created through the trigger for every PurchaseOrder type record created, you'll need to enable PublishRecordEvents in the settings.

salesOrder.cue

package transaction

import (
	"github.com/tailor-platform/tailorctl/schema/v2/tailordb"
	"tailor.build/template/services/tailordb:permissions"
)

SalesOrder: tailordb.#Type & {
	Name:        "SalesOrder"
	Description: "SalesOrder model."
	Settings: {
		PublishRecordEvents: true
	}
	Fields: {
		...
	}

	TypePermission: permissions.editorAccess
}

4. Apply the change

Run the following command to apply the changes.

terraform apply
# Apply Executor
tailorctl workspace apply -m ./workspace.cue 

5. Verify the trigger

Click on the application and select Executors from the navigation to view the created executor in the Console.

Tailor Console Executors Overview

View the executor details by clicking the Open Details button in the Details column of the overview table.

Tailor Console Executors Details

In the console, select GraphQL Playground to create a new sales order.

GraphQL

mutation {
  createSalesOrder(input:{ orderNumber: 987 }) {
    id
  }
}

After executing the query, view the job created for that executor by selecting the Jobs tab and choosing the executor from the dropdown menu.

Tailor Console Executor Jobs

Clicking Open Attempts displays details of job execution attempts made by the Executor. When the Target execution fails with a status code other than 2xx, the Tailor Platform retries the job execution. However, retries are not performed for certain errors, such as incorrect executor configuration or script errors.

Upon successful completion of the job, a new Shipment record is created.

Tailor Console Executor Jobs