Skip to content

Commit

Permalink
Merge pull request #23 from scandipwa/performance
Browse files Browse the repository at this point in the history
Performance optimization
  • Loading branch information
alfredsgenkins authored Jan 7, 2020
2 parents 76a156f + d4c24c4 commit f94f611
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 88 deletions.
59 changes: 30 additions & 29 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
{
"name": "scandipwa/quote-graphql",
"description": "N/A",
"type": "magento2-module",
"require": {
"magento/magento2-base": "^2.3.2"
"name": "scandipwa/quote-graphql",
"description": "N/A",
"type": "magento2-module",
"require": {
"magento/magento2-base": "^2.3.2",
"scandipwa/performance": "^1.0"
},
"support": {
"source": "https://github.com/scandipwa/quote-graphql",
"email": "[email protected]"
},
"authors": [
{
"name": "Alfreds Genkins",
"email": "[email protected]"
},
"support": {
"source": "https://github.com/scandipwa/quote-graphql",
"email": "[email protected]"
},
"authors": [
{
"name": "Alfreds Genkins",
"email": "[email protected]"
},
{
"name": "Ilja Lapkovskis",
"email": "[email protected]"
}
],
"license": [
"OSL-3.0",
"AFL-3.0"
{
"name": "Ilja Lapkovskis",
"email": "[email protected]"
}
],
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"src/registration.php"
],
"autoload": {
"files": [
"src/registration.php"
],
"psr-4": {
"ScandiPWA\\QuoteGraphQl\\": "src"
}
"psr-4": {
"ScandiPWA\\QuoteGraphQl\\": "src"
}
}
}
10 changes: 4 additions & 6 deletions src/Model/Resolver/ExpandedOrderResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,28 @@
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;
use ScandiPWA\QuoteGraphQl\Model\Customer\CheckCustomerAccount;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;

use Magento\Sales\Model\OrderRepository;

/**
* Orders data reslover
* Orders data resolver
*/
class ExpandedOrderResolver implements ResolverInterface
{
/**
* @var CollectionFactoryInterface
*/
private $collectionFactory;
protected $collectionFactory;

/**
* @var CheckCustomerAccount
*/
private $checkCustomerAccount;
protected $checkCustomerAccount;

/**
* @var OrderRepository
*/
private $orderRepository;
protected $orderRepository;

/**
* @param CollectionFactoryInterface $collectionFactory
Expand Down
113 changes: 69 additions & 44 deletions src/Model/Resolver/GetCartForCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@

namespace ScandiPWA\QuoteGraphQl\Model\Resolver;

use Magento\Catalog\Model\Product;
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable;
use Magento\Catalog\Model\ProductFactory;

use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Api\GuestCartRepositoryInterface;
use Magento\Quote\Model\QuoteManagement;
use Magento\Quote\Model\Quote\Item as QuoteItem;
use Magento\Webapi\Controller\Rest\ParamOverriderCustomerId;
use ScandiPWA\Performance\Model\Resolver\Products\DataPostProcessor;

class GetCartForCustomer extends CartResolver
{
Expand All @@ -41,25 +41,56 @@ class GetCartForCustomer extends CartResolver
*/
protected $productFactory;

/**
* @var DataPostProcessor
*/
protected $productPostProcessor;

/**
* @var array
*/
protected $productsData;

/**
* GetCartForCustomer constructor.
* @param ParamOverriderCustomerId $overriderCustomerId
* @param CartManagementInterface $quoteManagement
* @param GuestCartRepositoryInterface $guestCartRepository
* @param Configurable $configurable
* @param ProductFactory $productFactory
* @param DataPostProcessor $productPostProcessor
*/
public function __construct(
ParamOverriderCustomerId $overriderCustomerId,
CartManagementInterface $quoteManagement,
GuestCartRepositoryInterface $guestCartRepository,
Configurable $configurable,
ProductFactory $productFactory
)
{
parent::__construct($guestCartRepository, $overriderCustomerId, $quoteManagement);
ProductFactory $productFactory,
DataPostProcessor $productPostProcessor
) {
parent::__construct(
$guestCartRepository,
$overriderCustomerId,
$quoteManagement
);

$this->configurable = $configurable;
$this->productFactory = $productFactory;
$this->productPostProcessor = $productPostProcessor;
}

/**
* @param QuoteItem $item
* @param Product $product
* @return array
*/
protected function mergeQuoteItemData(
QuoteItem $item,
Product $product
) {
return [
'product' => $this->productsData[$product->getId()]
] + $item->getData();
}

/**
Expand All @@ -79,55 +110,49 @@ public function resolve(
ResolveInfo $info,
array $value = null,
array $args = null
)
{
) {
$cart = $this->getCart($args);

$items = $cart->getItems();
$itemsData = [];

foreach ($cart->getItems() as $item) {
$product = $item->getProduct();
$parentIds = $this->configurable->getParentIdsByChild($product->getId());
if (count($parentIds)) {
$parentProduct = $this->productFactory->create()->load(reset($parentIds));
$itemsData[] = array_merge(
$item->getData(),
['product' =>
array_merge(
$parentProduct->getData(),
['model' => $parentProduct]
)
]
);
} else {
$itemsData[] = array_merge(
$item->getData(),
['product' =>
array_merge(
$product->getData(),
['model' => $product]
)
]
);
if ($items) {
// Prepare product data in advance
$products = array_map(function ($item) {
return $item->getProduct();
}, $items);

$adjustedInfo = $info->fieldNodes[0];
$this->productsData = $this->productPostProcessor->process(
$products,
'items/product',
$adjustedInfo
);

foreach ($items as $item) {
/** @var QuoteItem $item */
$product = $item->getProduct();
$parentIds = $this->configurable->getParentIdsByChild($product->getId());

if (count($parentIds)) {
$parentProduct = $this->productFactory->create()->load(reset($parentIds));
$itemsData[] = $this->mergeQuoteItemData($item, $parentProduct);
} else {
$itemsData[] = $this->mergeQuoteItemData($item, $product);
}
}
}

$address = $cart->isVirtual() ? $cart->getBillingAddress() : $cart->getShippingAddress();
$tax_amount = $address->getTaxAmount();
$discount_amount = $address->getDiscountAmount();

return array_merge(
$cart->getData(),
[
return [
'items' => $itemsData,
'tax_amount' => $tax_amount,
'discount_amount' => $discount_amount,
/**
* In interface it is PHPDocumented that it returns bool,
* while in implementation it returns int.
*/
'is_virtual' => (bool)$cart->getIsVirtual()
]
);
// In interface it is PHPDocumented that it returns bool,
// while in implementation it returns int.
'is_virtual' => (bool) $cart->getIsVirtual()
] + $cart->getData();
}
}
78 changes: 69 additions & 9 deletions src/Model/Resolver/ProductResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,107 @@

namespace ScandiPWA\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Catalog\Model\ProductRepository;
use Magento\Sales\Model\Order\Item;
use ScandiPWA\Performance\Model\Resolver\Products\DataPostProcessor;
use ScandiPWA\Performance\Model\Resolver\ResolveInfoFieldsTrait;
use ScandiPWA\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product;

/**
* Retrieves the Product list in orders
*/
class ProductResolver implements ResolverInterface
{
use ResolveInfoFieldsTrait;

/**
* @var ProductRepository
*/
protected $productRepository;

/**
* @var Product
*/
protected $productDataProvider;

/**
* @var SearchCriteriaBuilder
*/
protected $searchCriteriaBuilder;

/**
* @var DataPostProcessor
*/
protected $postProcessor;

/**
* ProductResolver constructor.
* @param ProductRepository $productRepository
* @param Product $productDataProvider
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param DataPostProcessor $postProcessor
*/
public function __construct(
ProductRepository $productRepository
ProductRepository $productRepository,
Product $productDataProvider,
SearchCriteriaBuilder $searchCriteriaBuilder,
DataPostProcessor $postProcessor
) {
$this->productRepository = $productRepository;
$this->productDataProvider = $productDataProvider;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->postProcessor = $postProcessor;
}

/**
* Get All Product Items of Order.
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (!isset($value['products'])) {
return null;
return [];
}

foreach ($value['products'] as $key => $item) {
$product = $this->productRepository->get($item['sku']);
$productSKUs = array_map(function ($item) {
return $item['sku'];
}, $value['products']);

$attributeCodes = $this->getFieldsFromProductInfo($info, 'order_products');

$productData = $product->toArray();
$productData['model'] = $product;
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('sku', $productSKUs, 'in')
->create();

$data[$key] = $productData;
$products = $this->productDataProvider
->getList(
$searchCriteria,
$attributeCodes,
false,
true
)
->getItems();

$productsData = $this->postProcessor->process(
$products,
'order_products',
$info
);

$data = [];

foreach ($value['products'] as $key => $item) {
/** @var $item Item */
$data[$key] = $productsData[$item->getProductId()];
$data[$key]['qty'] = $item->getQtyOrdered();
$data[$key]['row_total'] = $item->getBaseRowTotalInclTax();
$data[$key]['original_price'] = $item->getBaseOriginalPrice();
Expand Down

0 comments on commit f94f611

Please sign in to comment.