Description
Summary (*)
We are using Magento as a headless backend for our front-facing application, we are using the GraphQL API to build the frontend.
We noticed if we added a product to cart, and that product later became out of stock, any further attempts to add items to cart will fail with Some cart items are out of stock
effectively locking up the cart from adding other items or changing other items quantities.
This is the returned GraphQL error:
"message": "Some of the products are out of stock.",
This is a breaking issue for us, as the alternative would result in a very poor experience for our users.
Currently, we have things set up that the frontend is able to detect whenever this exception is thrown and tries to amend it by removing the offending items and attempting to re-play whatever mutations are done to the cart since then.
Needless to say, this is a very flaky solution and it doesn't make sense to have this restriction in the first place since the REST API seems to be fine with this scenario.
Examples (*)
To Reproduce:
- Using the GraphQL API: Add an item to cart.
- Mark it as out-of-stock from the admin dashboard.
- Using the GraphQL API: Try to add other products to cart.
I have successfully reproduced this on a fresh magento environment
Proposed solution
Ideally, the GraphQL API should allow mutating the cart as long as the out-of-stock item is not being the target of the mutation (updating its quantity for example).
A slightly less robust solution is to allow the mutation to go through, but return errors
alongside the data
prompting the GQL client to update their UI and also be aware of possible problems.
GraphQL requests for reproducing
Create an empty cart
mutation {
createEmptyCart
}
Add product to cart
mutation {
addSimpleProductsToCart(
input: {
cart_id: "{ CART_ID }" # <--- cart id from the previous request goes here
cart_items: [
{
data: {
quantity: 1
sku: "simple-product" # <--- product sku goes here
}
}
]
}
) {
cart {
items {
id
product {
sku
stock_status
}
quantity
}
}
}