Skip to content

Commit 62bf762

Browse files
committed
create GraphQl get list Tag by Tag key
1 parent 8fabf69 commit 62bf762

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

Model/Resolver/DataProvider/Tag.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,7 @@ public function __construct(
4040
*/
4141
public function getData(string $tagsIdentifier): array
4242
{
43-
$tagModel = $this->_tagModelFactory->create();
44-
$tagModel->loadByIdentifier($tagsIdentifier);
45-
if (!$tagModel->getId()) {
46-
return [];
47-
}
48-
$tag = $this->tagRepository->getById($tagModel->getId());
43+
$tag = $this->tagRepository->getById($tagsIdentifier);
4944

5045
if (false === $tag->getStatus()) {
5146
throw new NoSuchEntityException();
@@ -57,6 +52,7 @@ public function getData(string $tagsIdentifier): array
5752
TagInterface::TAG_TITLE => $tag->getTagTitle(),
5853
TagInterface::TAG_IDENTIFIER => $tag->getIdentifier(),
5954
TagInterface::TAG_DESCRIPTION => $tag->getTagDescription(),
55+
TagInterface::STORE_ID => $tag->getStoreId(),
6056
];
6157
return $tagData;
6258
}

Model/Resolver/Tag.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,23 @@ public function resolve(
4949
}
5050

5151
/**
52-
* @param string $args
52+
* @param array $args
5353
* @return string[]
5454
* @throws GraphQlInputException
5555
*/
56-
private function getTagIdentifier(string $args): array
56+
private function getTagIdentifier(array $args): array
5757
{
58-
if (!isset($args['identifiers']) || !is_array($args['identifiers']) || count($args['identifiers']) === 0) {
59-
throw new GraphQlInputException(__('"identifiers" of Tag should be specified'));
58+
if (!isset($args['identifiers'])||!isset($args['tag_id'])||!isset($args['tag_title'])||!isset($args['status'])) {
59+
throw new GraphQlInputException(__('"identifiers", "tag_id", "tag_title" or "status" of Tag should be specified'));
6060
}
61-
61+
//return (array)$args['identifiers'];
6262
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
6363
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
6464
$connection = $resource->getConnection();
6565
$tableName = $resource->getTableName('lof_producttags_tag');
66-
$select = $connection->select()->from(
67-
$tableName,
68-
'identifier'
69-
)
70-
->where(
71-
'identifier'
72-
)
73-
->like($args['identifiers']);
74-
return $select;
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;
7569
}
7670

7771
/**
@@ -81,8 +75,8 @@ private function getTagIdentifier(string $args): array
8175
*/
8276
private function getTagData(array $tagsIdentifier): array
8377
{
84-
$tagsData = [];
85-
78+
$tagsData = [];
79+
//$tagsIdentifier = ['test-2', 'test-3', 'test-4', 'test-5'];
8680
foreach ($tagsIdentifier as $tagIdentifier) {
8781
try {
8882
$tagsData[$tagIdentifier] = $this->tagDataProvider->getData($tagIdentifier);

etc/schema.graphqls

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
type Query {
33

44
tag (
5-
identifiers: FilterTypeInput @doc(description: "Query by tag_id.")
5+
identifiers: String @doc(description: "Query by identifier."),
6+
tag_id: String @doc(description: "Query by tag_id."),
7+
tag_title: String @doc(description: "Query by tag_title."),
8+
status: Boolean @doc(description: "Query by status.")
69
) : Tag @resolver( class: "Lof\\ProductTagsGraphQl\\Model\\Resolver\\Tag") ,
710
product (
811
identifiers: [String] @doc(description: "Query by identifier.")
@@ -20,6 +23,7 @@ type TagsData {
2023
tag_description : String @doc(description: "Query by tag_description.") ,
2124
number_products : Int @doc(description: "Query by number_products.") ,
2225
create_at : String @doc(description: "Query by create_at.") ,
26+
store_id : [Int] @doc(description: "Query by store_id.") ,
2327
}
2428

2529
type Product {

0 commit comments

Comments
 (0)