Re: Example for inventorybulkadjustquantityatlocation with GraphQL

Solved

Example for inventorybulkadjustquantityatlocation with GraphQL

friendoftheroot
Shopify Partner
49 5 8

Hello,

I have a functionality in restapi to update my stock. Works well but very slow.
I want to transition to use GraphQL to update my inventory.

Using inventorybulkadjustquantityatlocation (https://shopify.dev/api/admin-graphql/2021-10/mutations/inventorybulkadjustquantityatlocation) I can set individually very well.

But I cannot get the formatting correct to send multiple updates in one query.

Here is my $postdata variable that I send via Curl:

 

 

{"query":"mutation inventoryBulkAdjustQuantityAtLocation($inventoryItemAdjustments: [InventoryAdjustItemInput!]!, $locationId: ID!)
{ inventoryBulkAdjustQuantityAtLocation(inventoryItemAdjustments: $inventoryItemAdjustments, locationId: $locationId)
{userErrors { field message} }}",

"variables":{"inventoryItemAdjustments":[{"availableDelta":16,"inventoryItemId":"gid://shopify/InventoryItem/43028674117838"},{"availableDelta":3,"inventoryItemId":"gid://shopify/InventoryItem/43028674150606"},{"availableDelta":2,"inventoryItemId":"gid://shopify/InventoryItem/43028674183374"},{"availableDelta":10,"inventoryItemId":"gid://shopify/InventoryItem/43028676018382"}],"locationId":"gid://shopify/Location/62241177806"}}

 

 

 
I must be missing something simple here and maybe hand over the array of values incorrect in the json.
What is the correct way to send the variables? 

Please advice.

Accepted Solution (1)
CameronMZB
Shopify Partner
9 3 3

This is an accepted solution.

I haven't tried passing locationId in with the variables, so my first example has it hard coded; I use something like this.

 

--header 'X-Shopify-Access-Token: {your api admin token}' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"mutation BulkInventory( $invItemAdjustments: [InventoryAdjustItemInput!]!) { inventoryBulkAdjustQuantityAtLocation( inventoryItemAdjustments: $invItemAdjustments, locationId: \"gid://shopify/Location/62241177806\") { inventoryLevels { id available item { id sku } location { id name } } userErrors { field message } }}","variables":{"invItemAdjustments":[{"inventoryItemId":"gid://shopify/InventoryItem/43028674117838","availableDelta":16},{"inventoryItemId":"gid://shopify/InventoryItem/43028674150606","availableDelta":3},{"inventoryItemId":"gid://shopify/InventoryItem/43028674183374","availableDelta":2},{"inventoryItemId":"gid://shopify/InventoryItem/43028676018382","availableDelta":10}]}}'
 
 
This one should work, but I haven't tested it:

--header 'X-Shopify-Access-Token: {your api admin token}' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"mutation BulkInventory( $invItemAdjustments: [InventoryAdjustItemInput!]!, $locationId: ID!) { inventoryBulkAdjustQuantityAtLocation( inventoryItemAdjustments: $invItemAdjustments, locationId: $locationId) { inventoryLevels { id available item { id sku } location { id name } } userErrors { field message } }}","variables":{"invItemAdjustments":[{"inventoryItemId":"gid://shopify/InventoryItem/43028674117838","availableDelta":16},{"inventoryItemId":"gid://shopify/InventoryItem/43028674150606","availableDelta":3},{"inventoryItemId":"gid://shopify/InventoryItem/43028674183374","availableDelta":2},{"inventoryItemId":"gid://shopify/InventoryItem/43028676018382","availableDelta":10}],"locationId":"gid://shopify/Location/62241177806"}}'

View solution in original post

Replies 5 (5)

friendoftheroot
Shopify Partner
49 5 8

Guy, please have a look into this. I have been researching on and off and people only post a graphql to update one single product inventory at a time.

It would be needed to do it like in the example properly with 40-100 at a time.

What is the correct formatting. I'd be glad to have a json example please.

CameronMZB
Shopify Partner
9 3 3

This is an accepted solution.

I haven't tried passing locationId in with the variables, so my first example has it hard coded; I use something like this.

 

--header 'X-Shopify-Access-Token: {your api admin token}' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"mutation BulkInventory( $invItemAdjustments: [InventoryAdjustItemInput!]!) { inventoryBulkAdjustQuantityAtLocation( inventoryItemAdjustments: $invItemAdjustments, locationId: \"gid://shopify/Location/62241177806\") { inventoryLevels { id available item { id sku } location { id name } } userErrors { field message } }}","variables":{"invItemAdjustments":[{"inventoryItemId":"gid://shopify/InventoryItem/43028674117838","availableDelta":16},{"inventoryItemId":"gid://shopify/InventoryItem/43028674150606","availableDelta":3},{"inventoryItemId":"gid://shopify/InventoryItem/43028674183374","availableDelta":2},{"inventoryItemId":"gid://shopify/InventoryItem/43028676018382","availableDelta":10}]}}'
 
 
This one should work, but I haven't tested it:

--header 'X-Shopify-Access-Token: {your api admin token}' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"mutation BulkInventory( $invItemAdjustments: [InventoryAdjustItemInput!]!, $locationId: ID!) { inventoryBulkAdjustQuantityAtLocation( inventoryItemAdjustments: $invItemAdjustments, locationId: $locationId) { inventoryLevels { id available item { id sku } location { id name } } userErrors { field message } }}","variables":{"invItemAdjustments":[{"inventoryItemId":"gid://shopify/InventoryItem/43028674117838","availableDelta":16},{"inventoryItemId":"gid://shopify/InventoryItem/43028674150606","availableDelta":3},{"inventoryItemId":"gid://shopify/InventoryItem/43028674183374","availableDelta":2},{"inventoryItemId":"gid://shopify/InventoryItem/43028676018382","availableDelta":10}],"locationId":"gid://shopify/Location/62241177806"}}'
friendoftheroot
Shopify Partner
49 5 8

Thank you for taking the effort and giving a great answer.
It is very helpful that you showed versions with the location global or for each row. 🙂

I have troubles to check if this works with the graphql app.
See this screenshot: https://ibb.co/d2vy6V8

I used this header:
mutation inventoryBulkAdjustQuantityAtLocation($inventoryItemAdjustments: [InventoryAdjustItemInput!]!, $locationId: ID!) {
inventoryBulkAdjustQuantityAtLocation(inventoryItemAdjustments: $inventoryItemAdjustments, locationId: $locationId) {
userErrors {
field
message
}
}
}



I used these variables:
{
"variables":{
"invItemAdjustments":[
{
"inventoryItemId":"gid://shopify/InventoryItem/43028674117838",
"availableDelta":16
},
{
"inventoryItemId":"gid://shopify/InventoryItem/43028674150606",
"availableDelta":3
},
{
"inventoryItemId":"gid://shopify/InventoryItem/43028674183374",
"availableDelta":2
},
{
"inventoryItemId":"gid://shopify/InventoryItem/43028676018382",
"availableDelta":10
}
],
"locationId":"gid://shopify/Location/62241177806"
}
}

CameronMZB
Shopify Partner
9 3 3

In the graphiql app, you have to go remove the outer layer; putting data in the variable pane wraps the text { variable }. So when testing there, you'd want something like this:

{
"invItemAdjustments":[
{
"inventoryItemId":"gid://shopify/InventoryItem/43028674117838",
"availableDelta":16
},
{
"inventoryItemId":"gid://shopify/InventoryItem/43028674150606",
"availableDelta":3
},
{
"inventoryItemId":"gid://shopify/InventoryItem/43028674183374",
"availableDelta":2
},
{
"inventoryItemId":"gid://shopify/InventoryItem/43028676018382",
"availableDelta":10
}
],
"locationId":"gid://shopify/Location/62241177806"
}

friendoftheroot
Shopify Partner
49 5 8

This works well now. I came down from 10min to 1.5min for 10.000 lines. Thanks.
Would be good to hand over more than 200 at a time. But it is what it is.