Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Products: Access to related up sell cross sell product fields #249

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions app/code/Magento/CatalogGraphQl/Model/Resolver/Product/Related.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls, add declare(strict_types=1); to all of new files
And check code style of all of files


namespace Magento\CatalogGraphQl\Model\Resolver\Product;

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\Framework\Exception\LocalizedException;

class Related implements ResolverInterface
{

/**
* Fetches the data from persistence models and format it according to the GraphQL schema.
*
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @throws \Exception
* @return mixed|Value
*/


public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}

$product = $value['model'];
$productListType = $field->getName();

return [
'model' => $product,
'product_list_type' => $productListType
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Product\Related;

use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related\CrossSellDataProvider;
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;


class CrossSellProducts implements ResolverInterface
{
/**
* Attribute to select fields
*/
public const FIELDS = ['sku', 'name', 'price', 'image', 'url_path', 'url_key'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should take this data from request

/**
* @var CrossSellDataProvider
*/
private $dataProvider;

public function __construct(CrossSellDataProvider $dataProvider)
{
$this->dataProvider = $dataProvider;
}

/**
* Fetches the data from persistence models and format it according to the GraphQL schema.
*
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @throws \Exception
* @return mixed|Value
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use inheritdoc

{
$product = $value['model'];
$this->dataProvider->addFieldToSelect(self::FIELDS);
$collection = $this->dataProvider->getData($product);

$count = 0;
foreach ($collection as $item) {
$data[$count] = $item->getData();
$data[$count]['model'] = $item;
$count++;
}
return $data;
}

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

namespace Magento\CatalogGraphQl\Model\Resolver\Product\Related;


use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related\RelatedDataProvider;
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;


/**
* Class RelatedProducts
* @package Magento\CatalogGraphQl\Model\Resolver\Product\Related
*/
class RelatedProducts implements ResolverInterface
{

/**
* Attribute to select fields
*/
public const FIELDS = ['sku', 'name', 'price', 'image', 'url_path', 'url_key'];
/**
* @var RelatedDataProvider
*/
private $dataProvider;

/**
* RelatedProducts constructor.
* @param RelatedDataProvider $dataProvider
*/
public function __construct(RelatedDataProvider $dataProvider)
{
$this->dataProvider = $dataProvider;
}

/**
* Fetches the data from persistence models and format it according to the GraphQL schema.
*
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @throws \Exception
* @return mixed|Value
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$product = $value['model'];
$this->dataProvider->addFieldToSelect(self::FIELDS);
$collection = $this->dataProvider->getData($product);

$count = 0;
$data = [];
foreach ($collection as $item) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foreach ($data as $key => $value)

$data[$count] = $item->getData();
$data[$count]['model'] = $item;
$count++;
}
return $data;
}

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

namespace Magento\CatalogGraphQl\Model\Resolver\Product\Related;


use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related\UpSellDataProvider;
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;


/**
* Class UpSellProducts
* @package Magento\CatalogGraphQl\Model\Resolver\Product\Related
*/
class UpSellProducts implements ResolverInterface
{
/**
* Attribute to select fields
*/
public const FIELDS = ['sku', 'name', 'price', 'image', 'url_path', 'url_key'];
/**
* @var UpSellDataProvider
*/
private $dataProvider;

/**
* UpSellProducts constructor.
* @param UpSellDataProvider $dataProvider
*/
public function __construct(UpSellDataProvider $dataProvider)
{
$this->dataProvider = $dataProvider;
}

/**
* Fetches the data from persistence models and format it according to the GraphQL schema.
*
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @throws \Exception
* @return mixed|Value
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$product = $value['model'];
$this->dataProvider->addFieldToSelect(self::FIELDS);
$collection = $this->dataProvider->getData($product);

$count = 0;
$data = [];
foreach ($collection as $item) {
$data[$count] = $item->getData();
$data[$count]['model'] = $item;
$count++;
}
return $data;
}

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

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

abstract class AbstractDataProvider
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls, don't use supertypes only for code reusing (if a relation is not like type A is subtype of B)

{


/**
* @var array
*/
protected $fields = [];
/**
* @var $collection
*/
protected $collection;


/**
* @param $field
*/
public function addFieldToSelect($field)
{
$this->fields = $field;
}


/**
* @return array
*/
public function getFields(): array
{
return $this->fields;
}

/**
* @return mixed
*/
public function getCollection()
{
return $this->collection;
}

/**
* @param $product
* @return mixed
*/
public function getData($product)
{
$this->prepareCollection($product);
return $this->collection;

}

/**
* @param $product
*/
protected function prepareCollection($product): void
{
$this->collection = $product->getRelatedProducts();
$this->collection->addAttributeToSelect($this->getFields());
}


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

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


/**
* Class CrossSellDataProvider
* @package Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related
*/
class CrossSellDataProvider extends AbstractDataProvider
{
/**
* @param $product
*/
protected function prepareCollection($product): void
{
$this->collection = $product->getCrossSellProductCollection();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like proxy on another method

$this->collection->addAttributeToSelect($this->getFields());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php


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


class RelatedDataProvider extends AbstractDataProvider
{


protected function prepareCollection($product): void
{
$this->collection = $product->getRelatedProductCollection();
$this->collection->addAttributeToSelect($this->getFields());
}


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

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

/**
* Class UpSellDataProvider
* @package Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related
*/
class UpSellDataProvider extends AbstractDataProvider
{

/**
* @param $product
*/
protected function prepareCollection($product): void
{
$this->collection = $product->getUpSellProductCollection();
$this->collection->addAttributeToSelect($this->getFields());
}
}
Loading