Skip to content

Commit

Permalink
[Wishlist] Remove name from WishlistOutput #920
Browse files Browse the repository at this point in the history
  • Loading branch information
XxXgeoXxX committed Sep 25, 2019
1 parent 1d841c3 commit 7d03bdc
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 1 deletion.
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) {
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)) {
return $wishlistsData;
}

foreach ($wishlists as $wishlist) {
$wishlistsData [] = [
'sharing_code' => $wishlist->getSharingCode(),
'updated_at' => $wishlist->getUpdatedAt(),
'items_count' => $wishlist->getItemsCount(),
'model' => $wishlist,
];
}
return $wishlistsData;
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/WishlistGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Query {
}

type Customer {
wishlists: Wishlist! @resolver(class:"\\Magento\\WishlistGraphQl\\Model\\Resolver\\WishlistResolver") @cache(cacheable: false)
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") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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('customer@example.com', 'password')
);

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

}

/**
* @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];
}
}

0 comments on commit 7d03bdc

Please sign in to comment.