A task uses its preview to demonstrate what actions the task intends to generate. Among other purposes (see below), this is also how tasks request the Shopify permissions they require.
Mechanic generates a task preview by rendering the task code using a preview event, which resembles a live event that the task may see. The task is then responsible for rendering preview actions in response to the preview event, actions which are visually presented to the user and are analyzed by the platform, but are never actually performed.
A preview has three critical purposes:
Showing the user that the task will do what they expect it to do
Showing the task developer that the task code is functioning as intended
Showing the Mechanic platform what permissions the task requires
Core to the design of Mechanic is the idea that we can make it easy to make it easy – in this case, making it easy for developers to show their users what a Mechanic task can be expected to do.
By rendering preview actions, a task can prove to the user that it is interpreting their configuration as they intended. For example, by rendering a preview Email action, a task can show the user that their configured email content is appearing as expected inside the email body. This increases trust in the task, and allows users confidence in the task's outcome, even before the task processes a live event.
Developers may think of previews as a sort of test, using preview actions to prove that their task code is functioning as intended. A quality task will exercise all of its code in response to a preview event; doing so gives the developer instant feedback on task results, without actually having to run the task with a live event.
At the platform level, Mechanic uses previews to determine what permissions a task requires.
Mechanic gets this information from the actions that a task generates during preview, as well as from analysis of the Liquid lookups and GraphQL queries that a task uses during runtime.
For example, if a task renders a Shopify action containing a customerCreate mutation, Mechanic will prompt the user to grant access to the write_customers
Shopify OAuth scope. If Mechanic observes a task using shop.customers
, or observes the shopify filter receiving a customer-related GraphQL query, it will prompt for the read_customers
scope.
Some GraphQL mutations have multiple potential scope requirements, like tagsAdd or metafieldsSet. Because the requirements of these mutations hinge on their arguments, make sure that your preview actions are rendered with realistic ID strings (e.g. id: "gid://shopify/Product/12345"
). Mechanic will look for these IDs to determine what scopes to request.
Previews are generated using synthetic, temporary, non-persisted events – at least one for each event topic that the task subscribes to. These events are sourced from one of three places, in order of priority:
If the task defines its own preview event for a given topic, the preview will use the defined event;
Or, if the Mechanic account has a recent event with a matching topic on file, the preview will use data from that event;
Or, if the event topic is standard and known to Mechanic (i.e. not a part of the User domain), the preview will use illustrative example event data defined by the Mechanic platform.
A preview event is identical to a live event in all respects but one: it contains a preview
attribute, set to true
, identifying it as a preview event.
For live events, the preview
attribute does not exist. This means that event.preview == false
is not a valid way to detect a live event. Instead, use event.preview != true
, or event.preview == nil
.
{% if event.preview %}
{% log "This is a preview event, generated by Mechanic." %}
{% else %}
{% log "This is a live event, received by Shopify." %}
{% endif %}
{% if event.preview != true %}
{% log "This is a live event, received by Shopify." %}
{% else %}
{% log "This is a preview event, generated by Mechanic." %}
{% endif %}
A preview event's data is taken from the Mechanic account's event history, providing a realistic sample of the data a task can expect to see. (If the account history has no events for a given topic just yet, Mechanic will attempt to use anonymous sample event data of its own.)
A developer can choose between rendering static and dynamic preview actions. Static preview actions are hard-coded, written to appear whenever event.preview
is true. Dynamic preview actions are the result of the task code running normally, using event data in preview in the same way that it would use that event data with a live event. Because dynamic preview actions are the result of meaningfully exercising the task's code, they can provide a good indicator of how the task will behave with a live event. By contrast, static preview actions do not provide useful feedback on how a task is coded.
A static preview action is rendered in direct response to event.preview
. In general, it's better to use dynamic preview actions, but an understanding of both techniques is useful.
In the following example, a static preview action demonstrates that the task intends to tag incoming orders with "web". In actuality, the task's intent is to only tag orders that arrive via the Online Store channel; because the task can't be sure whether or not the preview event will contain such an order, a static preview action is used to ensure that a preview event always results in a tagging action.
Branching a task like this has two problems:
The actual condition of the task is not exercised during preview. The task will need to be tested with live orders from multiple channels, in order to verify that the task works properly.
Duplicating code makes it easier for one copy of the code to fall out of date. By using completely different code for the preview and live actions, it becomes easier for developers to forget to keep the two copies in sync as the task evolves.
{% if event.preview %}
{% action "shopify" %}
mutation {
tagsAdd(id: "gid://shopify/Order/1234567890", tags: "web") {
userErrors { field, message }
}
}
{% endaction %}
{% elsif order.source_name == "web" %}
{% action "shopify" %}
mutation {
tagsAdd(id: {{ order.admin_graphql_api_id | json }}, tags: "web") {
userErrors { field, message }
}
}
{% endaction %}
{% endif %}
shopify/orders/create
A dynamic preview action is the natural result of exercising a task's code as completely as possible, without adding any business logic that responds to event.preview
. Put another way, the idea is to make the preview (that appears during task editing) look as similar to a live event as possible.
{% if some_evaluated_condition %}
{% action "shopify" %}
mutation {
tagsAdd(id: {{ order.admin_graphql_api_id | json }}, tags: "web") {
userErrors { field, message }
}
}
{% endaction %}
{% endif %}
There are two techniques available for "steering" the task towards desired outcomes during preview.
Use defined preview events to control preview event data, without ever having to add preview-related code to the task itself. This is the cleanest way to control data provided by the event during preview.
Use stub data to dynamically swap in preview-friendly values. This is generally not necessary for preview event data, but may be necessary when querying Shopify for data during a task: because the Shopify API is disabled during preview, using stub data can be useful for swapping in realistic values that would be returned during a live run.
During task preview, Mechanic scans the task's subscriptions. For each event topic found, Mechanic constructs a synthetic preview event, resembling one that the task might encounter during live use.
By default, each preview event's data is sampled from previous events that the Mechanic account has seen, for the same topic.
However, developers may define their own preview events, containing whatever data the developer wishes to use for preview. This may be useful for several reasons:
Most tasks conditionally respond to events based on their data. Controlling the event data present during preview allows the developer to deterministically verify the results of the action.
Further, by deterministically/predictably generating actions, the developer can consistently demonstrate the permissions they need to Mechanic. (To learn more about this, see Previews.)
Defining preview event data is usually simpler than defining stub data.
Stubbing the event
variable (or any of the subject variables) removes any intelligence from the objects Mechanic generates from event data, a drawback avoided by defining a preview event and its data. Using the Order object as an example, a task may typically access its custom attributes via order.note_attributes.color
, or via order.note_attributes[0].value
. This dynamic behavior is lost if the event
variable is stubbed out, which can result in behaviors that are difficult to diagnose.
Multiple preview events may be defined per event topic. This allows developers to verify that their task renders the appropriate results under a variety of circumstances.
Defined preview events can be labeled with a description, which is visible in the task preview pane. This makes it easy to identify the scenario that a preview event is meant to represent.
Preview events may be defined using the "Edit preview events" button, in the task preview pane.
The configuration area for preview events contains a quickstart link for each event topic the task subscribes to, allowing developers to get started using sample data if the event topic is known to Mechanic. Or, the developer may start with a blank preview event definition, filling in whatever topic and data are useful.
A developer may define any number of preview events per topic. If no preview events are defined for a given topic, Mechanic will construct its own ad-hoc event during preview.
Displayed beneath the event topic in the preview pane, allowing the developer to distinguish one scenario from another.
Identifies the event definition to Mechanic, when Mechanic goes to construct preview events by topic.
Used to construct event.data
, and may be set to whatever values are useful in representing a specific scenario. The data structures used here should resemble what Mechanic will receive for a live event of the same topic.
Notably, the data here can be limited to just the properties that are useful. For example, while Mechanic might normally generate a complete payload for shopify/orders/create, the developer might only care about the "email"
property of the order – and so their defined preview event data might be limited to just that property. (Note that the inverse may not be true: defining preview event data for traversals into other objects, e.g. using preview event data to define a value for order.line_items[0].product.title
, will not work.)
For a trivial task, subscribing to shopify/customers/create, and having the following task code...
{% if customer.email contains "gmail.com" %}
{% log message: "got a gmail user!", email: customer.email %}
{% else %}
{% log message: "got someone else!", email: customer.email %}
{% endif %}
... we define two preview events, one which represents a Gmail user, and one which does not. This allows us to easily assert that the task behaves properly in both scenarios.
Preview event definitions are stored along with the task itself, and thus are present in the tasks version history (and, naturally, in task exports).
Because definitions are a part of the task itself, they're appropriate for use as a testing tool, allowing the developer to verify that a task behaves as intended at every stage of the task's development.
Stub data is hard-coded into a task, providing an unchanging source of data for previews. It is an important tool when generating dynamic preview actions. Stub data may be used for user-defined variables, but may also override environment variables as needed.
Most tasks make decisions based on the Liquid variables automatically provided, making it a common practice to stub them during preview mode. Any and all Liquid variables may be replaced by stub data, including event
and any event subject variables.
In simple cases, replacement objects may be constructed using the assign tag.
{% if event.preview %}
{% assign order = hash %}
{% assign order["source_name"] = "web" %}
{% assign order["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
{% endif %}
{% if order.source_name == "web" %}
{% action "shopify" %}
mutation {
tagsAdd(id: {{ order.admin_graphql_api_id | json }}, tags: "web") {
userErrors { field, message }
}
}
{% endaction %}
{% endif %}
It's also possible to construct this data using parse_json.
{% if event.preview %}
{% capture order_json %}
{
"source_name": "web",
"admin_graphql_api_id": "gid://shopify/Order/1234567890"
}
{% endcapture %}
{% assign order = order_json | parse_json %}
{% endif %}
{% if order.source_name == "web" %}
{% action "shopify" %}
mutation {
tagsAdd(id: {{ order.admin_graphql_api_id | json }}, tags: "web") {
userErrors { field, message }
}
}
{% endaction %}
{% endif %}
Mechanic makes GraphQL data available to tasks via the shopify filter. Mechanic observes the shopify filter in action during preview mode, using its inputs to inform Mechanic's knowledge of what permissions the task needs.
For this reason, it's important to allow the shopify filter to run normally, and construct stub data afterwards.
It can be useful to specify stub data using JSON, fed through the parse_json filter. Sample JSON is easy to generate using Shopify's GraphiQL app.
{% capture query %}
query {
publications(first: 250) {
edges {
node {
id
name
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"publications": {
"edges": [
{
"node": {
"id": "gid://shopify/Publication/69217648807",
"name": "Online Store"
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% log available_publications: result.data.publications %}
{% assign cursor = nil %}
{% assign total_inventory = 0 %}
{% for n in (0..100) %}
{% capture query %}
query {
orders(
first: 250
query: "status:open"
after: {{ cursor | json }}
) {
pageInfo { hasNextPage }
edges {
node { name, email }
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"orders": {
"pageInfo": {
"hasNextPage": false
},
"edges": [
{
"node": {
"name": "#1135",
"email": "isaac@example.com"
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for order_edge in result.data.orders.edges %}
{% assign order_node = order_edge.node %}
{% if order_node.email == blank %}
{% continue %}
{% endif %}
{% action "email" %}
{
"to": {{ order_node.email | json }},
"subject": {{ "We're still working on " | append: order_node.name | json }},
"body": "Thanks for your patience!"
}
{% endaction %}
{% endfor %}
{% if result.data.orders.pageInfo.hasNextPage %}
{% assign cursor = result.data.orders.edges.last.cursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}