Skip to content

Commit d0891fc

Browse files
ENGCOM-6165: [Wishlist] Remove name from WishlistOutput #920 #957
- Merge Pull Request magento/graphql-ce#957 from XxXgeoXxX/graphql-ce:2.3-develop#920 - Merged commits: 1. 1d841c3 2. 7d03bdc 3. 7d4a306 4. c92ecf8 5. 4c85d84 6. b52fe3e 7. 0977e94 8. bff6344 9. 282b09b 10. acbc881 11. 6b443b4
2 parents 6f1c7e3 + 6b443b4 commit d0891fc

File tree

4 files changed

+212
-5
lines changed

4 files changed

+212
-5
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 customer wishlist data
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+
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
45+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
46+
}
47+
$wishlist = $this->wishlistFactory->create()->loadByCustomerId($context->getUserId(), true);
48+
return [
49+
'id' => (string) $wishlist->getId(),
50+
'sharing_code' => $wishlist->getSharingCode(),
51+
'updated_at' => $wishlist->getUpdatedAt(),
52+
'items_count' => $wishlist->getItemsCount(),
53+
'model' => $wishlist,
54+
];
55+
}
56+
}

app/code/Magento/WishlistGraphQl/etc/module.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9-
<module name="Magento_WishlistGraphQl" />
9+
<module name="Magento_WishlistGraphQl">
10+
<sequence>
11+
<module name="Magento_Customer"/>
12+
<module name="Magento_CustomerGraphQl"/>
13+
</sequence>
14+
</module>
1015
</config>

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@
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

8-
type WishlistOutput {
8+
type Customer {
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)
10+
}
11+
12+
type WishlistOutput @doc(description: "Deprecated: `Wishlist` type should be used instead") {
13+
items: [WishlistItem] @deprecated(reason: "Use field `items` from type `Wishlist` instead") @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\WishlistItemsResolver") @doc(description: "An array of items in the customer's wish list"),
14+
items_count: Int @deprecated(reason: "Use field `items_count` from type `Wishlist` instead") @doc(description: "The number of items in the wish list"),
15+
name: String @deprecated(reason: "This field is related to Commerce functionality and is always `null` in Open Source edition") @doc(description: "When multiple wish lists are enabled, the name the customer assigns to the wishlist"),
16+
sharing_code: String @deprecated(reason: "Use field `sharing_code` from type `Wishlist` instead") @doc(description: "An encrypted code that Magento uses to link to the wish list"),
17+
updated_at: String @deprecated(reason: "Use field `updated_at` from type `Wishlist` instead") @doc(description: "The time of the last modification to the wish list")
18+
}
19+
20+
type Wishlist {
21+
id: ID @doc(description: "Wishlist unique identifier")
922
items: [WishlistItem] @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\WishlistItemsResolver") @doc(description: "An array of items in the customer's wish list"),
1023
items_count: Int @doc(description: "The number of items in the wish list"),
11-
name: String @doc(description: "When multiple wish lists are enabled, the name the customer assigns to the wishlist"),
1224
sharing_code: String @doc(description: "An encrypted code that Magento uses to link to the wish list"),
1325
updated_at: String @doc(description: "The time of the last modification to the wish list")
1426
}
@@ -19,4 +31,4 @@ type WishlistItem {
1931
description: String @doc(description: "The customer's comment about this item"),
2032
added_at: String @doc(description: "The time when the customer added the item to the wish list"),
2133
product: ProductInterface @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\ProductResolver")
22-
}
34+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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 CustomerWishlistTest 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 testCustomerWishlist(): 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+
wishlist {
49+
id
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+
$this->assertEquals((string)$wishlistItem->getId(), $response['customer']['wishlist']['id']);
70+
$this->assertEquals($wishlistItem->getItemsCount(), $response['customer']['wishlist']['items_count']);
71+
$this->assertEquals($wishlistItem->getSharingCode(), $response['customer']['wishlist']['sharing_code']);
72+
$this->assertEquals($wishlistItem->getUpdatedAt(), $response['customer']['wishlist']['updated_at']);
73+
$this->assertEquals('simple', $response['customer']['wishlist']['items'][0]['product']['sku']);
74+
}
75+
76+
/**
77+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
78+
*/
79+
public function testCustomerAlwaysHasWishlist(): void
80+
{
81+
$query =
82+
<<<QUERY
83+
{
84+
customer {
85+
wishlist {
86+
id
87+
}
88+
}
89+
}
90+
QUERY;
91+
92+
$response = $this->graphQlQuery(
93+
$query,
94+
[],
95+
'',
96+
$this->getCustomerAuthHeaders('[email protected]', 'password')
97+
);
98+
99+
$this->assertNotEmpty($response['customer']['wishlist']['id']);
100+
}
101+
102+
/**
103+
* @expectedException \Exception
104+
* @expectedExceptionMessage The current customer isn't authorized.
105+
*/
106+
public function testGuestCannotGetWishlist()
107+
{
108+
$query =
109+
<<<QUERY
110+
{
111+
customer {
112+
wishlist {
113+
items_count
114+
sharing_code
115+
updated_at
116+
}
117+
}
118+
}
119+
QUERY;
120+
$this->graphQlQuery($query);
121+
}
122+
123+
/**
124+
* @param string $email
125+
* @param string $password
126+
* @return array
127+
* @throws \Magento\Framework\Exception\AuthenticationException
128+
*/
129+
private function getCustomerAuthHeaders(string $email, string $password): array
130+
{
131+
$customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
132+
return ['Authorization' => 'Bearer ' . $customerToken];
133+
}
134+
}

0 commit comments

Comments
 (0)