A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
Hello,
Never used GraphQL before and possible doing something wrong. I'm trying to adjust inventory quantity in Shopify from an external source but the quantity does not chanes.
Getting product GID:
{
productVariants(query: "barcode:4548482860270", first: 10) {
edges {
node {
id
inventoryQuantity
inventoryItem {
inventoryLevels(first: 10) {
edges {
node {
location {
id
}
}
}
}
}
}
}
}
}
here is the result:
{
"data": {
"productVariantUpdate": {
"product": {
"id": "gid://shopify/Product/5831589232806"
},
"productVariant": {
"id": "gid://shopify/ProductVariant/36633914146982",
"inventoryQuantity": 8
},
"userErrors": []
}
},
"extensions": {
"cost": {
"requestedQueryCost": 10,
"actualQueryCost": 10,
"throttleStatus": {
"maximumAvailable": 1000,
"currentlyAvailable": 990,
"restoreRate": 50
}
}
}
}
Then trying to update quantity:
mutation {
productVariantUpdate(input: {
id: "gid://shopify/ProductVariant/36633914146982",
price: 120
inventoryQuantities:[{
availableQuantity: 10
locationId: "gid://shopify/Location/56015257766"
}]
}) {
product {
id
}
productVariant {
id
inventoryQuantity
price
}
userErrors {
field
message
}
}
}
The price was changed to 120 but the quantity is still 8:
{
"data": {
"productVariantUpdate": {
"product": {
"id": "gid://shopify/Product/5831589232806"
},
"productVariant": {
"id": "gid://shopify/ProductVariant/36633914146982",
"inventoryQuantity": 8,
"price": "120.00"
},
"userErrors": []
}
},
"extensions": {
"cost": {
"requestedQueryCost": 10,
"actualQueryCost": 10,
"throttleStatus": {
"maximumAvailable": 1000,
"currentlyAvailable": 990,
"restoreRate": 50
}
}
}
}
Also is it possible to search item by barcode and update qty/price in a single mutation?
I think that is the wrong mutation. The inventory cost field is available, but for quantity you need to use an inventory adjusment call.
https://shopify.dev/docs/admin-api/graphql/reference/mutation/inventoryadjustquantity
Like all things new, this takes some getting used to, what you can and cannot do, and how to do it. I have been updating inventory on 200,000 SKUs in bulk jobs and still marvel at how savage all this process is.
So in reality I need to make 3 queries: one to get ProductVariant GID, InventoryLevel GID and available quantity, second one to update price and last one to adjust quantity available e.g. I need to have 6 qty total, first query returned me 2 available so I'm adjusting by formula 6-2=4?
I believe Shopify does accept a "delta" change now more so than a fixed number. The problem with assigning fixed numbers was revealed years ago, as it can skew the results. So when you have 4 new items to add to your inventory, you are not supposed to figure out the new total yourself, 6. Instead you assign a +4 and let Shopify worry about how that impacts available inventory. Usually one knows how many new units they are adding (or subtracting), so the delta change is the way to go.