Skip to content

Commit d11fda5

Browse files
Access to related/up-sell/cross-sell product fields in graphql
1 parent 5f976fa commit d11fda5

File tree

9 files changed

+398
-3
lines changed

9 files changed

+398
-3
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Magento\CatalogGraphQl\Model\Resolver\Product;
4+
5+
use Magento\Framework\GraphQl\Config\Element\Field;
6+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
7+
use Magento\Framework\GraphQl\Query\Resolver\Value;
8+
use Magento\Framework\GraphQl\Query\ResolverInterface;
9+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
10+
11+
use Magento\Framework\Exception\LocalizedException;
12+
13+
class Related implements ResolverInterface
14+
{
15+
16+
/**
17+
* Fetches the data from persistence models and format it according to the GraphQL schema.
18+
*
19+
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
20+
* @param ContextInterface $context
21+
* @param ResolveInfo $info
22+
* @param array|null $value
23+
* @param array|null $args
24+
* @throws \Exception
25+
* @return mixed|Value
26+
*/
27+
28+
29+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
30+
{
31+
if (!isset($value['model'])) {
32+
throw new LocalizedException(__('"model" value should be specified'));
33+
}
34+
35+
$product = $value['model'];
36+
$productListType = $field->getName();
37+
38+
return [
39+
'model' => $product,
40+
'product_list_type' => $productListType
41+
];
42+
}
43+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\CatalogGraphQl\Model\Resolver\Product\Related;
9+
10+
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related\CrossSellDataProvider;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
13+
use Magento\Framework\GraphQl\Query\Resolver\Value;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
17+
18+
class CrossSellProducts implements ResolverInterface
19+
{
20+
/**
21+
* Attribute to select fields
22+
*/
23+
public const FIELDS = ['sku', 'name', 'price', 'image', 'url_path', 'url_key'];
24+
/**
25+
* @var CrossSellDataProvider
26+
*/
27+
private $dataProvider;
28+
29+
public function __construct(CrossSellDataProvider $dataProvider)
30+
{
31+
$this->dataProvider = $dataProvider;
32+
}
33+
34+
/**
35+
* Fetches the data from persistence models and format it according to the GraphQL schema.
36+
*
37+
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
38+
* @param ContextInterface $context
39+
* @param ResolveInfo $info
40+
* @param array|null $value
41+
* @param array|null $args
42+
* @throws \Exception
43+
* @return mixed|Value
44+
*/
45+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
46+
{
47+
$product = $value['model'];
48+
$this->dataProvider->addFieldToSelect(self::FIELDS);
49+
$collection = $this->dataProvider->getData($product);
50+
51+
$count = 0;
52+
foreach ($collection as $item) {
53+
$data[$count] = $item->getData();
54+
$data[$count]['model'] = $item;
55+
$count++;
56+
}
57+
return $data;
58+
}
59+
60+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\CatalogGraphQl\Model\Resolver\Product\Related;
9+
10+
11+
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related\RelatedDataProvider;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
14+
use Magento\Framework\GraphQl\Query\Resolver\Value;
15+
use Magento\Framework\GraphQl\Query\ResolverInterface;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
18+
19+
/**
20+
* Class RelatedProducts
21+
* @package Magento\CatalogGraphQl\Model\Resolver\Product\Related
22+
*/
23+
class RelatedProducts implements ResolverInterface
24+
{
25+
26+
/**
27+
* Attribute to select fields
28+
*/
29+
public const FIELDS = ['sku', 'name', 'price', 'image', 'url_path', 'url_key'];
30+
/**
31+
* @var RelatedDataProvider
32+
*/
33+
private $dataProvider;
34+
35+
/**
36+
* RelatedProducts constructor.
37+
* @param RelatedDataProvider $dataProvider
38+
*/
39+
public function __construct(RelatedDataProvider $dataProvider)
40+
{
41+
$this->dataProvider = $dataProvider;
42+
}
43+
44+
/**
45+
* Fetches the data from persistence models and format it according to the GraphQL schema.
46+
*
47+
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
48+
* @param ContextInterface $context
49+
* @param ResolveInfo $info
50+
* @param array|null $value
51+
* @param array|null $args
52+
* @throws \Exception
53+
* @return mixed|Value
54+
*/
55+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
56+
{
57+
$product = $value['model'];
58+
$this->dataProvider->addFieldToSelect(self::FIELDS);
59+
$collection = $this->dataProvider->getData($product);
60+
61+
$count = 0;
62+
$data = [];
63+
foreach ($collection as $item) {
64+
$data[$count] = $item->getData();
65+
$data[$count]['model'] = $item;
66+
$count++;
67+
}
68+
return $data;
69+
}
70+
71+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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\CatalogGraphQl\Model\Resolver\Product\Related;
9+
10+
11+
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related\UpSellDataProvider;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
14+
use Magento\Framework\GraphQl\Query\Resolver\Value;
15+
use Magento\Framework\GraphQl\Query\ResolverInterface;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
18+
19+
/**
20+
* Class UpSellProducts
21+
* @package Magento\CatalogGraphQl\Model\Resolver\Product\Related
22+
*/
23+
class UpSellProducts implements ResolverInterface
24+
{
25+
/**
26+
* Attribute to select fields
27+
*/
28+
public const FIELDS = ['sku', 'name', 'price', 'image', 'url_path', 'url_key'];
29+
/**
30+
* @var UpSellDataProvider
31+
*/
32+
private $dataProvider;
33+
34+
/**
35+
* UpSellProducts constructor.
36+
* @param UpSellDataProvider $dataProvider
37+
*/
38+
public function __construct(UpSellDataProvider $dataProvider)
39+
{
40+
$this->dataProvider = $dataProvider;
41+
}
42+
43+
/**
44+
* Fetches the data from persistence models and format it according to the GraphQL schema.
45+
*
46+
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
47+
* @param ContextInterface $context
48+
* @param ResolveInfo $info
49+
* @param array|null $value
50+
* @param array|null $args
51+
* @throws \Exception
52+
* @return mixed|Value
53+
*/
54+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
55+
{
56+
$product = $value['model'];
57+
$this->dataProvider->addFieldToSelect(self::FIELDS);
58+
$collection = $this->dataProvider->getData($product);
59+
60+
$count = 0;
61+
$data = [];
62+
foreach ($collection as $item) {
63+
$data[$count] = $item->getData();
64+
$data[$count]['model'] = $item;
65+
$count++;
66+
}
67+
return $data;
68+
}
69+
70+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related;
9+
10+
abstract class AbstractDataProvider
11+
{
12+
13+
14+
/**
15+
* @var array
16+
*/
17+
protected $fields = [];
18+
/**
19+
* @var $collection
20+
*/
21+
protected $collection;
22+
23+
24+
/**
25+
* @param $field
26+
*/
27+
public function addFieldToSelect($field)
28+
{
29+
$this->fields = $field;
30+
}
31+
32+
33+
/**
34+
* @return array
35+
*/
36+
public function getFields(): array
37+
{
38+
return $this->fields;
39+
}
40+
41+
/**
42+
* @return mixed
43+
*/
44+
public function getCollection()
45+
{
46+
return $this->collection;
47+
}
48+
49+
/**
50+
* @param $product
51+
* @return mixed
52+
*/
53+
public function getData($product)
54+
{
55+
$this->prepareCollection($product);
56+
return $this->collection;
57+
58+
}
59+
60+
/**
61+
* @param $product
62+
*/
63+
protected function prepareCollection($product): void
64+
{
65+
$this->collection = $product->getRelatedProducts();
66+
$this->collection->addAttributeToSelect($this->getFields());
67+
}
68+
69+
70+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related;
9+
10+
11+
/**
12+
* Class CrossSellDataProvider
13+
* @package Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related
14+
*/
15+
class CrossSellDataProvider extends AbstractDataProvider
16+
{
17+
/**
18+
* @param $product
19+
*/
20+
protected function prepareCollection($product): void
21+
{
22+
$this->collection = $product->getCrossSellProductCollection();
23+
$this->collection->addAttributeToSelect($this->getFields());
24+
}
25+
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
4+
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related;
5+
6+
7+
class RelatedDataProvider extends AbstractDataProvider
8+
{
9+
10+
11+
protected function prepareCollection($product): void
12+
{
13+
$this->collection = $product->getRelatedProductCollection();
14+
$this->collection->addAttributeToSelect($this->getFields());
15+
}
16+
17+
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related;
9+
10+
/**
11+
* Class UpSellDataProvider
12+
* @package Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Related
13+
*/
14+
class UpSellDataProvider extends AbstractDataProvider
15+
{
16+
17+
/**
18+
* @param $product
19+
*/
20+
protected function prepareCollection($product): void
21+
{
22+
$this->collection = $product->getUpSellProductCollection();
23+
$this->collection->addAttributeToSelect($this->getFields());
24+
}
25+
}

0 commit comments

Comments
 (0)