Hi, I’m trying to wrap my head around the best way to call a Mutation graphql query for Shopify Fulfillment Orders.
I have a record that looks like this:
record {
"trackingNo": "trackinno",
"carrier": "carrier xyz",
"fulfillmentOrders": [
{
"id": "gid://shopify/FulfillmentOrder/78841931234234",
"lineItems": [
{
"id" : "gid://shopify/FulfillmentOrderLineItem/1709859675423",
"quantity": 1
},
{
"id" : "gid://shopify/FulfillmentOrderLineItem/1709859675423",
"quantity": 1
}
]
}
]
}
I decided to use the Shopify GraphQL step with "One to many" yes, path to many = fulfillmentOrders
I'm able to use the {{#each}} block helper to get the line Items like the mutation requires, but I'm having a hard time adding a comma between them, because I need a condition to determine if the array is larger than 1 or not... because if the array has only 1 element, a comma is not needed.
I don't know if a mutation with multiple array elements should be written all using a Handlebar template, or if I should use some of the premap, postmap hooks for that.... Or can I even use mapping for GraphQL steps?
Thanks for any insight!
Right where I would need a comma, I typically put {{^if @last}},{{/if}}. This means if where you are in the loop is not the last item, then use a comma.
That being said, the beauty of GraphQL is you don't need to use the one-to-many. Since you are able to send multiple mutations to Shopify in one API call, you could easily just put another each loop ({{#each record.fulfillmentOrders}}{{/each}}) in your mutation so then all mutations for the same order are sent at once. This would be beneficial if you are running up against API rate limits. It would not be beneficial for error management, though. If you send multiple mutations and one of them fails, then it would be harder to manage that in Celigo since retrying that error would actually retry all fulfillmentOrders since we don't know which failed vs worked already. Given that, I'd stick to one-to-many so that error management works perfectly.
1 Like
Thank you for the insight Tyler.... While waiting for this answer, I actually also found another solution using the @index keyword:
{{#if @index}}, {{/if}}
From what I understood, @index will return the iteration index inside the Each loop, and if it's 0, it #if will return "false".
Re: your suggestion, in the {{^if ...}} handlebar template, does the caret sign (^) provide some sort of "negation" directive?
Yeah {{#if @index}}, {{/if}} would work as long as you put it at the beginning of your loop since a 0 index would evaluate to false and anything else would evaluate to true, thus adding a comma at the beginning of each loop except the first one.
Also, yes, the ^ is the negative direction which is why my suggestion also works, though it needs to be placed at the end of your loop, just before the {{/each}}.
2 Likes