Demonstration: Generating a file and uploading it to Shopify
This task demonstrates how one might use the Files action to generate a file, using the fileCreate GraphQL mutation to transfer the resulting file to Shopify for storage.
Try this taskWhen it runs
- Occurs when a user manually triggers the task
- Occurs when a Mechanic action is performed
Task code and subscriptions
You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.
Subscriptions
mechanic/user/trigger
mechanic/actions/perform
How task subscriptions work
Task code
{% if event.topic == "mechanic/user/trigger" %}
{% action "files" %}
{
"invoice.pdf": {
"pdf": {
"html": "<h1>Order #12345</h1>\n<p>It's due!</p>"
}
}
}
{% endaction %}
{% elsif event.topic == "mechanic/actions/perform" %}
{% if event.preview %}
{% capture action_json %}
{
"type": "files",
"run": {
"result": {
"invoice.pdf": {
"url": "https://usemechanic.com/file/0000/invoice.pdf"
}
}
}
}
{% endcapture %}
{% assign action = action_json | parse_json %}
{% endif %}
{% if action.type == "files" %}
{% action "shopify" %}
mutation {
fileCreate(
files: [
{
originalSource: {{ action.run.result["invoice.pdf"].url | json }}
}
]
) {
files {
fileStatus
fileErrors {
code
details
message
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% endif %}
{% endif %}
Mechanic Liquid reference