1111use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
1212use Magento \Framework \GraphQl \Query \ResolverInterface ;
1313use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
14+ use Magento \Framework \Lock \LockManagerInterface ;
1415use Magento \GraphQl \Model \Query \ContextInterface ;
1516use Magento \Quote \Model \Cart \AddProductsToCart as AddProductsToCartService ;
1617use Magento \Quote \Model \Cart \Data \AddProductsToCartOutput ;
@@ -41,19 +42,27 @@ class AddProductsToCart implements ResolverInterface
4142 */
4243 private $ itemDataProcessor ;
4344
45+ /**
46+ * @var LockManagerInterface
47+ */
48+ private $ lockManager ;
49+
4450 /**
4551 * @param GetCartForUser $getCartForUser
4652 * @param AddProductsToCartService $addProductsToCart
47- * @param ItemDataProcessorInterface $itemDataProcessor
53+ * @param ItemDataProcessorInterface $itemDataProcessor
54+ * @param LockManagerInterface $lockManager
4855 */
4956 public function __construct (
5057 GetCartForUser $ getCartForUser ,
5158 AddProductsToCartService $ addProductsToCart ,
52- ItemDataProcessorInterface $ itemDataProcessor
59+ ItemDataProcessorInterface $ itemDataProcessor ,
60+ LockManagerInterface $ lockManager
5361 ) {
5462 $ this ->getCartForUser = $ getCartForUser ;
5563 $ this ->addProductsToCartService = $ addProductsToCart ;
5664 $ this ->itemDataProcessor = $ itemDataProcessor ;
65+ $ this ->lockManager = $ lockManager ;
5766 }
5867
5968 /**
@@ -72,6 +81,12 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7281 $ maskedCartId = $ args ['cartId ' ];
7382 $ cartItemsData = $ args ['cartItems ' ];
7483 $ storeId = (int )$ context ->getExtensionAttributes ()->getStore ()->getId ();
84+ $ lockName = 'cart_processing_lock_ ' . $ maskedCartId ;
85+ while ($ this ->lockManager ->isLocked ($ lockName )) {
86+ // wait till other process working with the same cart complete
87+ usleep (rand (100 , 600 ));
88+ }
89+ $ this ->lockManager ->lock ($ lockName , 1 );
7590
7691 // Shopping Cart validation
7792 $ this ->getCartForUser ->execute ($ maskedCartId , $ context ->getUserId (), $ storeId );
@@ -86,6 +101,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
86101
87102 /** @var AddProductsToCartOutput $addProductsToCartOutput */
88103 $ addProductsToCartOutput = $ this ->addProductsToCartService ->execute ($ maskedCartId , $ cartItems );
104+ $ this ->lockManager ->unlock ($ lockName );
89105
90106 return [
91107 'cart ' => [
0 commit comments