1313use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
1414use Magento \QuoteGraphQl \Model \Cart \AddProductsToCart ;
1515use Magento \QuoteGraphQl \Model \Cart \GetCartForUser ;
16+ use Magento \Quote \Model \QuoteMutexInterface ;
17+ use Magento \Framework \GraphQl \Query \Resolver \ContextInterface ;
1618
1719/**
1820 * Add configurable products to cart GraphQl resolver
@@ -30,16 +32,24 @@ class AddConfigurableProductsToCart implements ResolverInterface
3032 */
3133 private $ addProductsToCart ;
3234
35+ /**
36+ * @var QuoteMutexInterface
37+ */
38+ private $ quoteMutex ;
39+
3340 /**
3441 * @param GetCartForUser $getCartForUser
3542 * @param AddProductsToCart $addProductsToCart
43+ * @param QuoteMutexInterface $quoteMutex
3644 */
3745 public function __construct (
3846 GetCartForUser $ getCartForUser ,
39- AddProductsToCart $ addProductsToCart
47+ AddProductsToCart $ addProductsToCart ,
48+ QuoteMutexInterface $ quoteMutex
4049 ) {
4150 $ this ->getCartForUser = $ getCartForUser ;
4251 $ this ->addProductsToCart = $ addProductsToCart ;
52+ $ this ->quoteMutex = $ quoteMutex ;
4353 }
4454
4555 /**
@@ -50,15 +60,33 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
5060 if (!isset ($ args ['input ' ]['cart_id ' ]) || empty ($ args ['input ' ]['cart_id ' ])) {
5161 throw new GraphQlInputException (__ ('Required parameter "cart_id" is missing ' ));
5262 }
53- $ maskedCartId = $ args ['input ' ]['cart_id ' ];
5463
5564 if (!isset ($ args ['input ' ]['cart_items ' ]) || empty ($ args ['input ' ]['cart_items ' ])
5665 || !is_array ($ args ['input ' ]['cart_items ' ])
5766 ) {
5867 throw new GraphQlInputException (__ ('Required parameter "cart_items" is missing ' ));
5968 }
60- $ cartItems = $ args ['input ' ]['cart_items ' ];
6169
70+ return $ this ->quoteMutex ->execute (
71+ [$ args ['input ' ]['cart_id ' ]],
72+ \Closure::fromCallable ([$ this , 'run ' ]),
73+ [$ context , $ args ]
74+ );
75+ }
76+
77+ /**
78+ * Run the resolver.
79+ *
80+ * @param ContextInterface $context
81+ * @param array|null $args
82+ * @return array[]
83+ * @throws GraphQlInputException
84+ * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
85+ */
86+ public function run ($ context , array $ args ): array
87+ {
88+ $ cartItems = $ args ['input ' ]['cart_items ' ];
89+ $ maskedCartId = $ args ['input ' ]['cart_id ' ];
6290 $ storeId = (int )$ context ->getExtensionAttributes ()->getStore ()->getId ();
6391 $ cart = $ this ->getCartForUser ->execute ($ maskedCartId , $ context ->getUserId (), $ storeId );
6492 $ this ->addProductsToCart ->execute ($ cart , $ cartItems );
0 commit comments