From 1d841c378b6ae623b3ff5fd97ddf2f1f206636ca Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Mon, 23 Sep 2019 08:12:39 +0300 Subject: [PATCH 01/10] [Wishlist] Remove name from WishlistOutput #920 --- app/code/Magento/WishlistGraphQl/etc/module.xml | 7 ++++++- .../Magento/WishlistGraphQl/etc/schema.graphqls | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/WishlistGraphQl/etc/module.xml b/app/code/Magento/WishlistGraphQl/etc/module.xml index 337623cc85a9..c2f5b3165b2a 100644 --- a/app/code/Magento/WishlistGraphQl/etc/module.xml +++ b/app/code/Magento/WishlistGraphQl/etc/module.xml @@ -6,5 +6,10 @@ */ --> - + + + + + + diff --git a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls index 2aa5f03a787d..7daf15596f19 100644 --- a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls +++ b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls @@ -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\\WishlistResolver") @cache(cacheable: false) +} + +type WishlistOutput @doc(description: "Deprecated: 'Wishlist' type should be used instead") { + items: [WishlistItem] @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\WishlistItemsResolver") @doc(description: "Deprecated: use field `items` from type `Wishlist`"), + items_count: Int @doc(description: "Deprecated: use field `items_count` from type `Wishlist`"), + name: String @doc(description: "Deprecated."), + sharing_code: String @doc(description: "Deprecated: use field `sharing_code` from type `Wishlist`"), + updated_at: String @doc(description: "Deprecated: use field `updated_at` from type `Wishlist`") +} + +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") } From 7d03bdc3c8735446cb4f74d4219a9a54c11230b4 Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Wed, 25 Sep 2019 12:37:07 +0300 Subject: [PATCH 02/10] [Wishlist] Remove name from WishlistOutput #920 --- .../Resolver/CustomerWishlistsResolver.php | 67 +++++++++++ .../WishlistGraphQl/etc/schema.graphqls | 2 +- .../Wishlist/CustomerWishlistsTest.php | 110 ++++++++++++++++++ 3 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php new file mode 100644 index 000000000000..a2b8280fc3d0 --- /dev/null +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php @@ -0,0 +1,67 @@ +_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; + } +} diff --git a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls index 7daf15596f19..b7cc60fe100c 100644 --- a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls +++ b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls @@ -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") { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php new file mode 100644 index 000000000000..cdc774f08c2c --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php @@ -0,0 +1,110 @@ +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 = + <<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 = + <<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]; + } +} From 7d4a306702274f3d5f41154f0e57903c5136470b Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Thu, 26 Sep 2019 15:53:09 +0300 Subject: [PATCH 03/10] [Wishlist] Remove name from WishlistOutput #920 --- .../Magento/GraphQl/Wishlist/CustomerWishlistsTest.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php index cdc774f08c2c..2a6c70161a62 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php @@ -67,11 +67,10 @@ public function testGetCustomerWishlists(): void $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']); - + $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']); } /** From 4c85d8439053b9df7644f3e86105b4d5b2570522 Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Fri, 27 Sep 2019 13:43:20 +0300 Subject: [PATCH 04/10] [Wishlist] Remove name from WishlistOutput #920 --- .../WishlistGraphQl/etc/schema.graphqls | 10 +++--- .../Wishlist/CustomerWishlistsTest.php | 31 +++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls index b7cc60fe100c..28d80c4a2188 100644 --- a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls +++ b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls @@ -10,11 +10,11 @@ type Customer { } type WishlistOutput @doc(description: "Deprecated: 'Wishlist' type should be used instead") { - items: [WishlistItem] @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\WishlistItemsResolver") @doc(description: "Deprecated: use field `items` from type `Wishlist`"), - items_count: Int @doc(description: "Deprecated: use field `items_count` from type `Wishlist`"), - name: String @doc(description: "Deprecated."), - sharing_code: String @doc(description: "Deprecated: use field `sharing_code` from type `Wishlist`"), - updated_at: String @doc(description: "Deprecated: use field `updated_at` from type `Wishlist`") + 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"), + 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 { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php index 2a6c70161a62..2d6c3ff34b0a 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php @@ -73,6 +73,37 @@ public function testGetCustomerWishlists(): void $this->assertEquals('simple', $response['customer']['wishlists'][0]['items'][0]['product']['sku']); } + public function testCustomerWithoutWishlists(): void + { + $query = + <<graphQlQuery( + $query, + [], + '', + $this->getCustomerAuthHeaders('customer@example.com', 'password') + ); + + $this->assertEquals([], $response['customer']['wishlists']); + } + /** * @expectedException \Exception * @expectedExceptionMessage The current customer isn't authorized. From b52fe3e5f67a4e2d670a8599568da8266614b51f Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Fri, 27 Sep 2019 14:39:40 +0300 Subject: [PATCH 05/10] [Wishlist] Remove name from WishlistOutput #920 --- .../Magento/GraphQl/Wishlist/CustomerWishlistsTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php index 2d6c3ff34b0a..74b91cfb8520 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php @@ -73,6 +73,9 @@ public function testGetCustomerWishlists(): void $this->assertEquals('simple', $response['customer']['wishlists'][0]['items'][0]['product']['sku']); } + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + */ public function testCustomerWithoutWishlists(): void { $query = From 0977e9417605e7ffe7a37e9678142f16fde322ca Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Fri, 18 Oct 2019 09:06:14 +0300 Subject: [PATCH 06/10] [Wishlist] Remove name from WishlistOutput #920 --- .../Model/Resolver/CustomerWishlistsResolver.php | 10 ++++------ app/code/Magento/WishlistGraphQl/etc/schema.graphqls | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php index a2b8280fc3d0..ad32f953ef9c 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php @@ -41,18 +41,16 @@ public function resolve( array $value = null, array $args = null ) { - $customerId = $context->getUserId(); - /* Guest checking */ - if (!$customerId && 0 === $customerId) { + if (false === $context->getExtensionAttributes()->getIsCustomer()) { throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist')); } - $collection = $this->_wishlistCollectionFactory->create()->filterByCustomerId($customerId); - $wishlists = $collection->getItems(); + $collection = $this->_wishlistCollectionFactory->create()->filterByCustomerId($context->getUserId()); $wishlistsData = []; - if (0 === count($wishlists)) { + if (0 === $collection->getSize()) { return $wishlistsData; } + $wishlists = $collection->getItems(); foreach ($wishlists as $wishlist) { $wishlistsData [] = [ diff --git a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls index 28d80c4a2188..6a833efd7c2a 100644 --- a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls +++ b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls @@ -10,7 +10,7 @@ type Customer { } 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"), + 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"), 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"), From bff6344dea315ca27a9932cc84171ac5ca404bd6 Mon Sep 17 00:00:00 2001 From: Ievgenii Gryshkun Date: Fri, 18 Oct 2019 09:10:21 +0300 Subject: [PATCH 07/10] [Wishlist] Remove name from WishlistOutput #920 --- .../Model/Resolver/CustomerWishlistsResolver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php index ad32f953ef9c..3556eefe36a9 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php @@ -47,7 +47,7 @@ public function resolve( } $collection = $this->_wishlistCollectionFactory->create()->filterByCustomerId($context->getUserId()); $wishlistsData = []; - if (0 === $collection->getSize()) { + if (0 === $collection->getSize()) { return $wishlistsData; } $wishlists = $collection->getItems(); From 282b09b430ef06972e7adcaa4724e8041699c849 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Thu, 24 Oct 2019 10:31:30 -0500 Subject: [PATCH 08/10] magento/graphql-ce#920: [Wishlist] Remove name from WishlistOutput --- .../Model/Resolver/CustomerWishlistsResolver.php | 8 ++++---- app/code/Magento/WishlistGraphQl/etc/schema.graphqls | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php index 3556eefe36a9..804814c42481 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php @@ -21,14 +21,14 @@ class CustomerWishlistsResolver implements ResolverInterface /** * @var CollectionFactory */ - private $_wishlistCollectionFactory; + private $wishlistCollectionFactory; /** * @param CollectionFactory $wishlistCollectionFactory */ public function __construct(CollectionFactory $wishlistCollectionFactory) { - $this->_wishlistCollectionFactory = $wishlistCollectionFactory; + $this->wishlistCollectionFactory = $wishlistCollectionFactory; } /** @@ -43,9 +43,9 @@ public function resolve( ) { /* Guest checking */ if (false === $context->getExtensionAttributes()->getIsCustomer()) { - throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist')); + throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.')); } - $collection = $this->_wishlistCollectionFactory->create()->filterByCustomerId($context->getUserId()); + $collection = $this->wishlistCollectionFactory->create()->filterByCustomerId($context->getUserId()); $wishlistsData = []; if (0 === $collection->getSize()) { return $wishlistsData; diff --git a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls index 6a833efd7c2a..1009b6f803d9 100644 --- a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls +++ b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls @@ -9,10 +9,10 @@ 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") { +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"), 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"), + 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") } @@ -30,4 +30,4 @@ type WishlistItem { description: String @doc(description: "The customer's comment about this item"), added_at: String @doc(description: "The time when the customer added the item to the wish list"), product: ProductInterface @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\ProductResolver") -} \ No newline at end of file +} From acbc881498d4a32355382b44a1557943c3a7d345 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Tue, 29 Oct 2019 13:58:09 -0500 Subject: [PATCH 09/10] magento graphql-ce#920: Remove name from WishlistOutput --- .../Resolver/CustomerWishlistResolver.php | 57 ++++++++++++++++ .../Resolver/CustomerWishlistsResolver.php | 65 ------------------- .../WishlistGraphQl/etc/schema.graphqls | 5 +- ...listsTest.php => CustomerWishlistTest.php} | 38 +++++------ 4 files changed, 76 insertions(+), 89 deletions(-) create mode 100644 app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php delete mode 100644 app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php rename dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/{CustomerWishlistsTest.php => CustomerWishlistTest.php} (77%) diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php new file mode 100644 index 000000000000..1e2508fc8abe --- /dev/null +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php @@ -0,0 +1,57 @@ +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, + ]; + } +} diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php deleted file mode 100644 index 804814c42481..000000000000 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.php +++ /dev/null @@ -1,65 +0,0 @@ -wishlistCollectionFactory = $wishlistCollectionFactory; - } - - /** - * @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.')); - } - $collection = $this->wishlistCollectionFactory->create()->filterByCustomerId($context->getUserId()); - $wishlistsData = []; - if (0 === $collection->getSize()) { - return $wishlistsData; - } - $wishlists = $collection->getItems(); - - foreach ($wishlists as $wishlist) { - $wishlistsData [] = [ - 'sharing_code' => $wishlist->getSharingCode(), - 'updated_at' => $wishlist->getUpdatedAt(), - 'items_count' => $wishlist->getItemsCount(), - 'model' => $wishlist, - ]; - } - return $wishlistsData; - } -} diff --git a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls index 1009b6f803d9..deaa66921ba7 100644 --- a/app/code/Magento/WishlistGraphQl/etc/schema.graphqls +++ b/app/code/Magento/WishlistGraphQl/etc/schema.graphqls @@ -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") { @@ -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"), diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistTest.php similarity index 77% rename from dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php rename to dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistTest.php index 74b91cfb8520..0a22ddf39728 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistTest.php @@ -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 @@ -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(); @@ -46,7 +46,8 @@ public function testGetCustomerWishlists(): void { customer { - wishlists { + wishlist { + id items_count sharing_code updated_at @@ -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 = <<getCustomerAuthHeaders('customer@example.com', 'password') ); - $this->assertEquals([], $response['customer']['wishlists']); + $this->assertNotEmpty($response['customer']['wishlist']['id']); } /** From 6b443b4812fafd50d5a76589b00b4bd16b6df93e Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Tue, 29 Oct 2019 14:00:50 -0500 Subject: [PATCH 10/10] magento graphql-ce#920: Remove name from WishlistOutput --- .../Resolver/CustomerWishlistResolver.php | 3 +- .../GraphQl/Wishlist/CustomerWishlistTest.php | 47 +++++++++---------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php index 1e2508fc8abe..e866b9cead03 100644 --- a/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php +++ b/app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php @@ -14,7 +14,7 @@ use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; /** - * Fetches the Wishlists data according to the GraphQL schema + * Fetches customer wishlist data */ class CustomerWishlistResolver implements ResolverInterface { @@ -41,7 +41,6 @@ public function resolve( array $value = null, array $args = null ) { - /* Guest checking */ if (false === $context->getExtensionAttributes()->getIsCustomer()) { throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.')); } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistTest.php index 0a22ddf39728..fbd9c53faf7f 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistTest.php @@ -44,20 +44,19 @@ public function testCustomerWishlist(): void $query = <<