Skip to content

Commit 9fc254f

Browse files
authored
Merge pull request #4400 from magento-engcom/graphql-develop-prs
[Magento Community Engineering] Community Contributions - GraphQL
2 parents 5027a80 + 275699f commit 9fc254f

File tree

25 files changed

+1095
-264
lines changed

25 files changed

+1095
-264
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\Resolver\Category;
9+
10+
use Magento\Catalog\Api\Data\CategoryInterface;
11+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
12+
use Magento\Framework\EntityManager\MetadataPool;
13+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
14+
15+
/**
16+
* Check if category is active.
17+
*/
18+
class CheckCategoryIsActive
19+
{
20+
/**
21+
* @var CollectionFactory
22+
*/
23+
private $collectionFactory;
24+
25+
/**
26+
* @var MetadataPool
27+
*/
28+
private $metadata;
29+
30+
/**
31+
* @param CollectionFactory $collectionFactory
32+
* @param MetadataPool $metadata
33+
*/
34+
public function __construct(
35+
CollectionFactory $collectionFactory,
36+
MetadataPool $metadata
37+
) {
38+
$this->collectionFactory = $collectionFactory;
39+
$this->metadata = $metadata;
40+
}
41+
42+
/**
43+
* Check if category is active.
44+
*
45+
* @param int $categoryId
46+
* @throws GraphQlNoSuchEntityException
47+
*/
48+
public function execute(int $categoryId): void
49+
{
50+
$collection = $this->collectionFactory->create();
51+
$collection->addAttributeToFilter(CategoryInterface::KEY_IS_ACTIVE, ['eq' => 1])
52+
->getSelect()
53+
->where(
54+
$collection->getSelect()
55+
->getConnection()
56+
->quoteIdentifier(
57+
'e.' .
58+
$this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField()
59+
) . ' = ?',
60+
$categoryId
61+
);
62+
63+
if ($collection->count() === 0) {
64+
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
65+
}
66+
}
67+
}

app/code/Magento/CatalogGraphQl/Model/Resolver/CategoryTree.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver;
99

10+
use Magento\Catalog\Model\Category;
11+
use Magento\CatalogGraphQl\Model\Resolver\Category\CheckCategoryIsActive;
1012
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ExtractDataFromCategoryTree;
1113
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1214
use Magento\Framework\GraphQl\Config\Element\Field;
@@ -34,16 +36,24 @@ class CategoryTree implements ResolverInterface
3436
*/
3537
private $extractDataFromCategoryTree;
3638

39+
/**
40+
* @var CheckCategoryIsActive
41+
*/
42+
private $checkCategoryIsActive;
43+
3744
/**
3845
* @param \Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree
3946
* @param ExtractDataFromCategoryTree $extractDataFromCategoryTree
47+
* @param CheckCategoryIsActive $checkCategoryIsActive
4048
*/
4149
public function __construct(
4250
\Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree,
43-
ExtractDataFromCategoryTree $extractDataFromCategoryTree
51+
ExtractDataFromCategoryTree $extractDataFromCategoryTree,
52+
CheckCategoryIsActive $checkCategoryIsActive
4453
) {
4554
$this->categoryTree = $categoryTree;
4655
$this->extractDataFromCategoryTree = $extractDataFromCategoryTree;
56+
$this->checkCategoryIsActive = $checkCategoryIsActive;
4757
}
4858

4959
/**
@@ -72,6 +82,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7282
}
7383

7484
$rootCategoryId = $this->getCategoryId($args);
85+
if ($rootCategoryId !== Category::TREE_ROOT_ID) {
86+
$this->checkCategoryIsActive->execute($rootCategoryId);
87+
}
7588
$categoriesTree = $this->categoryTree->getTree($info, $rootCategoryId);
7689

7790
if (empty($categoriesTree) || ($categoriesTree->count() == 0)) {

app/code/Magento/GraphQl/etc/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type Mutation {
99

1010
input FilterTypeInput @doc(description: "FilterTypeInput specifies which action will be performed in a query ") {
1111
eq: String @doc(description: "Equals")
12-
finset: [String] @doc(description: "Find in set. The value can contain a set of comma-separated values")
12+
finset: [String] @deprecated (reason: "The finset filter is deprecated. Magento doesn't recomend to store comma separated values, therefore finset filter is redundant.")
1313
from: String @doc(description: "From. Must be used with 'to'")
1414
gt: String @doc(description: "Greater than")
1515
gteq: String @doc(description: "Greater than or equal to")

app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php

Lines changed: 8 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
namespace Magento\QuoteGraphQl\Model\Cart;
99

1010
use Magento\Catalog\Api\ProductRepositoryInterface;
11-
use Magento\Framework\DataObject;
12-
use Magento\Framework\DataObjectFactory;
1311
use Magento\Framework\Exception\NoSuchEntityException;
1412
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1513
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
@@ -21,25 +19,25 @@
2119
class AddSimpleProductToCart
2220
{
2321
/**
24-
* @var DataObjectFactory
22+
* @var CreateBuyRequest
2523
*/
26-
private $dataObjectFactory;
24+
private $createBuyRequest;
2725

2826
/**
2927
* @var ProductRepositoryInterface
3028
*/
3129
private $productRepository;
3230

3331
/**
34-
* @param DataObjectFactory $dataObjectFactory
3532
* @param ProductRepositoryInterface $productRepository
33+
* @param CreateBuyRequest $createBuyRequest
3634
*/
3735
public function __construct(
38-
DataObjectFactory $dataObjectFactory,
39-
ProductRepositoryInterface $productRepository
36+
ProductRepositoryInterface $productRepository,
37+
CreateBuyRequest $createBuyRequest
4038
) {
41-
$this->dataObjectFactory = $dataObjectFactory;
4239
$this->productRepository = $productRepository;
40+
$this->createBuyRequest = $createBuyRequest;
4341
}
4442

4543
/**
@@ -56,7 +54,7 @@ public function execute(Quote $cart, array $cartItemData): void
5654
{
5755
$sku = $this->extractSku($cartItemData);
5856
$quantity = $this->extractQuantity($cartItemData);
59-
$customizableOptions = $this->extractCustomizableOptions($cartItemData);
57+
$customizableOptions = $cartItemData['customizable_options'] ?? [];
6058

6159
try {
6260
$product = $this->productRepository->get($sku);
@@ -65,7 +63,7 @@ public function execute(Quote $cart, array $cartItemData): void
6563
}
6664

6765
try {
68-
$result = $cart->addProduct($product, $this->createBuyRequest($quantity, $customizableOptions));
66+
$result = $cart->addProduct($product, $this->createBuyRequest->execute($quantity, $customizableOptions));
6967
} catch (\Exception $e) {
7068
throw new GraphQlInputException(
7169
__(
@@ -116,60 +114,4 @@ private function extractQuantity(array $cartItemData): float
116114
}
117115
return $quantity;
118116
}
119-
120-
/**
121-
* Extract Customizable Options from cart item data
122-
*
123-
* @param array $cartItemData
124-
* @return array
125-
*/
126-
private function extractCustomizableOptions(array $cartItemData): array
127-
{
128-
if (!isset($cartItemData['customizable_options']) || empty($cartItemData['customizable_options'])) {
129-
return [];
130-
}
131-
132-
$customizableOptionsData = [];
133-
foreach ($cartItemData['customizable_options'] as $customizableOption) {
134-
if (isset($customizableOption['value_string'])) {
135-
$customizableOptionsData[$customizableOption['id']] = $this->convertCustomOptionValue(
136-
$customizableOption['value_string']
137-
);
138-
}
139-
}
140-
return $customizableOptionsData;
141-
}
142-
143-
/**
144-
* Format GraphQl input data to a shape that buy request has
145-
*
146-
* @param float $quantity
147-
* @param array $customOptions
148-
* @return DataObject
149-
*/
150-
private function createBuyRequest(float $quantity, array $customOptions): DataObject
151-
{
152-
return $this->dataObjectFactory->create([
153-
'data' => [
154-
'qty' => $quantity,
155-
'options' => $customOptions,
156-
],
157-
]);
158-
}
159-
160-
/**
161-
* Convert custom options vakue
162-
*
163-
* @param string $value
164-
* @return string|array
165-
*/
166-
private function convertCustomOptionValue(string $value)
167-
{
168-
$value = trim($value);
169-
if (substr($value, 0, 1) === "[" &&
170-
substr($value, strlen($value) - 1, 1) === "]") {
171-
return explode(',', substr($value, 1, -1));
172-
}
173-
return $value;
174-
}
175117
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Framework\DataObject;
11+
use Magento\Framework\DataObjectFactory;
12+
13+
/**
14+
* Creates buy request that can be used for working with cart items
15+
*/
16+
class CreateBuyRequest
17+
{
18+
/**
19+
* @var DataObjectFactory
20+
*/
21+
private $dataObjectFactory;
22+
23+
/**
24+
* @param DataObjectFactory $dataObjectFactory
25+
*/
26+
public function __construct(
27+
DataObjectFactory $dataObjectFactory
28+
) {
29+
$this->dataObjectFactory = $dataObjectFactory;
30+
}
31+
32+
/**
33+
* Returns buy request for working with cart items
34+
*
35+
* @param float $qty
36+
* @param array $customizableOptionsData
37+
* @return DataObject
38+
*/
39+
public function execute(float $qty, array $customizableOptionsData): DataObject
40+
{
41+
$customizableOptions = [];
42+
foreach ($customizableOptionsData as $customizableOption) {
43+
if (isset($customizableOption['value_string'])) {
44+
$customizableOptions[$customizableOption['id']] = $this->convertCustomOptionValue(
45+
$customizableOption['value_string']
46+
);
47+
}
48+
}
49+
50+
return $this->dataObjectFactory->create(
51+
[
52+
'data' => [
53+
'qty' => $qty,
54+
'options' => $customizableOptions,
55+
],
56+
]
57+
);
58+
}
59+
60+
/**
61+
* Convert custom options value
62+
*
63+
* @param string $value
64+
* @return string|array
65+
*/
66+
private function convertCustomOptionValue(string $value)
67+
{
68+
$value = trim($value);
69+
if (substr($value, 0, 1) === "[" &&
70+
substr($value, strlen($value) - 1, 1) === "]") {
71+
return explode(',', substr($value, 1, -1));
72+
}
73+
return $value;
74+
}
75+
}

0 commit comments

Comments
 (0)