Skip to content

Commit c3f16cb

Browse files
committed
Merge branch 'graphql-develop-prs' of github.com:magento-engcom/magento2ce into graphql-develop-prs
2 parents 71e751d + dbfa957 commit c3f16cb

File tree

22 files changed

+646
-28
lines changed

22 files changed

+646
-28
lines changed

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;
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+
}
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\DirectoryGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
12+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
13+
use Magento\Framework\GraphQl\Config\Element\Field;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\Reflection\DataObjectProcessor;
16+
use Magento\Directory\Api\CountryInformationAcquirerInterface;
17+
use Magento\Directory\Api\Data\CountryInformationInterface;
18+
19+
/**
20+
* Country field resolver, used for GraphQL request processing.
21+
*/
22+
class Country implements ResolverInterface
23+
{
24+
/**
25+
* @var DataObjectProcessor
26+
*/
27+
private $dataProcessor;
28+
29+
/**
30+
* @var CountryInformationAcquirerInterface
31+
*/
32+
private $countryInformationAcquirer;
33+
34+
/**
35+
* @param DataObjectProcessor $dataProcessor
36+
* @param CountryInformationAcquirerInterface $countryInformationAcquirer
37+
*/
38+
public function __construct(
39+
DataObjectProcessor $dataProcessor,
40+
CountryInformationAcquirerInterface $countryInformationAcquirer
41+
) {
42+
$this->dataProcessor = $dataProcessor;
43+
$this->countryInformationAcquirer = $countryInformationAcquirer;
44+
}
45+
46+
/**
47+
* @inheritdoc
48+
*/
49+
public function resolve(
50+
Field $field,
51+
$context,
52+
ResolveInfo $info,
53+
array $value = null,
54+
array $args = null
55+
) {
56+
try {
57+
$country = $this->countryInformationAcquirer->getCountryInfo($args['id']);
58+
} catch (NoSuchEntityException $exception) {
59+
throw new GraphQlNoSuchEntityException(__($exception->getMessage()), $exception);
60+
}
61+
62+
return $this->dataProcessor->buildOutputDataArray(
63+
$country,
64+
CountryInformationInterface::class
65+
);
66+
}
67+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\CurrencyInformationAcquirerInterface;
15+
use Magento\Directory\Api\Data\CurrencyInformationInterface;
16+
17+
/**
18+
* Currency field resolver, used for GraphQL request processing.
19+
*/
20+
class Currency implements ResolverInterface
21+
{
22+
/**
23+
* @var DataObjectProcessor
24+
*/
25+
private $dataProcessor;
26+
27+
/**
28+
* @var CurrencyInformationAcquirerInterface
29+
*/
30+
private $currencyInformationAcquirer;
31+
32+
/**
33+
* @param DataObjectProcessor $dataProcessor
34+
* @param CurrencyInformationAcquirerInterface $currencyInformationAcquirer
35+
*/
36+
public function __construct(
37+
DataObjectProcessor $dataProcessor,
38+
CurrencyInformationAcquirerInterface $currencyInformationAcquirer
39+
) {
40+
$this->dataProcessor = $dataProcessor;
41+
$this->currencyInformationAcquirer = $currencyInformationAcquirer;
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+
return $this->dataProcessor->buildOutputDataArray(
55+
$this->currencyInformationAcquirer->getCurrencyInfo(),
56+
CurrencyInformationInterface::class
57+
);
58+
}
59+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# DirectoryGraphQl
2+
3+
**DirectoryGraphQl** provides type and resolver information for the GraphQl module
4+
to generate directory information endpoints.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Directory Graph Ql Functional Tests
2+
3+
The Functional Test Module for **Magento Directory Graph Ql** module.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "magento/module-directory-graph-ql",
3+
"description": "N/A",
4+
"type": "magento2-module",
5+
"require": {
6+
"php": "~7.1.3||~7.2.0",
7+
"magento/module-directory": "*",
8+
"magento/framework": "*"
9+
},
10+
"suggest": {
11+
"magento/module-graph-ql": "*"
12+
},
13+
"license": [
14+
"OSL-3.0",
15+
"AFL-3.0"
16+
],
17+
"autoload": {
18+
"files": [
19+
"registration.php"
20+
],
21+
"psr-4": {
22+
"Magento\\DirectoryGraphQl\\": ""
23+
}
24+
}
25+
}
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_DirectoryGraphQl"/>
10+
</config>

0 commit comments

Comments
 (0)