Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

[Wishlist] Remove name from WishlistOutput #920 #957

Merged
merged 11 commits into from
Nov 1, 2019
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\WishlistGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Wishlist\Model\ResourceModel\Wishlist\CollectionFactory;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;

/**
* Fetches the Wishlists data according to the GraphQL schema
*/
class CustomerWishlistsResolver implements ResolverInterface
{
/**
* @var CollectionFactory
*/
private $_wishlistCollectionFactory;

/**
* @param CollectionFactory $wishlistCollectionFactory
*/
public function __construct(CollectionFactory $wishlistCollectionFactory)
{
$this->_wishlistCollectionFactory = $wishlistCollectionFactory;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$customerId = $context->getUserId();

/* Guest checking */
if (!$customerId && 0 === $customerId) {
lenaorobei marked this conversation as resolved.
Show resolved Hide resolved
throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist'));
}
$collection = $this->_wishlistCollectionFactory->create()->filterByCustomerId($customerId);
$wishlists = $collection->getItems();
$wishlistsData = [];
if (0 === count($wishlists)) {
lenaorobei marked this conversation as resolved.
Show resolved Hide resolved
return $wishlistsData;
}

foreach ($wishlists as $wishlist) {
$wishlistsData [] = [
'sharing_code' => $wishlist->getSharingCode(),
'updated_at' => $wishlist->getUpdatedAt(),
'items_count' => $wishlist->getItemsCount(),
'model' => $wishlist,
];
}
return $wishlistsData;
}
}
7 changes: 6 additions & 1 deletion app/code/Magento/WishlistGraphQl/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_WishlistGraphQl" />
<module name="Magento_WishlistGraphQl">
<sequence>
<module name="Magento_Customer"/>
<module name="Magento_CustomerGraphQl"/>
</sequence>
</module>
</config>
15 changes: 13 additions & 2 deletions app/code/Magento/WishlistGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ type Query {
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)
}

type WishlistOutput {
type Customer {
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)
}

type WishlistOutput @doc(description: "Deprecated: 'Wishlist' type should be used instead") {
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"),
lenaorobei marked this conversation as resolved.
Show resolved Hide resolved
items_count: Int @deprecated(reason: "Use field `items_count` from type `Wishlist` instead") @doc(description: "The number of items in the wish list"),
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"),
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"),
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")
}

type Wishlist {
items: [WishlistItem] @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\WishlistItemsResolver") @doc(description: "An array of items in the customer's wish list"),
items_count: Int @doc(description: "The number of items in the wish list"),
name: String @doc(description: "When multiple wish lists are enabled, the name the customer assigns to the wishlist"),
sharing_code: String @doc(description: "An encrypted code that Magento uses to link to the wish list"),
updated_at: String @doc(description: "The time of the last modification to the wish list")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Wishlist;

use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Wishlist\Model\Item;
use Magento\Wishlist\Model\ResourceModel\Wishlist\CollectionFactory;

class CustomerWishlistsTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

/**
* @var CollectionFactory
*/
private $_wishlistCollectionFactory;

protected function setUp()
{
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
$this->_wishlistCollectionFactory = Bootstrap::getObjectManager()->get(CollectionFactory::class);
}

/**
* @magentoApiDataFixture Magento/Wishlist/_files/wishlist.php
*/
public function testGetCustomerWishlists(): void
{
/** @var \Magento\Wishlist\Model\Wishlist $wishlist */
$collection = $this->_wishlistCollectionFactory->create()->filterByCustomerId(1);

/** @var Item $wishlistItem */
$wishlistItem = $collection->getFirstItem();
$query =
<<<QUERY
{
customer
{
wishlists {
items_count
sharing_code
updated_at
items {
product {
sku
}
}
}
}
}
QUERY;

$response = $this->graphQlQuery(
$query,
[],
'',
$this->getCustomerAuthHeaders('[email protected]', 'password')
);

$this->assertEquals($wishlistItem->getItemsCount(), $response['customer']['wishlists'][0]['items_count']);
$this->assertEquals($wishlistItem->getSharingCode(), $response['customer']['wishlists'][0]['sharing_code']);
$this->assertEquals($wishlistItem->getUpdatedAt(), $response['customer']['wishlists'][0]['updated_at']);
$this->assertEquals('simple', $response['customer']['wishlists'][0]['items'][0]['product']['sku']);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testCustomerWithoutWishlists(): void
{
$query =
<<<QUERY
{
customer
{
wishlists {
items_count
sharing_code
updated_at
items {
product {
sku
}
}
}
}
}
QUERY;

$response = $this->graphQlQuery(
$query,
[],
'',
$this->getCustomerAuthHeaders('[email protected]', 'password')
);

$this->assertEquals([], $response['customer']['wishlists']);
}

/**
* @expectedException \Exception
* @expectedExceptionMessage The current customer isn't authorized.
*/
public function testGetGuestWishlist()
{
$query =
<<<QUERY
{
customer
{
wishlists {
items_count
sharing_code
updated_at
}
}
}
QUERY;
$this->graphQlQuery($query);
}

/**
* @param string $email
* @param string $password
* @return array
* @throws \Magento\Framework\Exception\AuthenticationException
*/
private function getCustomerAuthHeaders(string $email, string $password): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
return ['Authorization' => 'Bearer ' . $customerToken];
}
}