Input
Input task is triggered manually by the user. Before it runs, it will ask you to upload a file. Later this file is available in code editor. This means you can easily access rows of your file and perform certain operations.
Looping over file rows is simple:
{% for row in file %}
{{ row["Your Column Name"] | log }}
{% endof %}
Above snippet is going log each row value for column named Your Column Name
.
Code editor is going to have file
object preloaded. This object is an array with JSON objects. Each JSON object has properties named exactly as columns in your source file.
{% capture mutation %}
mutation customerCreate($input: CustomerInput!) {
customerCreate(input: $input) {
customer {
id
}
userErrors {
field
message
}
}
}
{% endcapture %}
{% json variables %}
{
"input": {
"email": ""
}
}
{% endjson %}
{% for row in file %}
{% assign variables.input.email = row['email'] %}
{% assign result = mutation | graphql: variables %}
{% if result.customerCreate.userErrors != empty %}
{{ result.customerCreate.userErrors[0] | log }}
{% else %}
{{ "Created new customer with id: " | append: result.customerCreate.customer.id | log }}
{% endif %}
{% endfor %}
Next, in lines 1-13 we capture GraphQL mutation for creating a customer.
Lines 15-21 define variables required for GraphQL mutation.
Next we use for
loop to go through the file rows and for each row assign email to variables.input.email
.
Having mutation input ready we execute GraphQL query and create our customer.
Last updated