Skip to content

Commit 6fc726b

Browse files
author
Valeriy Naida
authored
Merge pull request #3461 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents ced96c1 + 370aaeb commit 6fc726b

File tree

17 files changed

+1239
-8
lines changed

17 files changed

+1239
-8
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\CatalogGraphQl\Model;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
12+
/**
13+
* Product data provider
14+
*
15+
* TODO: will be replaces on deferred mechanism
16+
*/
17+
class ProductDataProvider
18+
{
19+
/**
20+
* @var ProductRepositoryInterface
21+
*/
22+
private $productRepository;
23+
24+
/**
25+
* @param ProductRepositoryInterface $productRepository
26+
*/
27+
public function __construct(ProductRepositoryInterface $productRepository)
28+
{
29+
$this->productRepository = $productRepository;
30+
}
31+
32+
/**
33+
* Get product data by id
34+
*
35+
* @param int $productId
36+
* @return array
37+
*/
38+
public function getProductDataById(int $productId): array
39+
{
40+
$product = $this->productRepository->getById($productId);
41+
$productData = $product->toArray();
42+
$productData['model'] = $product;
43+
return $productData;
44+
}
45+
}

app/code/Magento/GraphQl/etc/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,10 @@
9797
</argument>
9898
</arguments>
9999
</type>
100+
<type name="Magento\Framework\GraphQl\Query\QueryComplexityLimiter">
101+
<arguments>
102+
<argument name="queryDepth" xsi:type="number">20</argument>
103+
<argument name="queryComplexity" xsi:type="number">300</argument>
104+
</arguments>
105+
</type>
100106
</config>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\CatalogGraphQl\Model\ProductDataProvider;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\Wishlist\Model\Item;
16+
17+
/**
18+
* Fetches the Product data according to the GraphQL schema
19+
*/
20+
class ProductResolver implements ResolverInterface
21+
{
22+
/**
23+
* @var ProductDataProvider
24+
*/
25+
private $productDataProvider;
26+
27+
/**
28+
* @param ProductDataProvider $productDataProvider
29+
*/
30+
public function __construct(ProductDataProvider $productDataProvider)
31+
{
32+
$this->productDataProvider = $productDataProvider;
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function resolve(
39+
Field $field,
40+
$context,
41+
ResolveInfo $info,
42+
array $value = null,
43+
array $args = null
44+
) {
45+
if (!isset($value['model'])) {
46+
throw new LocalizedException(__('Missing key "model" in Wishlist Item value data'));
47+
}
48+
/** @var Item $wishlistItem */
49+
$wishlistItem = $value['model'];
50+
51+
return $this->productDataProvider->getProductDataById((int)$wishlistItem->getProductId());
52+
}
53+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\Store\Api\Data\StoreInterface;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\Wishlist\Model\ResourceModel\Item\Collection as WishlistItemCollection;
17+
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory as WishlistItemCollectionFactory;
18+
use Magento\Wishlist\Model\Item;
19+
use Magento\Wishlist\Model\Wishlist;
20+
21+
/**
22+
* Fetches the Wishlist Items data according to the GraphQL schema
23+
*/
24+
class WishlistItemsResolver implements ResolverInterface
25+
{
26+
/**
27+
* @var WishlistItemCollectionFactory
28+
*/
29+
private $wishlistItemCollectionFactory;
30+
31+
/**
32+
* @var StoreManagerInterface
33+
*/
34+
private $storeManager;
35+
36+
/**
37+
* @param WishlistItemCollectionFactory $wishlistItemCollectionFactory
38+
* @param StoreManagerInterface $storeManager
39+
*/
40+
public function __construct(
41+
WishlistItemCollectionFactory $wishlistItemCollectionFactory,
42+
StoreManagerInterface $storeManager
43+
) {
44+
$this->wishlistItemCollectionFactory = $wishlistItemCollectionFactory;
45+
$this->storeManager = $storeManager;
46+
}
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
public function resolve(
52+
Field $field,
53+
$context,
54+
ResolveInfo $info,
55+
array $value = null,
56+
array $args = null
57+
) {
58+
if (!isset($value['model'])) {
59+
throw new LocalizedException(__('Missing key "model" in Wishlist value data'));
60+
}
61+
/** @var Wishlist $wishlist */
62+
$wishlist = $value['model'];
63+
64+
$wishlistItems = $this->getWishListItems($wishlist);
65+
66+
$data = [];
67+
foreach ($wishlistItems as $wishlistItem) {
68+
$data[] = [
69+
'id' => $wishlistItem->getId(),
70+
'qty' => $wishlistItem->getData('qty'),
71+
'description' => $wishlistItem->getDescription(),
72+
'added_at' => $wishlistItem->getAddedAt(),
73+
'model' => $wishlistItem,
74+
];
75+
}
76+
return $data;
77+
}
78+
79+
/**
80+
* Get wishlist items
81+
*
82+
* @param Wishlist $wishlist
83+
* @return Item[]
84+
*/
85+
private function getWishListItems(Wishlist $wishlist): array
86+
{
87+
/** @var WishlistItemCollection $wishlistItemCollection */
88+
$wishlistItemCollection = $this->wishlistItemCollectionFactory->create();
89+
$wishlistItemCollection
90+
->addWishlistFilter($wishlist)
91+
->addStoreFilter(array_map(function (StoreInterface $store) {
92+
return $store->getId();
93+
}, $this->storeManager->getStores()))
94+
->setVisibilityFilter();
95+
return $wishlistItemCollection->getItems();
96+
}
97+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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\ResourceModel\Wishlist as WishlistResourceModel;
14+
use Magento\Wishlist\Model\Wishlist;
15+
use Magento\Wishlist\Model\WishlistFactory;
16+
17+
/**
18+
* Fetches the Wishlist data according to the GraphQL schema
19+
*/
20+
class WishlistResolver implements ResolverInterface
21+
{
22+
/**
23+
* @var WishlistResourceModel
24+
*/
25+
private $wishlistResource;
26+
27+
/**
28+
* @var WishlistFactory
29+
*/
30+
private $wishlistFactory;
31+
32+
/**
33+
* @param WishlistResourceModel $wishlistResource
34+
* @param WishlistFactory $wishlistFactory
35+
*/
36+
public function __construct(WishlistResourceModel $wishlistResource, WishlistFactory $wishlistFactory)
37+
{
38+
$this->wishlistResource = $wishlistResource;
39+
$this->wishlistFactory = $wishlistFactory;
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
public function resolve(
46+
Field $field,
47+
$context,
48+
ResolveInfo $info,
49+
array $value = null,
50+
array $args = null
51+
) {
52+
$customerId = $context->getUserId();
53+
54+
/** @var Wishlist $wishlist */
55+
$wishlist = $this->wishlistFactory->create();
56+
$this->wishlistResource->load($wishlist, $customerId, 'customer_id');
57+
58+
if (null === $wishlist->getId()) {
59+
return [];
60+
}
61+
62+
return [
63+
'sharing_code' => $wishlist->getSharingCode(),
64+
'updated_at' => $wishlist->getUpdatedAt(),
65+
'items_count' => $wishlist->getItemsCount(),
66+
'name' => $wishlist->getName(),
67+
'model' => $wishlist,
68+
];
69+
}
70+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# WishlistGraphQl
2+
3+
**WishlistGraphQl** provides type information for the GraphQl module
4+
to generate wishlist fields.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "magento/module-wishlist-graph-ql",
3+
"description": "N/A",
4+
"type": "magento2-module",
5+
"require": {
6+
"php": "~7.1.3||~7.2.0",
7+
"magento/framework": "*",
8+
"magento/module-catalog-graph-ql": "*",
9+
"magento/module-wishlist": "*",
10+
"magento/module-store": "*"
11+
},
12+
"license": [
13+
"OSL-3.0",
14+
"AFL-3.0"
15+
],
16+
"autoload": {
17+
"files": [
18+
"registration.php"
19+
],
20+
"psr-4": {
21+
"Magento\\WishlistGraphQl\\": ""
22+
}
23+
}
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="Magento_WishlistGraphQl" />
10+
</config>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
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")
6+
}
7+
8+
type WishlistOutput {
9+
items: [WishlistItem] @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\WishlistItemsResolver") @doc(description: "An array of items in the customer's wish list"),
10+
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"),
12+
sharing_code: String @doc(description: "An encrypted code that Magento uses to link to the wish list"),
13+
updated_at: String @doc(description: "The time of the last modification to the wish list")
14+
}
15+
16+
type WishlistItem {
17+
id: Int @doc(description: "The wish list item ID")
18+
qty: Float @doc(description: "The quantity of this wish list item"),
19+
description: String @doc(description: "The customer's comment about this item"),
20+
added_at: String @doc(description: "The time when the customer added the item to the wish list"),
21+
product: ProductInterface @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\ProductResolver")
22+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
use Magento\Framework\Component\ComponentRegistrar;
9+
10+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_WishlistGraphQl', __DIR__);

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238
"magento/module-weee": "*",
239239
"magento/module-widget": "*",
240240
"magento/module-wishlist": "*",
241+
"magento/module-wishlist-graph-ql": "*",
241242
"magento/module-wishlist-analytics": "*",
242243
"magento/theme-adminhtml-backend": "*",
243244
"magento/theme-frontend-blank": "*",

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)