Skip to content

Commit

Permalink
Merge pull request #3627 from magento-engcom/graphql-develop-prs
Browse files Browse the repository at this point in the history
[EngCom] Public Pull Requests - GraphQL
  • Loading branch information
naydav authored Jan 24, 2019
2 parents 1fd2493 + 6ee10ce commit 0122d0b
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,21 @@ public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId): \Iterato

$collection->addFieldToFilter('level', ['gt' => $level]);
$collection->addFieldToFilter('level', ['lteq' => $level + $depth - self::DEPTH_OFFSET]);
$collection->addIsActiveFilter();
$collection->setOrder('level');
$collection->setOrder(
'position',
$collection::SORT_ORDER_DESC
);
$collection->getSelect()->orWhere(
$this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField() . ' = ?',
$collection->getSelect()
->getConnection()
->quoteIdentifier(
'e.' . $this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField()
) . ' = ?',
$rootCategoryId
);

return $collection->getIterator();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ class ExtractDataFromCategoryTree
*/
private $categoryHydrator;

/**
* @var CategoryInterface;
*/
private $iteratingCategory;

/**
* @var int
*/
private $startCategoryFetchLevel = 1;

/**
* @param Hydrator $categoryHydrator
*/
Expand All @@ -42,14 +52,63 @@ public function execute(\Iterator $iterator): array
/** @var CategoryInterface $category */
$category = $iterator->current();
$iterator->next();
$nextCategory = $iterator->current();
$tree[$category->getId()] = $this->categoryHydrator->hydrateCategory($category);
$tree[$category->getId()]['model'] = $category;
if ($nextCategory && (int) $nextCategory->getLevel() !== (int) $category->getLevel()) {
$tree[$category->getId()]['children'] = $this->execute($iterator);

$pathElements = explode("/", $category->getPath());
if (empty($tree)) {
$this->startCategoryFetchLevel = count($pathElements) - 1;
}

$this->iteratingCategory = $category;
$currentLevelTree = $this->explodePathToArray($pathElements, $this->startCategoryFetchLevel);
if (empty($tree)) {
$tree = $currentLevelTree;
}
$tree = $this->mergeCategoriesTrees($currentLevelTree, $tree);
}

return $tree;
}

/**
* Merge together complex categories trees
*
* @param array $tree1
* @param array $tree2
* @return array
*/
private function mergeCategoriesTrees(array &$tree1, array &$tree2): array
{
$mergedTree = $tree1;
foreach ($tree2 as $currentKey => &$value) {
if (is_array($value) && isset($mergedTree[$currentKey]) && is_array($mergedTree[$currentKey])) {
$mergedTree[$currentKey] = $this->mergeCategoriesTrees($mergedTree[$currentKey], $value);
} else {
$mergedTree[$currentKey] = $value;
}
}
return $mergedTree;
}

/**
* Recursive method to generate tree for one category path
*
* @param array $pathElements
* @param int $index
* @return array
*/
private function explodePathToArray(array $pathElements, int $index): array
{
$tree = [];
$tree[$pathElements[$index]]['id'] = $pathElements[$index];
if ($index === count($pathElements) - 1) {
$tree[$pathElements[$index]] = $this->categoryHydrator->hydrateCategory($this->iteratingCategory);
$tree[$pathElements[$index]]['model'] = $this->iteratingCategory;
}
$currentIndex = $index;
$index++;
if (isset($pathElements[$index])) {
$tree[$pathElements[$currentIndex]]['children'] = $this->explodePathToArray($pathElements, $index);
}
return $tree;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ private function updateCustomerAddress(int $customerId, int $addressId, array $a
{
$address = $this->getCustomerAddressForUser->execute($addressId, $customerId);
$this->dataObjectHelper->populateWithArray($address, $addressData, AddressInterface::class);
if (isset($addressData['region']['region_id'])) {
$address->setRegionId($address->getRegion()->getRegionId());
}

return $this->addressRepository->save($address);
}
}
1 change: 0 additions & 1 deletion app/code/Magento/GraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ type Query {
}

type Mutation {
placeholderMutation: String @doc(description: "Mutation type cannot be declared without fields. The placeholder will be removed when at least one mutation field is declared.")
}

input FilterTypeInput @doc(description: "FilterTypeInput specifies which action will be performed in a query ") {
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/StoreGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ type Query {

type Website @doc(description: "The type contains information about a website") {
id : Int @doc(description: "The ID number assigned to the website")
name : String @doc(description: "The website name. Websites use this name to identify it easyer.")
name : String @doc(description: "The website name. Websites use this name to identify it easier.")
code : String @doc(description: "A code assigned to the website to identify it")
sort_order : Int @doc(description: "The attribute to use for sorting websites")
default_group_id : String @doc(description: "The default group id that the website has")
default_group_id : String @doc(description: "The default group ID that the website has")
is_default : Boolean @doc(description: "Specifies if this is the default website")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public function testCategoriesTree()
$responseDataObject = new DataObject($response);
//Some sort of smoke testing
self::assertEquals(
'Ololo',
$responseDataObject->getData('category/children/7/children/1/description')
'Its a description of Test Category 1.2',
$responseDataObject->getData('category/children/0/children/1/description')
);
self::assertEquals(
'default-category',
Expand All @@ -99,16 +99,54 @@ public function testCategoriesTree()
$responseDataObject->getData('category/children/0/default_sort_by')
);
self::assertCount(
8,
7,
$responseDataObject->getData('category/children')
);
self::assertCount(
2,
$responseDataObject->getData('category/children/7/children')
$responseDataObject->getData('category/children/0/children')
);
self::assertEquals(
5,
$responseDataObject->getData('category/children/7/children/1/children/0/id')
13,
$responseDataObject->getData('category/children/0/children/1/id')
);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testGetCategoryById()
{
$rootCategoryId = 13;
$query = <<<QUERY
{
category(id: {$rootCategoryId}) {
id
name
}
}
QUERY;

// get customer ID token
/** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
$customerTokenService = $this->objectManager->create(
\Magento\Integration\Api\CustomerTokenServiceInterface::class
);
$customerToken = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');

$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
$response = $this->graphQlQuery($query, [], '', $headerMap);
$responseDataObject = new DataObject($response);
//Some sort of smoke testing
self::assertEquals(
'Category 1.2',
$responseDataObject->getData('category/name')
);
self::assertEquals(
13,
$responseDataObject->getData('category/id')
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,12 @@ private function assertCustomerAddressesFields(AddressInterface $address, $actua
];
$this->assertResponseFields($actualResponse, $assertionMap);
$this->assertTrue(is_array([$actualResponse['region']]), "region field must be of an array type.");
// https://github.com/magento/graphql-ce/issues/270
// $assertionRegionMap = [
// ['response_field' => 'region', 'expected_value' => $address->getRegion()->getRegion()],
// ['response_field' => 'region_code', 'expected_value' => $address->getRegion()->getRegionCode()],
// ['response_field' => 'region_id', 'expected_value' => $address->getRegion()->getRegionId()]
// ];
// $this->assertResponseFields($actualResponse['region'], $assertionRegionMap);
$assertionRegionMap = [
['response_field' => 'region', 'expected_value' => $address->getRegion()->getRegion()],
['response_field' => 'region_code', 'expected_value' => $address->getRegion()->getRegionCode()],
['response_field' => 'region_id', 'expected_value' => $address->getRegion()->getRegionId()]
];
$this->assertResponseFields($actualResponse['region'], $assertionRegionMap);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
->setParentId(3)
->setPath('1/2/3/13')
->setLevel(3)
->setDescription('Ololo')
->setDescription('Its a description of Test Category 1.2')
->setAvailableSortBy('name')
->setDefaultSortBy('name')
->setIsActive(true)
Expand Down

0 comments on commit 0122d0b

Please sign in to comment.