Skip to content

Commit 3edbfa6

Browse files
committed
updated module files
1 parent f53b238 commit 3edbfa6

File tree

6 files changed

+229
-1
lines changed

6 files changed

+229
-1
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
4+
namespace Lof\ProductTagsGraphQl\Model\Resolver\DataProvider;
5+
6+
class Product
7+
{
8+
9+
private $tag;
10+
11+
/**
12+
* @param \Lof\ProductTags\Api\Data\TagInterface $tag
13+
*/
14+
public function __construct(
15+
\Lof\ProductTags\Api\Data\TagInterface $tag
16+
) {
17+
$this->tag = $tag;
18+
}
19+
20+
public function getProduct()
21+
{
22+
return 'proviced data';
23+
}
24+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
namespace Lof\ProductTagsGraphQl\Model\Resolver\DataProvider;
8+
9+
use Lof\ProductTags\Api\Data\TagInterface;
10+
use Lof\ProductTags\Api\TagRepositoryInterface;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
13+
/**
14+
* Lof Product Tags data provider
15+
*/
16+
17+
class Tag
18+
{
19+
/**
20+
* @var TagRepositoryInterface
21+
*/
22+
private $tagRepository;
23+
24+
/**
25+
* @param \Lof\ProductTags\Api\TagRepositoryInterface $tagRepository
26+
*/
27+
public function __construct(
28+
\Lof\ProductTags\Api\TagRepositoryInterface $tagRepository
29+
) {
30+
$this->tagRepository = $tagRepository;
31+
}
32+
33+
/**
34+
* @param int $tagId
35+
* @return array
36+
* @throws NoSuchEntityException
37+
*/
38+
public function getData(int $tagId): array
39+
{
40+
$tag = $this->tagRepository->getById($tagId);
41+
42+
if (false === $tag->getStatus()) {
43+
throw new NoSuchEntityException();
44+
}
45+
46+
$tagData = [
47+
TagInterface::TAG_ID => $tag->getTagId(),
48+
TagInterface::TAG_STATUS => $tag->getStatus(),
49+
TagInterface::TAG_TITLE => $tag->getTagTitle(),
50+
TagInterface::TAG_IDENTIFIER => $tag->getIdentifier(),
51+
TagInterface::TAG_DESCRIPTION => $tag->getTagDescription(),
52+
];
53+
return $tagData;
54+
}
55+
}

Model/Resolver/Product.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
4+
namespace Lof\ProductTagsGraphQl\Model\Resolver;
5+
6+
use Magento\Framework\Exception\NoSuchEntityException;
7+
use Magento\Framework\GraphQl\Config\Element\Field;
8+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
9+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
10+
use Magento\Framework\GraphQl\Query\ResolverInterface;
11+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
12+
13+
class Product implements ResolverInterface
14+
{
15+
16+
private $productDataProvider;
17+
18+
/**
19+
* @param DataProvider\Product $productRepository
20+
*/
21+
public function __construct(
22+
DataProvider\Product $productDataProvider
23+
) {
24+
$this->productDataProvider = $productDataProvider;
25+
}
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
public function resolve(
31+
Field $field,
32+
$context,
33+
ResolveInfo $info,
34+
array $value = null,
35+
array $args = null
36+
) {
37+
$productData = $this->productDataProvider->getProduct();
38+
return $productData;
39+
}
40+
}

Model/Resolver/Tag.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 Lof\ProductTagsGraphQl\Model\Resolver;
9+
10+
use Lof\ProductTagsGraphQl\Model\Resolver\DataProvider\Tag as TagDataProvider;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
15+
use Magento\Framework\GraphQl\Query\ResolverInterface;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
18+
class Tag implements ResolverInterface
19+
{
20+
/**
21+
* @var TagDataProvider
22+
*/
23+
private $tagDataProvider;
24+
25+
/**
26+
* @param DataProvider\Tag $tagRepository
27+
*/
28+
public function __construct(DataProvider\Tag $tagDataProvider)
29+
{
30+
$this->tagDataProvider = $tagDataProvider;
31+
}
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function resolve(
37+
Field $field,
38+
$context,
39+
ResolveInfo $info,
40+
array $value = null,
41+
array $args = null
42+
) {
43+
$tagId = $this->getTagId($args);
44+
$tagData = $this->getTagData($tagId);
45+
46+
return $tagData;
47+
}
48+
49+
/**
50+
* @param array $args
51+
* @return int
52+
* @throws GraphQlInputException
53+
*/
54+
private function getTagId(array $args): int
55+
{
56+
if (!isset($args['id'])) {
57+
throw new GraphQlInputException(__('"Tag id should be specified'));
58+
}
59+
60+
return (int)$args['id'];
61+
}
62+
63+
/**
64+
* @param int $tagId
65+
* @return array
66+
* @throws GraphQlNoSuchEntityException
67+
*/
68+
private function getTagData(int $tagId): array
69+
{
70+
try {
71+
$tagData = $this->tagDataProvider->getData($tagId);
72+
} catch (NoSuchEntityException $e) {
73+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
74+
}
75+
return $tagData;
76+
}
77+
}

etc/module.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<?xml version="1.0" ?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3-
<module name="Lof_ProductTagsGraphQl" setup_version="1.0.0"/>
3+
<module name="Lof_ProductTagsGraphQl" setup_version="1.0.0">
4+
<sequence>
5+
<module name="Magento_GraphQl"/>
6+
<module name="Lof_ProductTags"/>
7+
</sequence>
8+
</module>
49
</config>

etc/schema.graphqls

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
type Query {
3+
4+
tag (
5+
id: Int @doc(description: "Query by id.")
6+
) : Tag @resolver( class: "Lof\\ProductTagsGraphQl\\Model\\Resolver\\Tag") ,
7+
product (
8+
product_id: Int @doc(description: "Query by product_id.")
9+
) : Product @resolver( class: "Lof\\ProductTagsGraphQl\\Model\\Resolver\\Product") ,
10+
}
11+
12+
type Tag {
13+
tag_id : Int @doc(description: "Query by tag_id.") ,
14+
status : Boolean @doc(description: "Query by status.") ,
15+
tag_title : String @doc(description: "Query by tag_title.") ,
16+
identifier : String @doc(description: "Query by identifier.") ,
17+
customer_id : Int @doc(description: "Query by customer_id.") ,
18+
tag_description : String @doc(description: "Query by tag_description.") ,
19+
number_products : Int @doc(description: "Query by number_products.") ,
20+
create_at : String @doc(description: "Query by create_at.") ,
21+
}
22+
23+
type Product {
24+
tag_id : Int @doc(description: "Query by tag_id.") ,
25+
product_id : Int @doc(description: "Query by product_id.") ,
26+
position : Int @doc(description: "Query by position.") ,
27+
}

0 commit comments

Comments
 (0)