13
13
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
14
14
use Magento \QuoteGraphQl \Model \Cart \AddProductsToCart ;
15
15
use Magento \QuoteGraphQl \Model \Cart \GetCartForUser ;
16
+ use Magento \Quote \Model \QuoteMutexInterface ;
17
+ use Magento \Framework \GraphQl \Query \Resolver \ContextInterface ;
16
18
17
19
/**
18
20
* Add configurable products to cart GraphQl resolver
@@ -30,16 +32,24 @@ class AddConfigurableProductsToCart implements ResolverInterface
30
32
*/
31
33
private $ addProductsToCart ;
32
34
35
+ /**
36
+ * @var QuoteMutexInterface
37
+ */
38
+ private $ quoteMutex ;
39
+
33
40
/**
34
41
* @param GetCartForUser $getCartForUser
35
42
* @param AddProductsToCart $addProductsToCart
43
+ * @param QuoteMutexInterface $quoteMutex
36
44
*/
37
45
public function __construct (
38
46
GetCartForUser $ getCartForUser ,
39
- AddProductsToCart $ addProductsToCart
47
+ AddProductsToCart $ addProductsToCart ,
48
+ QuoteMutexInterface $ quoteMutex
40
49
) {
41
50
$ this ->getCartForUser = $ getCartForUser ;
42
51
$ this ->addProductsToCart = $ addProductsToCart ;
52
+ $ this ->quoteMutex = $ quoteMutex ;
43
53
}
44
54
45
55
/**
@@ -50,15 +60,33 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
50
60
if (!isset ($ args ['input ' ]['cart_id ' ]) || empty ($ args ['input ' ]['cart_id ' ])) {
51
61
throw new GraphQlInputException (__ ('Required parameter "cart_id" is missing ' ));
52
62
}
53
- $ maskedCartId = $ args ['input ' ]['cart_id ' ];
54
63
55
64
if (!isset ($ args ['input ' ]['cart_items ' ]) || empty ($ args ['input ' ]['cart_items ' ])
56
65
|| !is_array ($ args ['input ' ]['cart_items ' ])
57
66
) {
58
67
throw new GraphQlInputException (__ ('Required parameter "cart_items" is missing ' ));
59
68
}
60
- $ cartItems = $ args ['input ' ]['cart_items ' ];
61
69
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 ' ];
62
90
$ storeId = (int )$ context ->getExtensionAttributes ()->getStore ()->getId ();
63
91
$ cart = $ this ->getCartForUser ->execute ($ maskedCartId , $ context ->getUserId (), $ storeId );
64
92
$ this ->addProductsToCart ->execute ($ cart , $ cartItems );
0 commit comments