Skip to content

Commit

Permalink
Merge pull request magento#3251 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
Valeriy Naida authored Oct 4, 2018
2 parents 20f1162 + 5bd6c1c commit 18dcf17
Show file tree
Hide file tree
Showing 28 changed files with 748 additions and 180 deletions.
16 changes: 14 additions & 2 deletions app/code/Magento/CatalogGraphQl/Model/Resolver/CategoryTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\CatalogGraphQl\Model\Resolver;

use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ExtractDataFromCategoryTree;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
Expand All @@ -27,16 +28,26 @@ class CategoryTree implements ResolverInterface
*/
private $categoryTree;

/**
* @var ExtractDataFromCategoryTree
*/
private $extractDataFromCategoryTree;

/**
* @param \Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree
* @param ExtractDataFromCategoryTree $extractDataFromCategoryTree
*/
public function __construct(
\Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree
\Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree,
ExtractDataFromCategoryTree $extractDataFromCategoryTree
) {
$this->categoryTree = $categoryTree;
$this->extractDataFromCategoryTree = $extractDataFromCategoryTree;
}

/**
* Get category id
*
* @param array $args
* @return int
* @throws GraphQlInputException
Expand All @@ -62,7 +73,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
$rootCategoryId = $this->getCategoryId($args);
$categoriesTree = $this->categoryTree->getTree($info, $rootCategoryId);
if (!empty($categoriesTree)) {
return current($categoriesTree);
$result = $this->extractDataFromCategoryTree->execute($categoriesTree);
return current($result);
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

use GraphQL\Language\AST\FieldNode;
use Magento\CatalogGraphQl\Model\Category\DepthCalculator;
use Magento\CatalogGraphQl\Model\Category\Hydrator;
use Magento\CatalogGraphQl\Model\Category\LevelCalculator;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\CatalogGraphQl\Model\AttributesJoiner;
use Magento\Catalog\Model\Category;

/**
* Category tree data provider
Expand Down Expand Up @@ -53,87 +53,64 @@ class CategoryTree
*/
private $metadata;

/**
* @var Hydrator
*/
private $hydrator;

/**
* @param CollectionFactory $collectionFactory
* @param AttributesJoiner $attributesJoiner
* @param DepthCalculator $depthCalculator
* @param LevelCalculator $levelCalculator
* @param MetadataPool $metadata
* @param Hydrator $hydrator
*/
public function __construct(
CollectionFactory $collectionFactory,
AttributesJoiner $attributesJoiner,
DepthCalculator $depthCalculator,
LevelCalculator $levelCalculator,
MetadataPool $metadata,
Hydrator $hydrator
MetadataPool $metadata
) {
$this->collectionFactory = $collectionFactory;
$this->attributesJoiner = $attributesJoiner;
$this->depthCalculator = $depthCalculator;
$this->levelCalculator = $levelCalculator;
$this->metadata = $metadata;
$this->hydrator = $hydrator;
}

/**
* Returns categories tree starting from parent $rootCategoryId
*
* @param ResolveInfo $resolveInfo
* @param int $rootCategoryId
* @return array
* @return \Iterator
*/
public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId) : array
public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId): \Iterator
{
$categoryQuery = $resolveInfo->fieldNodes[0];
$collection = $this->collectionFactory->create();
$this->joinAttributesRecursively($collection, $categoryQuery);
$depth = $this->depthCalculator->calculate($categoryQuery);
$level = $this->levelCalculator->calculate($rootCategoryId);

// If root category is being filter, we've to remove first slash
if ($rootCategoryId == Category::TREE_ROOT_ID) {
$regExpPathFilter = sprintf('.*%s/[/0-9]*$', $rootCategoryId);
} else {
$regExpPathFilter = sprintf('.*/%s/[/0-9]*$', $rootCategoryId);
}

//Search for desired part of category tree
$collection->addPathFilter(sprintf('.*/%s/[/0-9]*$', $rootCategoryId));
$collection->addPathFilter($regExpPathFilter);

$collection->addFieldToFilter('level', ['gt' => $level]);
$collection->addFieldToFilter('level', ['lteq' => $level + $depth - self::DEPTH_OFFSET]);
$collection->setOrder('level');
$collection->getSelect()->orWhere(
$this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField() . ' = ?',
$rootCategoryId
);
return $this->processTree($collection->getIterator());
}

/**
* Iterates through category tree
*
* @param \Iterator $iterator
* @return array
*/
private function processTree(\Iterator $iterator) : array
{
$tree = [];
while ($iterator->valid()) {
/** @var CategoryInterface $category */
$category = $iterator->current();
$iterator->next();
$nextCategory = $iterator->current();
$tree[$category->getId()] = $this->hydrator->hydrateCategory($category);
$tree[$category->getId()]['model'] = $category;
if ($nextCategory && (int) $nextCategory->getLevel() !== (int) $category->getLevel()) {
$tree[$category->getId()]['children'] = $this->processTree($iterator);
}
}

return $tree;
return $collection->getIterator();
}

/**
* Joins EAV attributes recursively
* Join attributes recursively
*
* @param Collection $collection
* @param FieldNode $fieldNode
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider;

use Magento\CatalogGraphQl\Model\Category\Hydrator;
use Magento\Catalog\Api\Data\CategoryInterface;

/**
* Extract data from category tree
*/
class ExtractDataFromCategoryTree
{
/**
* @var Hydrator
*/
private $categoryHydrator;

/**
* @param Hydrator $categoryHydrator
*/
public function __construct(
Hydrator $categoryHydrator
) {
$this->categoryHydrator = $categoryHydrator;
}

/**
* Extract data from category tree
*
* @param \Iterator $iterator
* @return array
*/
public function execute(\Iterator $iterator): array
{
$tree = [];
while ($iterator->valid()) {
/** @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);
}
}

return $tree;
}
}
7 changes: 6 additions & 1 deletion app/code/Magento/CatalogUrlRewriteGraphQl/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_CatalogUrlRewriteGraphQl" />
<module name="Magento_CatalogUrlRewriteGraphQl" >
<sequence>
<module name="Magento_UrlRewriteGraphQl"/>
<module name="Magento_CatalogGraphQl"/>
</sequence>
</module>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
interface ProductInterface {
url_key: String @doc(description: "The part of the URL that identifies the product")
url_path: String @doc(description: "The part of the URL that precedes the url_key")
url_rewrites: [UrlRewrite] @doc(description: "URL rewrites list") @resolver(class: "Magento\\UrlRewriteGraphQl\\Model\\Resolver\\UrlRewrite")
}

input ProductFilterInput {
Expand Down
4 changes: 4 additions & 0 deletions app/code/Magento/CmsGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"magento/module-cms": "*",
"magento/module-widget": "*"
},
"suggest": {
"magento/module-graph-ql": "*",
"magento/module-store-graph-ql": "*"
},
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
21 changes: 21 additions & 0 deletions app/code/Magento/CmsGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\StoreGraphQl\Model\Resolver\Store\StoreConfigDataProvider">
<arguments>
<argument name="extendedConfigData" xsi:type="array">
<item name="front" xsi:type="string">web/default/front</item>
<item name="cms_home_page" xsi:type="string">web/default/cms_home_page</item>
<item name="no_route" xsi:type="string">web/default/no_route</item>
<item name="cms_no_route" xsi:type="string">web/default/cms_no_route</item>
<item name="cms_no_cookies" xsi:type="string">web/default/cms_no_cookies</item>
<item name="show_cms_breadcrumbs" xsi:type="string">web/default/show_cms_breadcrumbs</item>
</argument>
</arguments>
</type>
</config>
9 changes: 9 additions & 0 deletions app/code/Magento/CmsGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.
type StoreConfig @doc(description: "The type contains information about a store config") {
front : String @doc(description: "Default Web URL")
cms_home_page : String @doc(description: "CMS Home Page")
no_route : String @doc(description: "Default No-route URL")
cms_no_route : String @doc(description: "CMS No Route Page")
cms_no_cookies : String @doc(description: "CMS No Cookies Page")
show_cms_breadcrumbs : Int @doc(description: "Show Breadcrumbs for CMS Pages")
}


type Query {
cmsPage (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Resolver\Customer\Account;

use Magento\Authorization\Model\UserContextInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Integration\Api\CustomerTokenServiceInterface;

/**
* Customers Revoke Token resolver, used for GraphQL request processing.
*/
class RevokeCustomerToken implements ResolverInterface
{
/**
* @var UserContextInterface
*/
private $userContext;

/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

/**
* @param UserContextInterface $userContext
* @param CustomerTokenServiceInterface $customerTokenService
*/
public function __construct(
UserContextInterface $userContext,
CustomerTokenServiceInterface $customerTokenService
) {
$this->userContext = $userContext;
$this->customerTokenService = $customerTokenService;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$customerId = (int)$this->userContext->getUserId();

if ($customerId === 0) {
throw new GraphQlAuthorizationException(
__(
'Current customer does not have access to the resource "%1"',
[\Magento\Customer\Model\Customer::ENTITY]
)
);
}

return $this->customerTokenService->revokeCustomerAccessToken($customerId);
}
}
1 change: 1 addition & 0 deletions app/code/Magento/CustomerGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Query {
type Mutation {
generateCustomerToken(email: String!, password: String!): CustomerToken @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\Customer\\Account\\GenerateCustomerToken") @doc(description:"Retrieve Customer token")
changeCustomerPassword(currentPassword: String!, newPassword: String!): Customer @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\Customer\\Account\\ChangePassword") @doc(description:"Changes password for logged in customer")
revokeCustomerToken: Boolean @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\Customer\\Account\\RevokeCustomerToken") @doc(description:"Revoke Customer token")
}

type CustomerToken {
Expand Down
Loading

0 comments on commit 18dcf17

Please sign in to comment.