Skip to content

Commit 0d3ef8e

Browse files
committed
GraphQl get ListTag
1 parent 62bf762 commit 0d3ef8e

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

Model/Resolver/DataProvider/Tag.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,12 @@ public function __construct(
3434
}
3535

3636
/**
37-
* @param string $tagsIdentifier
37+
* @param Lof\ProductTags\Model\Tag $tag
3838
* @return array
3939
* @throws NoSuchEntityException
4040
*/
41-
public function getData(string $tagsIdentifier): array
41+
public function getData( $tag): array
4242
{
43-
$tag = $this->tagRepository->getById($tagsIdentifier);
44-
4543
if (false === $tag->getStatus()) {
4644
throw new NoSuchEntityException();
4745
}

Model/Resolver/Tag.php

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Lof\ProductTagsGraphQl\Model\Resolver;
99

10+
use Lof\ProductTags\Api\Data\TagInterface;
11+
use Lof\ProductTags\Api\TagRepositoryInterface;
1012
use Lof\ProductTagsGraphQl\Model\Resolver\DataProvider\Tag as TagDataProvider;
1113
use Magento\Framework\Exception\NoSuchEntityException;
1214
use Magento\Framework\GraphQl\Config\Element\Field;
@@ -17,6 +19,11 @@
1719

1820
class Tag implements ResolverInterface
1921
{
22+
/**
23+
* @var TagRepositoryInterface
24+
*/
25+
private $tagRepository;
26+
2027
/**
2128
* @var TagDataProvider
2229
*/
@@ -25,9 +32,11 @@ class Tag implements ResolverInterface
2532
/**
2633
* @param DataProvider\Tag $tagDataProvider
2734
*/
28-
public function __construct(DataProvider\Tag $tagDataProvider)
35+
public function __construct(DataProvider\Tag $tagDataProvider,
36+
\Lof\ProductTags\Api\TagRepositoryInterface $tagRepository)
2937
{
3038
$this->tagDataProvider = $tagDataProvider;
39+
$this->tagRepository = $tagRepository;
3140
}
3241

3342
/**
@@ -40,8 +49,8 @@ public function resolve(
4049
array $value = null,
4150
array $args = null
4251
) {
43-
$tagsIdentifier = $this->getTagIdentifier($args);
44-
$tagsData = $this->getTagData($tagsIdentifier);
52+
$tags = $this->getTags($args);
53+
$tagsData = $this->getTagsData($tags);
4554
$resultData = [
4655
'items' => $tagsData,
4756
];
@@ -50,38 +59,39 @@ public function resolve(
5059

5160
/**
5261
* @param array $args
53-
* @return string[]
62+
* @return Lof\ProductTags\Model\ResourceModel\Tag\Collection $tagList
5463
* @throws GraphQlInputException
5564
*/
56-
private function getTagIdentifier(array $args): array
65+
private function getTags(array $args)
5766
{
58-
if (!isset($args['identifiers'])||!isset($args['tag_id'])||!isset($args['tag_title'])||!isset($args['status'])) {
67+
if (isset($args['identifiers'])||isset($args['tag_id'])||isset($args['tag_title'])||isset($args['status'])) {
68+
//throw new GraphQlInputException(__('"identifiers" of Tag should be specified'));
69+
$taglist = $this->tagRepository->getListTag($args);
70+
}
71+
else{
5972
throw new GraphQlInputException(__('"identifiers", "tag_id", "tag_title" or "status" of Tag should be specified'));
6073
}
61-
//return (array)$args['identifiers'];
62-
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
63-
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
64-
$connection = $resource->getConnection();
65-
$tableName = $resource->getTableName('lof_producttags_tag');
66-
$sql = "Select lof_producttags_tag.tag_id FROM lof_producttags_tag INNER JOIN lof_producttags_store ON lof_producttags_tag.tag_id = lof_producttags_store.tag_id WHERE lof_producttags_tag.identifier LIKE '%" . $args['identifiers'] . "%' AND lof_producttags_tag.tag_id LIKE '%" . $args['tag_id'] . "%' AND lof_producttags_tag.status = true AND lof_producttags_tag.tag_title LIKE '%" . $args['tag_title'] . "%';";
67-
$result = $connection->fetchCol($sql);
68-
return $result;
74+
//$taglist = $this->tagRepository->getListTag($args);
75+
76+
return $taglist;
6977
}
7078

7179
/**
72-
* @param array $tagsIdentifier
80+
* @param Lof\ProductTags\Model\ResourceModel\Tag\Collection $tagList
7381
* @return array
7482
* @throws GraphQlNoSuchEntityException
7583
*/
76-
private function getTagData(array $tagsIdentifier): array
84+
private function getTagsData($tagList): array
7785
{
78-
$tagsData = [];
79-
//$tagsIdentifier = ['test-2', 'test-3', 'test-4', 'test-5'];
80-
foreach ($tagsIdentifier as $tagIdentifier) {
81-
try {
82-
$tagsData[$tagIdentifier] = $this->tagDataProvider->getData($tagIdentifier);
83-
} catch (NoSuchEntityException $e) {
84-
$tagsData[$tagIdentifier] = new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
86+
$tagsData = [];
87+
if($tagList->getSize()){
88+
foreach ($tagList as $tagItem) {
89+
$tagId = $tagItem->getId();
90+
try {
91+
$tagsData[$tagId] = $this->tagDataProvider->getData($tagItem);
92+
} catch (NoSuchEntityException $e) {
93+
$tagsData[$tagId] = new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
94+
}
8595
}
8696
}
8797
return $tagsData;

0 commit comments

Comments
 (0)