Skip to content

Commit 7d03bdc

Browse files
committed
[Wishlist] Remove name from WishlistOutput #920
1 parent 1d841c3 commit 7d03bdc

File tree

3 files changed

+178
-1
lines changed

3 files changed

+178
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Query\ResolverInterface;
12+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
13+
use Magento\Wishlist\Model\ResourceModel\Wishlist\CollectionFactory;
14+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
15+
16+
/**
17+
* Fetches the Wishlists data according to the GraphQL schema
18+
*/
19+
class CustomerWishlistsResolver implements ResolverInterface
20+
{
21+
/**
22+
* @var CollectionFactory
23+
*/
24+
private $_wishlistCollectionFactory;
25+
26+
/**
27+
* @param CollectionFactory $wishlistCollectionFactory
28+
*/
29+
public function __construct(CollectionFactory $wishlistCollectionFactory)
30+
{
31+
$this->_wishlistCollectionFactory = $wishlistCollectionFactory;
32+
}
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
public function resolve(
38+
Field $field,
39+
$context,
40+
ResolveInfo $info,
41+
array $value = null,
42+
array $args = null
43+
) {
44+
$customerId = $context->getUserId();
45+
46+
/* Guest checking */
47+
if (!$customerId && 0 === $customerId) {
48+
throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist'));
49+
}
50+
$collection = $this->_wishlistCollectionFactory->create()->filterByCustomerId($customerId);
51+
$wishlists = $collection->getItems();
52+
$wishlistsData = [];
53+
if (0 === count($wishlists)) {
54+
return $wishlistsData;
55+
}
56+
57+
foreach ($wishlists as $wishlist) {
58+
$wishlistsData [] = [
59+
'sharing_code' => $wishlist->getSharingCode(),
60+
'updated_at' => $wishlist->getUpdatedAt(),
61+
'items_count' => $wishlist->getItemsCount(),
62+
'model' => $wishlist,
63+
];
64+
}
65+
return $wishlistsData;
66+
}
67+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type Query {
66
}
77

88
type Customer {
9-
wishlists: Wishlist! @resolver(class:"\\Magento\\WishlistGraphQl\\Model\\Resolver\\WishlistResolver") @cache(cacheable: false)
9+
wishlists: [Wishlist]! @resolver(class:"\\Magento\\WishlistGraphQl\\Model\\Resolver\\CustomerWishlistsResolver") @doc(description: "The wishlist query returns the contents of a customer's wish lists") @cache(cacheable: false)
1010
}
1111

1212
type WishlistOutput @doc(description: "Deprecated: 'Wishlist' type should be used instead") {
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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\GraphQl\Wishlist;
9+
10+
use Magento\Integration\Api\CustomerTokenServiceInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\TestCase\GraphQlAbstract;
13+
use Magento\Wishlist\Model\Item;
14+
use Magento\Wishlist\Model\ResourceModel\Wishlist\CollectionFactory;
15+
16+
class CustomerWishlistsTest extends GraphQlAbstract
17+
{
18+
/**
19+
* @var CustomerTokenServiceInterface
20+
*/
21+
private $customerTokenService;
22+
23+
/**
24+
* @var CollectionFactory
25+
*/
26+
private $_wishlistCollectionFactory;
27+
28+
protected function setUp()
29+
{
30+
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
31+
$this->_wishlistCollectionFactory = Bootstrap::getObjectManager()->get(CollectionFactory::class);
32+
}
33+
34+
/**
35+
* @magentoApiDataFixture Magento/Wishlist/_files/wishlist.php
36+
*/
37+
public function testGetCustomerWishlists(): void
38+
{
39+
/** @var \Magento\Wishlist\Model\Wishlist $wishlist */
40+
$collection = $this->_wishlistCollectionFactory->create()->filterByCustomerId(1);
41+
42+
/** @var Item $wishlistItem */
43+
$wishlistItem = $collection->getFirstItem();
44+
$query =
45+
<<<QUERY
46+
{
47+
customer
48+
{
49+
wishlists {
50+
items_count
51+
sharing_code
52+
updated_at
53+
items {
54+
product {
55+
sku
56+
}
57+
}
58+
}
59+
}
60+
}
61+
QUERY;
62+
63+
$response = $this->graphQlQuery(
64+
$query,
65+
[],
66+
'',
67+
$this->getCustomerAuthHeaders('[email protected]', 'password')
68+
);
69+
70+
$this->assertEquals($wishlistItem->getItemsCount(), $response['wishlists'][0]['items_count']);
71+
$this->assertEquals($wishlistItem->getSharingCode(), $response['wishlists'][0]['sharing_code']);
72+
$this->assertEquals($wishlistItem->getUpdatedAt(), $response['wishlists'][0]['updated_at']);
73+
$this->assertEquals('simple', $response['wishlists'][0]['items'][0]['product']['sku']);
74+
75+
}
76+
77+
/**
78+
* @expectedException \Exception
79+
* @expectedExceptionMessage The current customer isn't authorized.
80+
*/
81+
public function testGetGuestWishlist()
82+
{
83+
$query =
84+
<<<QUERY
85+
{
86+
customer
87+
{
88+
wishlists {
89+
items_count
90+
sharing_code
91+
updated_at
92+
}
93+
}
94+
}
95+
QUERY;
96+
$this->graphQlQuery($query);
97+
}
98+
99+
/**
100+
* @param string $email
101+
* @param string $password
102+
* @return array
103+
* @throws \Magento\Framework\Exception\AuthenticationException
104+
*/
105+
private function getCustomerAuthHeaders(string $email, string $password): array
106+
{
107+
$customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
108+
return ['Authorization' => 'Bearer ' . $customerToken];
109+
}
110+
}

0 commit comments

Comments
 (0)