Skip to content

Commit 0350e3c

Browse files
authored
LYNX-790: Add clearWishlist mutation
1 parent 47babf0 commit 0350e3c

File tree

5 files changed

+378
-4
lines changed

5 files changed

+378
-4
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Wishlist\Model\ResourceModel;
9+
10+
use Exception;
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Wishlist\Model\Wishlist;
13+
use Magento\Wishlist\Model\Wishlist\Data\Error;
14+
use Magento\Wishlist\Model\Wishlist\Data\WishlistOutput;
15+
use Psr\Log\LoggerInterface;
16+
17+
class ClearWishlist
18+
{
19+
private const ERROR_UNDEFINED = 'UNDEFINED';
20+
21+
/**
22+
* ClearWishlist Constructor
23+
*
24+
* @param ResourceConnection $resourceConnection
25+
* @param LoggerInterface $logger
26+
*/
27+
public function __construct(
28+
private readonly ResourceConnection $resourceConnection,
29+
private readonly LoggerInterface $logger
30+
) {
31+
}
32+
33+
/**
34+
* Remove all items from specified wishlist
35+
*
36+
* @param Wishlist $wishlist
37+
* @return WishlistOutput
38+
*/
39+
public function execute(Wishlist $wishlist): WishlistOutput
40+
{
41+
try {
42+
$this->resourceConnection->getConnection()->delete(
43+
$this->resourceConnection->getTableName('wishlist_item'),
44+
['wishlist_id = ?' => $wishlist->getId()]
45+
);
46+
47+
return new WishlistOutput($wishlist, []);
48+
} catch (Exception $exception) {
49+
$this->logger->error($exception->getMessage());
50+
51+
return new WishlistOutput($wishlist, [
52+
new Error(
53+
"Could not delete wishlist items for WishlistId '{$wishlist->getId()}'.",
54+
self::ERROR_UNDEFINED
55+
)
56+
]);
57+
}
58+
}
59+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\WishlistGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Wishlist\Model\ResourceModel\ClearWishlist as ClearWishlistResource;
16+
use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResource;
17+
use Magento\Wishlist\Model\Wishlist;
18+
use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig;
19+
use Magento\Wishlist\Model\Wishlist\Data\Error;
20+
use Magento\Wishlist\Model\WishlistFactory;
21+
use Magento\WishlistGraphQl\Mapper\WishlistDataMapper;
22+
23+
/**
24+
* Resolver to clear all the products from the specified wishlist
25+
*/
26+
class ClearWishlist implements ResolverInterface
27+
{
28+
/**
29+
* ClearWishlist Constructor
30+
*
31+
* @param WishlistConfig $wishlistConfig
32+
* @param ClearWishlistResource $clearWishlistResource
33+
* @param WishlistFactory $wishlistFactory
34+
* @param WishlistResource $wishlistResource
35+
* @param WishlistDataMapper $wishlistDataMapper
36+
*/
37+
public function __construct(
38+
private readonly WishlistConfig $wishlistConfig,
39+
private readonly ClearWishlistResource $clearWishlistResource,
40+
private readonly WishlistFactory $wishlistFactory,
41+
private readonly WishlistResource $wishlistResource,
42+
private readonly WishlistDataMapper $wishlistDataMapper
43+
) {
44+
}
45+
46+
/**
47+
* @inheritdoc
48+
*/
49+
public function resolve(
50+
Field $field,
51+
$context,
52+
ResolveInfo $info,
53+
?array $value = null,
54+
?array $args = null
55+
): array {
56+
if (!$this->wishlistConfig->isEnabled()) {
57+
throw new GraphQlInputException(__('The wishlist configuration is currently disabled.'));
58+
}
59+
60+
$customerId = (int)$context->getUserId();
61+
if (!$customerId) {
62+
throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist'));
63+
}
64+
65+
$wishlistId = ((int)$args['wishlistId']) ?: null;
66+
if (!$wishlistId) {
67+
throw new GraphQlInputException(__('The wishlistId is required.'));
68+
}
69+
70+
$wishlist = $this->getWishlist($wishlistId);
71+
if (!$wishlist->getId() || $customerId !== (int)$wishlist->getCustomerId()) {
72+
throw new GraphQlInputException(__('The wishlist was not found.'));
73+
}
74+
75+
$wishlistOutput = $this->clearWishlistResource->execute($wishlist);
76+
77+
return [
78+
'user_errors' => \array_map(
79+
function (Error $error) {
80+
return [
81+
'code' => $error->getCode(),
82+
'message' => $error->getMessage(),
83+
];
84+
},
85+
$wishlistOutput->getErrors()
86+
),
87+
'wishlist' => $this->wishlistDataMapper->map($wishlistOutput->getWishlist())
88+
];
89+
}
90+
91+
/**
92+
* Get customer wishlist
93+
*
94+
* @param int $wishlistId
95+
* @return Wishlist
96+
*/
97+
private function getWishlist(int $wishlistId): Wishlist
98+
{
99+
$wishlist = $this->wishlistFactory->create();
100+
$this->wishlistResource->load($wishlist, $wishlistId);
101+
102+
return $wishlist;
103+
}
104+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2018 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
99
<module name="Magento_WishlistGraphQl">
1010
<sequence>
1111
<module name="Magento_Customer"/>
1212
<module name="Magento_CustomerGraphQl"/>
13+
<module name="Magento_Wishlist"/>
1314
</sequence>
1415
</module>
1516
</config>

app/code/Magento/WishlistGraphQl/etc/schema.graphqls

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Copyright © Magento, Inc. All rights reserved.
2-
# See COPYING.txt for license details.
1+
# Copyright 2018 Adobe
2+
# All Rights Reserved.
33

44
type Query {
55
wishlist: WishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\WishlistResolver") @deprecated(reason: "Moved under `Customer.wishlist`.") @doc(description: "Return the contents of a customer's wish list.") @cache(cacheable: false)
@@ -64,6 +64,7 @@ type Mutation {
6464
wishlistId: ID!, @doc(description: "The unique ID of the wish list")
6565
wishlistItemIds: [ID!] @doc(description: "An array of IDs representing products to be added to the cart. If no IDs are specified, all items in the wishlist will be added to the cart")
6666
): AddWishlistItemsToCartOutput @resolver(class: "Magento\\WishlistGraphQl\\Model\\Resolver\\Wishlist\\AddToCart") @doc(description: "Add items in the specified wishlist to the customer's cart.")
67+
clearWishlist(wishlistId: ID! @doc(description: "The ID of a wish list.")): RemoveProductsFromWishlistOutput @doc(description: "Remove all the products from the specified wish list.") @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\ClearWishlist")
6768
}
6869

6970
type AddWishlistItemsToCartOutput @doc(description: "Contains the resultant wish list and any error information.") {

0 commit comments

Comments
 (0)