Skip to content

Commit

Permalink
magento graphql-ce#920: Remove name from WishlistOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
lenaorobei committed Oct 29, 2019
1 parent 282b09b commit acbc881
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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\WishlistFactory;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;

/**
* Fetches the Wishlists data according to the GraphQL schema
*/
class CustomerWishlistResolver implements ResolverInterface
{
/**
* @var WishlistFactory
*/
private $wishlistFactory;

/**
* @param WishlistFactory $wishlistFactory
*/
public function __construct(WishlistFactory $wishlistFactory)
{
$this->wishlistFactory = $wishlistFactory;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
/* Guest checking */
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}
$wishlist = $this->wishlistFactory->create()->loadByCustomerId($context->getUserId(), true);
return [
'id' => (string) $wishlist->getId(),
'sharing_code' => $wishlist->getSharingCode(),
'updated_at' => $wishlist->getUpdatedAt(),
'items_count' => $wishlist->getItemsCount(),
'model' => $wishlist,
];
}
}

This file was deleted.

5 changes: 3 additions & 2 deletions app/code/Magento/WishlistGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# See COPYING.txt for license details.

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)
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)
}

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)
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)
}

type WishlistOutput @doc(description: "Deprecated: `Wishlist` type should be used instead") {
Expand All @@ -18,6 +18,7 @@ type WishlistOutput @doc(description: "Deprecated: `Wishlist` type should be use
}

type Wishlist {
id: ID @doc(description: "Wishlist unique identifier")
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"),
sharing_code: String @doc(description: "An encrypted code that Magento uses to link to the wish list"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Magento\Wishlist\Model\Item;
use Magento\Wishlist\Model\ResourceModel\Wishlist\CollectionFactory;

class CustomerWishlistsTest extends GraphQlAbstract
class CustomerWishlistTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
Expand All @@ -23,21 +23,21 @@ class CustomerWishlistsTest extends GraphQlAbstract
/**
* @var CollectionFactory
*/
private $_wishlistCollectionFactory;
private $wishlistCollectionFactory;

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

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

/** @var Item $wishlistItem */
$wishlistItem = $collection->getFirstItem();
Expand All @@ -46,7 +46,8 @@ public function testGetCustomerWishlists(): void
{
customer
{
wishlists {
wishlist {
id
items_count
sharing_code
updated_at
Expand All @@ -66,32 +67,25 @@ public function testGetCustomerWishlists(): void
'',
$this->getCustomerAuthHeaders('customer@example.com', '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']);
$this->assertEquals((string)$wishlistItem->getId(), $response['customer']['wishlist']['id']);
$this->assertEquals($wishlistItem->getItemsCount(), $response['customer']['wishlist']['items_count']);
$this->assertEquals($wishlistItem->getSharingCode(), $response['customer']['wishlist']['sharing_code']);
$this->assertEquals($wishlistItem->getUpdatedAt(), $response['customer']['wishlist']['updated_at']);
$this->assertEquals('simple', $response['customer']['wishlist']['items'][0]['product']['sku']);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testCustomerWithoutWishlists(): void
public function testCustomerAlwaysHasWishlist(): void
{
$query =
<<<QUERY
{
customer
{
wishlists {
items_count
sharing_code
updated_at
items {
product {
sku
}
}
wishlist {
id
}
}
}
Expand All @@ -104,7 +98,7 @@ public function testCustomerWithoutWishlists(): void
$this->getCustomerAuthHeaders('customer@example.com', 'password')
);

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

/**
Expand Down

0 comments on commit acbc881

Please sign in to comment.