Skip to content

Commit 7020725

Browse files
author
Valeriy Nayda
committed
GraphQL-43: [Query] My Account > My Wish List
- Refactoring
1 parent cc797f7 commit 7020725

File tree

9 files changed

+214
-213
lines changed

9 files changed

+214
-213
lines changed

app/code/Magento/WishlistGraphQl/Model/WishlistItemsProductDataProvider.php renamed to app/code/Magento/CatalogGraphQl/Model/ProductDataProvider.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,38 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\WishlistGraphQl\Model;
8+
namespace Magento\CatalogGraphQl\Model;
99

1010
use Magento\Catalog\Api\ProductRepositoryInterface;
1111

12-
class WishlistItemsProductDataProvider
12+
/**
13+
* Product data provider
14+
*
15+
* TODO: will be replaces on deferred mechanism
16+
*/
17+
class ProductDataProvider
1318
{
1419
/**
1520
* @var ProductRepositoryInterface
1621
*/
1722
private $productRepository;
1823

24+
/**
25+
* @param ProductRepositoryInterface $productRepository
26+
*/
1927
public function __construct(ProductRepositoryInterface $productRepository)
2028
{
2129
$this->productRepository = $productRepository;
2230
}
2331

24-
public function getProductDataById(int $productId) {
32+
/**
33+
* Get product data by id
34+
*
35+
* @param int $productId
36+
* @return array
37+
*/
38+
public function getProductDataById(int $productId): array
39+
{
2540
$product = $this->productRepository->getById($productId);
2641
$productData = $product->toArray();
2742
$productData['model'] = $product;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\WishlistGraphQl\Model\Resolver;
9+
10+
use Magento\CatalogGraphQl\Model\ProductDataProvider;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Wishlist\Model\Item;
16+
17+
/**
18+
* Fetches the Product data according to the GraphQL schema
19+
*/
20+
class ProductResolver implements ResolverInterface
21+
{
22+
/**
23+
* @var ProductDataProvider
24+
*/
25+
private $productDataProvider;
26+
27+
/**
28+
* @param ProductDataProvider $productDataProvider
29+
*/
30+
public function __construct(ProductDataProvider $productDataProvider)
31+
{
32+
$this->productDataProvider = $productDataProvider;
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function resolve(
39+
Field $field,
40+
$context,
41+
ResolveInfo $info,
42+
array $value = null,
43+
array $args = null
44+
) {
45+
if (!isset($value['model'])) {
46+
throw new LocalizedException(__('Missing key "model" in Wishlist Item value data'));
47+
}
48+
/** @var Item $wishlistItem */
49+
$wishlistItem = $value['model'];
50+
51+
return $this->productDataProvider->getProductDataById($wishlistItem->getProductId());
52+
}
53+
}

app/code/Magento/WishlistGraphQl/Model/Resolver/WishlistItemsProductsResolver.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

app/code/Magento/WishlistGraphQl/Model/Resolver/WishlistItemsResolver.php

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,46 @@
77

88
namespace Magento\WishlistGraphQl\Model\Resolver;
99

10+
use Magento\Framework\Exception\LocalizedException;
1011
use Magento\Framework\GraphQl\Config\Element\Field;
11-
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
12-
use Magento\Framework\GraphQl\Query\Resolver\Value;
1312
use Magento\Framework\GraphQl\Query\ResolverInterface;
1413
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\Store\Api\Data\StoreInterface;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\Wishlist\Model\ResourceModel\Item\Collection as WishlistItemCollection;
17+
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory as WishlistItemCollectionFactory;
1518
use Magento\Wishlist\Model\Item;
16-
use Magento\WishlistGraphQl\Model\WishlistItemsDataProvider;
19+
use Magento\Wishlist\Model\Wishlist;
1720

21+
/**
22+
* Fetches the Wishlist Items data according to the GraphQL schema
23+
*/
1824
class WishlistItemsResolver implements ResolverInterface
1925
{
2026
/**
21-
* @var WishlistItemsDataProvider
27+
* @var WishlistItemCollectionFactory
2228
*/
23-
private $wishlistItemsDataProvider;
29+
private $wishlistItemCollectionFactory;
2430

25-
public function __construct(WishlistItemsDataProvider $wishlistItemsDataProvider)
26-
{
27-
$this->wishlistItemsDataProvider = $wishlistItemsDataProvider;
31+
/**
32+
* @var StoreManagerInterface
33+
*/
34+
private $storeManager;
35+
36+
/**
37+
* @param WishlistItemCollectionFactory $wishlistItemCollectionFactory
38+
* @param StoreManagerInterface $storeManager
39+
*/
40+
public function __construct(
41+
WishlistItemCollectionFactory $wishlistItemCollectionFactory,
42+
StoreManagerInterface $storeManager
43+
) {
44+
$this->wishlistItemCollectionFactory = $wishlistItemCollectionFactory;
45+
$this->storeManager = $storeManager;
2846
}
2947

3048
/**
31-
* Fetches the data from persistence models and format it according to the GraphQL schema.
32-
*
33-
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
34-
* @param ContextInterface $context
35-
* @param ResolveInfo $info
36-
* @param array|null $value
37-
* @param array|null $args
38-
* @throws \Exception
39-
* @return mixed|Value
49+
* @inheritdoc
4050
*/
4151
public function resolve(
4252
Field $field,
@@ -45,14 +55,41 @@ public function resolve(
4555
array $value = null,
4656
array $args = null
4757
) {
48-
return array_map(function (Item $wishlistItem) {
49-
return [
58+
if (!isset($value['model'])) {
59+
throw new LocalizedException(__('Missing key "model" in Wishlist value data'));
60+
}
61+
/** @var Wishlist $wishlist */
62+
$wishlist = $value['model'];
63+
64+
$wishlistItems = $this->getWishListItems($wishlist);
65+
66+
$data = [];
67+
foreach ($wishlistItems as $wishlistItem) {
68+
$data[] = [
5069
'id' => $wishlistItem->getId(),
5170
'qty' => $wishlistItem->getData('qty'),
52-
'description' => (string)$wishlistItem->getDescription(),
71+
'description' => $wishlistItem->getDescription(),
5372
'added_at' => $wishlistItem->getAddedAt(),
54-
'product_id' => (int)$wishlistItem->getProductId()
73+
'model' => $wishlistItem,
5574
];
56-
}, $this->wishlistItemsDataProvider->getWishlistItemsForCustomer($context->getUserId()));
75+
}
76+
return $data;
77+
}
78+
79+
/**
80+
* @param Wishlist $wishlist
81+
* @return Item[]
82+
*/
83+
private function getWishListItems(Wishlist $wishlist): array
84+
{
85+
/** @var WishlistItemCollection $wishlistItemCollection */
86+
$wishlistItemCollection = $this->wishlistItemCollectionFactory->create();
87+
$wishlistItemCollection
88+
->addWishlistFilter($wishlist)
89+
->addStoreFilter(array_map(function (StoreInterface $store) {
90+
return $store->getId();
91+
}, $this->storeManager->getStores()))
92+
->setVisibilityFilter();
93+
return $wishlistItemCollection->getItems();
5794
}
5895
}

app/code/Magento/WishlistGraphQl/Model/Resolver/WishlistResolver.php

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,39 @@
88
namespace Magento\WishlistGraphQl\Model\Resolver;
99

1010
use Magento\Framework\GraphQl\Config\Element\Field;
11-
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
12-
use Magento\Framework\GraphQl\Query\Resolver\Value;
1311
use Magento\Framework\GraphQl\Query\ResolverInterface;
1412
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15-
use Magento\WishlistGraphQl\Model\WishlistDataProvider;
13+
use Magento\Wishlist\Model\ResourceModel\Wishlist as WishlistResourceModel;
14+
use Magento\Wishlist\Model\Wishlist;
15+
use Magento\Wishlist\Model\WishlistFactory;
1616

17+
/**
18+
* Fetches the Wishlist data according to the GraphQL schema
19+
*/
1720
class WishlistResolver implements ResolverInterface
1821
{
1922
/**
20-
* @var WishlistDataProvider
23+
* @var WishlistResourceModel
24+
*/
25+
private $wishlistResource;
26+
27+
/**
28+
* @var WishlistFactory
2129
*/
22-
private $wishlistDataProvider;
30+
private $wishlistFactory;
2331

24-
public function __construct(WishlistDataProvider $wishlistDataProvider)
32+
/**
33+
* @param WishlistResourceModel $wishlistResource
34+
* @param WishlistFactory $wishlistFactory
35+
*/
36+
public function __construct(WishlistResourceModel $wishlistResource, WishlistFactory $wishlistFactory)
2537
{
26-
$this->wishlistDataProvider = $wishlistDataProvider;
38+
$this->wishlistResource = $wishlistResource;
39+
$this->wishlistFactory = $wishlistFactory;
2740
}
2841

2942
/**
30-
* Fetches the data from persistence models and format it according to the GraphQL schema.
31-
*
32-
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
33-
* @param ContextInterface $context
34-
* @param ResolveInfo $info
35-
* @param array|null $value
36-
* @param array|null $args
37-
* @throws \Exception
38-
* @return mixed|Value
43+
* @inheritdoc
3944
*/
4045
public function resolve(
4146
Field $field,
@@ -44,10 +49,22 @@ public function resolve(
4449
array $value = null,
4550
array $args = null
4651
) {
47-
$wishlist = $this->wishlistDataProvider->getWishlistForCustomer($context->getUserId());
52+
$customerId = $context->getUserId();
53+
54+
/** @var Wishlist $wishlist */
55+
$wishlist = $this->wishlistFactory->create();
56+
$this->wishlistResource->load($wishlist, $customerId, 'customer_id');
57+
58+
if (null === $wishlist->getId()) {
59+
return [];
60+
}
61+
4862
return [
4963
'sharing_code' => $wishlist->getSharingCode(),
50-
'updated_at' => $wishlist->getUpdatedAt()
64+
'updated_at' => $wishlist->getUpdatedAt(),
65+
'items_count' => $wishlist->getItemsCount(),
66+
'name' => $wishlist->getName(),
67+
'model' => $wishlist,
5168
];
5269
}
5370
}

app/code/Magento/WishlistGraphQl/Model/WishlistDataProvider.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)