Skip to content

Commit 92118f5

Browse files
authored
Merge pull request #3703 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents b2f1417 + 81ba7a1 commit 92118f5

File tree

34 files changed

+1024
-67
lines changed

34 files changed

+1024
-67
lines changed

app/code/Magento/BundleGraphQl/Model/BundleProductTypeResolver.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@
88
namespace Magento\BundleGraphQl\Model;
99

1010
use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;
11+
use Magento\Bundle\Model\Product\Type as Type;
1112

1213
/**
13-
* {@inheritdoc}
14+
* @inheritdoc
1415
*/
1516
class BundleProductTypeResolver implements TypeResolverInterface
1617
{
18+
const BUNDLE_PRODUCT = 'BundleProduct';
19+
1720
/**
18-
* {@inheritdoc}
21+
* @inheritdoc
1922
*/
2023
public function resolveType(array $data) : string
2124
{
22-
if (isset($data['type_id']) && $data['type_id'] == 'bundle') {
23-
return 'BundleProduct';
25+
if (isset($data['type_id']) && $data['type_id'] == Type::TYPE_CODE) {
26+
return self::BUNDLE_PRODUCT;
2427
}
2528
return '';
2629
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,21 @@ public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId): \Iterato
101101

102102
$collection->addFieldToFilter('level', ['gt' => $level]);
103103
$collection->addFieldToFilter('level', ['lteq' => $level + $depth - self::DEPTH_OFFSET]);
104+
$collection->addAttributeToFilter('is_active', 1, "left");
104105
$collection->setOrder('level');
106+
$collection->setOrder(
107+
'position',
108+
$collection::SORT_ORDER_DESC
109+
);
105110
$collection->getSelect()->orWhere(
106-
$this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField() . ' = ?',
111+
$collection->getSelect()
112+
->getConnection()
113+
->quoteIdentifier(
114+
'e.' . $this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField()
115+
) . ' = ?',
107116
$rootCategoryId
108117
);
118+
109119
return $collection->getIterator();
110120
}
111121

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/ExtractDataFromCategoryTree.php

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ class ExtractDataFromCategoryTree
2020
*/
2121
private $categoryHydrator;
2222

23+
/**
24+
* @var CategoryInterface
25+
*/
26+
private $iteratingCategory;
27+
28+
/**
29+
* @var int
30+
*/
31+
private $startCategoryFetchLevel = 1;
32+
2333
/**
2434
* @param Hydrator $categoryHydrator
2535
*/
@@ -42,14 +52,60 @@ public function execute(\Iterator $iterator): array
4252
/** @var CategoryInterface $category */
4353
$category = $iterator->current();
4454
$iterator->next();
45-
$nextCategory = $iterator->current();
46-
$tree[$category->getId()] = $this->categoryHydrator->hydrateCategory($category);
47-
$tree[$category->getId()]['model'] = $category;
48-
if ($nextCategory && (int) $nextCategory->getLevel() !== (int) $category->getLevel()) {
49-
$tree[$category->getId()]['children'] = $this->execute($iterator);
55+
$pathElements = explode("/", $category->getPath());
56+
if (empty($tree)) {
57+
$this->startCategoryFetchLevel = count($pathElements) - 1;
58+
}
59+
$this->iteratingCategory = $category;
60+
$currentLevelTree = $this->explodePathToArray($pathElements, $this->startCategoryFetchLevel);
61+
if (empty($tree)) {
62+
$tree = $currentLevelTree;
63+
}
64+
$tree = $this->mergeCategoriesTrees($currentLevelTree, $tree);
65+
}
66+
return $tree;
67+
}
68+
69+
/**
70+
* Merge together complex categories trees
71+
*
72+
* @param array $tree1
73+
* @param array $tree2
74+
* @return array
75+
*/
76+
private function mergeCategoriesTrees(array &$tree1, array &$tree2): array
77+
{
78+
$mergedTree = $tree1;
79+
foreach ($tree2 as $currentKey => &$value) {
80+
if (is_array($value) && isset($mergedTree[$currentKey]) && is_array($mergedTree[$currentKey])) {
81+
$mergedTree[$currentKey] = $this->mergeCategoriesTrees($mergedTree[$currentKey], $value);
82+
} else {
83+
$mergedTree[$currentKey] = $value;
5084
}
5185
}
86+
return $mergedTree;
87+
}
5288

89+
/**
90+
* Recursive method to generate tree for one category path
91+
*
92+
* @param array $pathElements
93+
* @param int $index
94+
* @return array
95+
*/
96+
private function explodePathToArray(array $pathElements, int $index): array
97+
{
98+
$tree = [];
99+
$tree[$pathElements[$index]]['id'] = $pathElements[$index];
100+
if ($index === count($pathElements) - 1) {
101+
$tree[$pathElements[$index]] = $this->categoryHydrator->hydrateCategory($this->iteratingCategory);
102+
$tree[$pathElements[$index]]['model'] = $this->iteratingCategory;
103+
}
104+
$currentIndex = $index;
105+
$index++;
106+
if (isset($pathElements[$index])) {
107+
$tree[$pathElements[$currentIndex]]['children'] = $this->explodePathToArray($pathElements, $index);
108+
}
53109
return $tree;
54110
}
55111
}

app/code/Magento/ConfigurableProductGraphQl/Model/Options/Collection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ private function fetch() : array
124124
$this->attributeMap[$productId][$attribute->getId()]['attribute_code']
125125
= $attribute->getProductAttribute()->getAttributeCode();
126126
$this->attributeMap[$productId][$attribute->getId()]['values'] = $attributeData['options'];
127+
$this->attributeMap[$productId][$attribute->getId()]['label']
128+
= $attribute->getProductAttribute()->getStoreLabel();
127129
}
128130

129131
return $this->attributeMap;

app/code/Magento/ConfigurableProductGraphQl/Model/Resolver/ConfigurableVariant.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ public function __construct(
7171
}
7272

7373
/**
74-
* Fetch and format configurable variants.
75-
*
76-
* {@inheritDoc}
74+
* @inheritdoc
7775
*/
7876
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
7977
{
@@ -85,7 +83,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8583
return $this->valueFactory->create($result);
8684
}
8785

88-
$this->variantCollection->addParentId((int)$value[$linkField]);
86+
$this->variantCollection->addParentProduct($value['model']);
8987
$fields = $this->getProductFields($info);
9088
$matchedFields = $this->attributeCollection->getRequestAttributes($fields);
9189
$this->variantCollection->addEavAttributes($matchedFields);

app/code/Magento/ConfigurableProductGraphQl/Model/Resolver/Variant/Attributes.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\ConfigurableProductGraphQl\Model\Resolver\Variant;
99

10+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
11+
use Magento\Framework\GraphQl\Query\Resolver\Value;
1012
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1113
use Magento\Framework\GraphQl\Config\Element\Field;
1214
use Magento\Framework\GraphQl\Query\ResolverInterface;
@@ -17,9 +19,17 @@
1719
class Attributes implements ResolverInterface
1820
{
1921
/**
22+
* @inheritdoc
23+
*
2024
* Format product's option data to conform to GraphQL schema
2125
*
22-
* {@inheritdoc}
26+
* @param Field $field
27+
* @param ContextInterface $context
28+
* @param ResolveInfo $info
29+
* @param array|null $value
30+
* @param array|null $args
31+
* @throws \Exception
32+
* @return mixed|Value
2333
*/
2434
public function resolve(
2535
Field $field,
@@ -35,12 +45,12 @@ public function resolve(
3545
$data = [];
3646
foreach ($value['options'] as $option) {
3747
$code = $option['attribute_code'];
38-
if (!isset($value['product'][$code])) {
48+
if (!isset($value['product']['model'][$code])) {
3949
continue;
4050
}
4151

4252
foreach ($option['values'] as $optionValue) {
43-
if ($optionValue['value_index'] != $value['product'][$code]) {
53+
if ($optionValue['value_index'] != $value['product']['model'][$code]) {
4454
continue;
4555
}
4656
$data[] = [

app/code/Magento/ConfigurableProductGraphQl/Model/Variant/Collection.php

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
use Magento\Catalog\Api\Data\ProductInterface;
1111
use Magento\Catalog\Model\Product;
12-
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\CollectionFactory;
13-
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\Collection as ChildCollection;
1412
use Magento\Catalog\Model\ProductFactory;
13+
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\Collection as ChildCollection;
14+
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\CollectionFactory;
1515
use Magento\Framework\EntityManager\MetadataPool;
1616
use Magento\Framework\Api\SearchCriteriaBuilder;
1717
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product as DataProvider;
@@ -47,9 +47,9 @@ class Collection
4747
private $metadataPool;
4848

4949
/**
50-
* @var int[]
50+
* @var Product[]
5151
*/
52-
private $parentIds = [];
52+
private $parentProducts = [];
5353

5454
/**
5555
* @var array
@@ -83,19 +83,21 @@ public function __construct(
8383
}
8484

8585
/**
86-
* Add parent Id to collection filter
86+
* Add parent to collection filter
8787
*
88-
* @param int $id
88+
* @param Product $product
8989
* @return void
9090
*/
91-
public function addParentId(int $id) : void
91+
public function addParentProduct(Product $product) : void
9292
{
93-
if (!in_array($id, $this->parentIds) && !empty($this->childrenMap)) {
93+
if (isset($this->parentProducts[$product->getId()])) {
94+
return;
95+
}
96+
97+
if (!empty($this->childrenMap)) {
9498
$this->childrenMap = [];
95-
$this->parentIds[] = $id;
96-
} elseif (!in_array($id, $this->parentIds)) {
97-
$this->parentIds[] = $id;
9899
}
100+
$this->parentProducts[$product->getId()] = $product;
99101
}
100102

101103
/**
@@ -130,20 +132,23 @@ public function getChildProductsByParentId(int $id) : array
130132
* Fetch all children products from parent id's.
131133
*
132134
* @return array
135+
* @throws \Exception
133136
*/
134137
private function fetch() : array
135138
{
136-
if (empty($this->parentIds) || !empty($this->childrenMap)) {
139+
if (empty($this->parentProducts) || !empty($this->childrenMap)) {
137140
return $this->childrenMap;
138141
}
139142

140143
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
141-
foreach ($this->parentIds as $id) {
144+
foreach ($this->parentProducts as $product) {
145+
$attributeData = $this->getAttributesCodes($product);
142146
/** @var ChildCollection $childCollection */
143147
$childCollection = $this->childCollectionFactory->create();
148+
$childCollection->addAttributeToSelect($attributeData);
149+
144150
/** @var Product $product */
145-
$product = $this->productFactory->create();
146-
$product->setData($linkField, $id);
151+
$product->setData($linkField, $product->getId());
147152
$childCollection->setProductFilter($product);
148153

149154
/** @var Product $childProduct */
@@ -160,4 +165,24 @@ private function fetch() : array
160165

161166
return $this->childrenMap;
162167
}
168+
169+
/**
170+
* Get attributes code
171+
*
172+
* @param \Magento\Catalog\Model\Product $currentProduct
173+
* @return array
174+
*/
175+
private function getAttributesCodes(Product $currentProduct): array
176+
{
177+
$attributeCodes = [];
178+
$allowAttributes = $currentProduct->getTypeInstance()->getConfigurableAttributes($currentProduct);
179+
foreach ($allowAttributes as $attribute) {
180+
$productAttribute = $attribute->getProductAttribute();
181+
if (!\in_array($productAttribute->getAttributeCode(), $attributeCodes)) {
182+
$attributeCodes[] = $productAttribute->getAttributeCode();
183+
}
184+
}
185+
186+
return $attributeCodes;
187+
}
163188
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\DirectoryGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\Reflection\DataObjectProcessor;
14+
use Magento\Directory\Api\CountryInformationAcquirerInterface;
15+
use Magento\Directory\Api\Data\CountryInformationInterface;
16+
17+
/**
18+
* Countries field resolver, used for GraphQL request processing.
19+
*/
20+
class Countries implements ResolverInterface
21+
{
22+
/**
23+
* @var DataObjectProcessor
24+
*/
25+
private $dataProcessor;
26+
27+
/**
28+
* @var CountryInformationAcquirerInterface
29+
*/
30+
private $countryInformationAcquirer;
31+
32+
/**
33+
* @param DataObjectProcessor $dataProcessor
34+
* @param CountryInformationAcquirerInterface $countryInformationAcquirer
35+
*/
36+
public function __construct(
37+
DataObjectProcessor $dataProcessor,
38+
CountryInformationAcquirerInterface $countryInformationAcquirer
39+
) {
40+
$this->dataProcessor = $dataProcessor;
41+
$this->countryInformationAcquirer = $countryInformationAcquirer;
42+
}
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
public function resolve(
48+
Field $field,
49+
$context,
50+
ResolveInfo $info,
51+
array $value = null,
52+
array $args = null
53+
) {
54+
$countries = $this->countryInformationAcquirer->getCountriesInfo();
55+
56+
$output = [];
57+
foreach ($countries as $country) {
58+
$output[] = $this->dataProcessor->buildOutputDataArray($country, CountryInformationInterface::class);
59+
}
60+
61+
return $output;
62+
}
63+
}

0 commit comments

Comments
 (0)