Skip to content

Commit acbc881

Browse files
committed
magento graphql-ce#920: Remove name from WishlistOutput
1 parent 282b09b commit acbc881

File tree

4 files changed

+76
-89
lines changed

4 files changed

+76
-89
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\WishlistFactory;
14+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
15+
16+
/**
17+
* Fetches the Wishlists data according to the GraphQL schema
18+
*/
19+
class CustomerWishlistResolver implements ResolverInterface
20+
{
21+
/**
22+
* @var WishlistFactory
23+
*/
24+
private $wishlistFactory;
25+
26+
/**
27+
* @param WishlistFactory $wishlistFactory
28+
*/
29+
public function __construct(WishlistFactory $wishlistFactory)
30+
{
31+
$this->wishlistFactory = $wishlistFactory;
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+
/* Guest checking */
45+
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
46+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
47+
}
48+
$wishlist = $this->wishlistFactory->create()->loadByCustomerId($context->getUserId(), true);
49+
return [
50+
'id' => (string) $wishlist->getId(),
51+
'sharing_code' => $wishlist->getSharingCode(),
52+
'updated_at' => $wishlist->getUpdatedAt(),
53+
'items_count' => $wishlist->getItemsCount(),
54+
'model' => $wishlist,
55+
];
56+
}
57+
}

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

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

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# See COPYING.txt for license details.
33

44
type Query {
5-
wishlist: WishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\WishlistResolver") @doc(description: "The wishlist query returns the contents of a customer's wish list") @cache(cacheable: false)
5+
wishlist: WishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\WishlistResolver") @deprecated(reason: "Moved under `Customer` `wishlist`") @doc(description: "The wishlist query returns the contents of a customer's wish list") @cache(cacheable: false)
66
}
77

88
type Customer {
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)
9+
wishlist: Wishlist! @resolver(class:"\\Magento\\WishlistGraphQl\\Model\\Resolver\\CustomerWishlistResolver") @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") {
@@ -18,6 +18,7 @@ type WishlistOutput @doc(description: "Deprecated: `Wishlist` type should be use
1818
}
1919

2020
type Wishlist {
21+
id: ID @doc(description: "Wishlist unique identifier")
2122
items: [WishlistItem] @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\WishlistItemsResolver") @doc(description: "An array of items in the customer's wish list"),
2223
items_count: Int @doc(description: "The number of items in the wish list"),
2324
sharing_code: String @doc(description: "An encrypted code that Magento uses to link to the wish list"),

dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php renamed to dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistTest.php

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Magento\Wishlist\Model\Item;
1414
use Magento\Wishlist\Model\ResourceModel\Wishlist\CollectionFactory;
1515

16-
class CustomerWishlistsTest extends GraphQlAbstract
16+
class CustomerWishlistTest extends GraphQlAbstract
1717
{
1818
/**
1919
* @var CustomerTokenServiceInterface
@@ -23,21 +23,21 @@ class CustomerWishlistsTest extends GraphQlAbstract
2323
/**
2424
* @var CollectionFactory
2525
*/
26-
private $_wishlistCollectionFactory;
26+
private $wishlistCollectionFactory;
2727

2828
protected function setUp()
2929
{
3030
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
31-
$this->_wishlistCollectionFactory = Bootstrap::getObjectManager()->get(CollectionFactory::class);
31+
$this->wishlistCollectionFactory = Bootstrap::getObjectManager()->get(CollectionFactory::class);
3232
}
3333

3434
/**
3535
* @magentoApiDataFixture Magento/Wishlist/_files/wishlist.php
3636
*/
37-
public function testGetCustomerWishlists(): void
37+
public function testCustomerWishlist(): void
3838
{
3939
/** @var \Magento\Wishlist\Model\Wishlist $wishlist */
40-
$collection = $this->_wishlistCollectionFactory->create()->filterByCustomerId(1);
40+
$collection = $this->wishlistCollectionFactory->create()->filterByCustomerId(1);
4141

4242
/** @var Item $wishlistItem */
4343
$wishlistItem = $collection->getFirstItem();
@@ -46,7 +46,8 @@ public function testGetCustomerWishlists(): void
4646
{
4747
customer
4848
{
49-
wishlists {
49+
wishlist {
50+
id
5051
items_count
5152
sharing_code
5253
updated_at
@@ -66,32 +67,25 @@ public function testGetCustomerWishlists(): void
6667
'',
6768
$this->getCustomerAuthHeaders('[email protected]', 'password')
6869
);
69-
70-
$this->assertEquals($wishlistItem->getItemsCount(), $response['customer']['wishlists'][0]['items_count']);
71-
$this->assertEquals($wishlistItem->getSharingCode(), $response['customer']['wishlists'][0]['sharing_code']);
72-
$this->assertEquals($wishlistItem->getUpdatedAt(), $response['customer']['wishlists'][0]['updated_at']);
73-
$this->assertEquals('simple', $response['customer']['wishlists'][0]['items'][0]['product']['sku']);
70+
$this->assertEquals((string)$wishlistItem->getId(), $response['customer']['wishlist']['id']);
71+
$this->assertEquals($wishlistItem->getItemsCount(), $response['customer']['wishlist']['items_count']);
72+
$this->assertEquals($wishlistItem->getSharingCode(), $response['customer']['wishlist']['sharing_code']);
73+
$this->assertEquals($wishlistItem->getUpdatedAt(), $response['customer']['wishlist']['updated_at']);
74+
$this->assertEquals('simple', $response['customer']['wishlist']['items'][0]['product']['sku']);
7475
}
7576

7677
/**
7778
* @magentoApiDataFixture Magento/Customer/_files/customer.php
7879
*/
79-
public function testCustomerWithoutWishlists(): void
80+
public function testCustomerAlwaysHasWishlist(): void
8081
{
8182
$query =
8283
<<<QUERY
8384
{
8485
customer
8586
{
86-
wishlists {
87-
items_count
88-
sharing_code
89-
updated_at
90-
items {
91-
product {
92-
sku
93-
}
94-
}
87+
wishlist {
88+
id
9589
}
9690
}
9791
}
@@ -104,7 +98,7 @@ public function testCustomerWithoutWishlists(): void
10498
$this->getCustomerAuthHeaders('[email protected]', 'password')
10599
);
106100

107-
$this->assertEquals([], $response['customer']['wishlists']);
101+
$this->assertNotEmpty($response['customer']['wishlist']['id']);
108102
}
109103

110104
/**

0 commit comments

Comments
 (0)