Skip to content

Commit 378e693

Browse files
author
msgchinh
committed
update graphql
1 parent 8b965cf commit 378e693

11 files changed

+515
-4
lines changed

Model/Resolver/Categories.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Lof\FaqGraphQl\Model\Resolver;
5+
6+
use Magento\Framework\GraphQl\Config\Element\Field;
7+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
8+
use Magento\Framework\GraphQl\Query\ResolverInterface;
9+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
10+
use Lof\Faq\Api\CategoriesInterface;
11+
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\Builder as SearchCriteriaBuilder;
12+
13+
class Categories implements ResolverInterface
14+
{
15+
/**
16+
* @var SearchCriteriaBuilder
17+
*/
18+
private $searchCriteriaBuilder;
19+
/**
20+
* @var CategoriesInterface
21+
*/
22+
private $categoryInterface;
23+
24+
public function __construct(
25+
CategoriesInterface $categories,
26+
SearchCriteriaBuilder $searchCriteriaBuilder
27+
) {
28+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
29+
$this->categoryInterface = $categories;
30+
}
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
public function resolve(
36+
Field $field,
37+
$context,
38+
ResolveInfo $info,
39+
array $value = null,
40+
array $args = null
41+
) {
42+
if ($args['currentPage'] < 1) {
43+
throw new GraphQlInputException(__('currentPage value must be greater than 0.'));
44+
}
45+
if ($args['pageSize'] < 1) {
46+
throw new GraphQlInputException(__('pageSize value must be greater than 0.'));
47+
}
48+
$searchCriteria = $this->searchCriteriaBuilder->build('faqCategories', $args);
49+
$searchCriteria->setCurrentPage($args['currentPage']);
50+
$searchCriteria->setPageSize($args['pageSize']);
51+
$search = '';
52+
if (isset($args['search']) && $args['search']) {
53+
$search = $args['search'];
54+
}
55+
$searchResult = $this->categoryInterface->getList($searchCriteria, $search);
56+
$totalPages = $args['pageSize'] ? ((int)ceil($searchResult->getTotalCount() / $args['pageSize'])) : 0;
57+
58+
return [
59+
'total_count' => $searchResult->getTotalCount(),
60+
'items' => $searchResult->getItems(),
61+
'page_info' => [
62+
'page_size' => $args['pageSize'],
63+
'current_page' => $args['currentPage'],
64+
'total_pages' => $totalPages
65+
],
66+
];
67+
}
68+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
namespace Lof\FaqGraphQl\Model\Resolver;
3+
4+
use Magento\Framework\Exception\LocalizedException;
5+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
6+
use Magento\Framework\GraphQl\Config\Element\Field;
7+
use Magento\Framework\GraphQl\Query\Resolver\Value;
8+
use Magento\Framework\GraphQl\Query\ResolverInterface;
9+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
10+
use Lof\Faq\Api\QuestionListByCategoryInterface;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
13+
/**
14+
* Class to resolve custom attribute_set_name field in faq category GraphQL query
15+
*/
16+
class CategoryImageResolver implements ResolverInterface
17+
{
18+
19+
/**
20+
* @var QuestionListByCategoryInterface
21+
*/
22+
private $questionListByCategory;
23+
/**
24+
* @var StoreManagerInterface
25+
*/
26+
private $_storeManager;
27+
28+
/**
29+
* CategoryQuestionResolver constructor.
30+
* @param QuestionListByCategoryInterface $questionListByCategory
31+
* @param StoreManagerInterface $storeManager
32+
*/
33+
public function __construct(
34+
QuestionListByCategoryInterface $questionListByCategory,
35+
StoreManagerInterface $storeManager
36+
) {
37+
$this->questionListByCategory = $questionListByCategory;
38+
$this->_storeManager = $storeManager;
39+
}
40+
41+
/**
42+
* @param Field $field
43+
* @param ContextInterface $context
44+
* @param ResolveInfo $info
45+
* @param array|null $value
46+
* @param array|null $args
47+
* @return array|Value|mixed
48+
* @throws LocalizedException
49+
*/
50+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
51+
{
52+
if (isset($value['image']) && $value['image']) {
53+
return $this->_storeManager->getStore()->getBaseUrl(
54+
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
55+
) . $value['image'];
56+
} else {
57+
return '';
58+
}
59+
}
60+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
namespace Lof\FaqGraphQl\Model\Resolver;
3+
4+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
5+
use Magento\Framework\GraphQl\Config\Element\Field;
6+
use Magento\Framework\GraphQl\Query\Resolver\Value;
7+
use Magento\Framework\GraphQl\Query\ResolverInterface;
8+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
9+
use Lof\Faq\Api\QuestionListByCategoryInterface;
10+
11+
/**
12+
* Class to resolve custom attribute_set_name field in faq category GraphQL query
13+
*/
14+
class CategoryQuestionResolver implements ResolverInterface
15+
{
16+
17+
/**
18+
* @var QuestionListByCategoryInterface
19+
*/
20+
private $questionListByCategory;
21+
22+
/**
23+
* CategoryQuestionResolver constructor.
24+
* @param QuestionListByCategoryInterface $questionListByCategory
25+
*/
26+
public function __construct(
27+
QuestionListByCategoryInterface $questionListByCategory
28+
) {
29+
$this->questionListByCategory = $questionListByCategory;
30+
}
31+
32+
/**
33+
* @param Field $field
34+
* @param ContextInterface $context
35+
* @param ResolveInfo $info
36+
* @param array|null $value
37+
* @param array|null $args
38+
* @return array|Value|mixed
39+
* @throws \Magento\Framework\Exception\LocalizedException
40+
*/
41+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
42+
{
43+
if (isset($value['category_id']) && $value['category_id']) {
44+
$result = $this->questionListByCategory->getQuestionByCategoryForApi($value['category_id']);
45+
return [
46+
'total_count' => $result->getTotalCount(),
47+
'items' => $result->getItems()
48+
];
49+
} else {
50+
return [];
51+
}
52+
}
53+
}

Model/Resolver/FaqQuestion.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33

44
namespace Lof\FaqGraphQl\Model\Resolver;
55

6-
use Magento\Framework\Exception\NoSuchEntityException;
76
use Magento\Framework\GraphQl\Config\Element\Field;
8-
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
9-
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
107
use Magento\Framework\GraphQl\Query\ResolverInterface;
118
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
129

@@ -16,7 +13,7 @@ class FaqQuestion implements ResolverInterface
1613
private $faqQuestionDataProvider;
1714

1815
/**
19-
* @param DataProvider\FaqQuestion $faqQuestionRepository
16+
* @param DataProvider\FaqQuestion $faqQuestionDataProvider
2017
*/
2118
public function __construct(
2219
DataProvider\FaqQuestion $faqQuestionDataProvider
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Lof\FaqGraphQl\Model\Resolver;
6+
7+
use Magento\Framework\GraphQl\Config\Element\Field;
8+
use Magento\Framework\GraphQl\ConfigInterface;
9+
use Magento\Framework\GraphQl\Query\Resolver\Argument\FieldEntityAttributesInterface;
10+
11+
class FilterArgumentCategories implements FieldEntityAttributesInterface
12+
{
13+
/** @var ConfigInterface */
14+
private $config;
15+
16+
/**
17+
* FilterArgumentCategories constructor.
18+
* @param ConfigInterface $config
19+
*/
20+
public function __construct(ConfigInterface $config)
21+
{
22+
$this->config = $config;
23+
}
24+
25+
/**
26+
* @return array
27+
*/
28+
public function getEntityAttributes(): array
29+
{
30+
$fields = [];
31+
/** @var Field $field */
32+
foreach ($this->config->getConfigElement('FaqCategory')->getFields() as $field) {
33+
$fields[$field->getName()] = [
34+
'type' => 'String',
35+
'fieldName' => $field->getName(),
36+
];
37+
}
38+
return $fields;
39+
}
40+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
namespace Lof\FaqGraphQl\Model\Resolver;
3+
4+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
5+
use Magento\Framework\GraphQl\Config\Element\Field;
6+
use Magento\Framework\GraphQl\Query\Resolver\Value;
7+
use Magento\Framework\GraphQl\Query\ResolverInterface;
8+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
9+
use Lof\Faq\Api\CategoriesInterface;
10+
11+
/**
12+
* Class to resolve custom attribute_set_name field in faq question GraphQL query
13+
*/
14+
class QuestionCategoryResolver implements ResolverInterface
15+
{
16+
17+
/**
18+
* @var CategoriesInterface
19+
*/
20+
private $categoryInterface;
21+
22+
public function __construct(
23+
CategoriesInterface $categoryInterface
24+
) {
25+
$this->categoryInterface = $categoryInterface;
26+
}
27+
28+
/**
29+
* @param Field $field
30+
* @param ContextInterface $context
31+
* @param ResolveInfo $info
32+
* @param array|null $value
33+
* @param array|null $args
34+
* @return array|Value|mixed
35+
*/
36+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
37+
{
38+
if (isset($value['category_id']) && $value['category_id']) {
39+
$items = [];
40+
foreach ($value['category_id'] as $categoryId) {
41+
$items[] = $this->categoryInterface->getById($categoryId);
42+
}
43+
return [
44+
'total_count' => count($items),
45+
'items' => $items
46+
];
47+
} else {
48+
return [];
49+
}
50+
}
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
namespace Lof\FaqGraphQl\Model\Resolver;
3+
4+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
5+
use Magento\Framework\GraphQl\Config\Element\Field;
6+
use Magento\Framework\GraphQl\Query\Resolver\Value;
7+
use Magento\Framework\GraphQl\Query\ResolverInterface;
8+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
9+
use Lof\Faq\Api\QuestionInfoByIdInterface;
10+
11+
/**
12+
* Class to resolve custom attribute_set_name field in faq question GraphQL query
13+
*/
14+
class QuestionRelatedResolver implements ResolverInterface
15+
{
16+
17+
/**
18+
* @var QuestionInfoByIdInterface
19+
*/
20+
private $questionRepository;
21+
22+
public function __construct(
23+
QuestionInfoByIdInterface $questionRepository
24+
) {
25+
$this->questionRepository = $questionRepository;
26+
}
27+
28+
/**
29+
* @param Field $field
30+
* @param ContextInterface $context
31+
* @param ResolveInfo $info
32+
* @param array|null $value
33+
* @param array|null $args
34+
* @return array|Value|mixed
35+
*/
36+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
37+
{
38+
if (isset($value['relatedquestions']) && $value['relatedquestions']) {
39+
$items = [];
40+
foreach ($value['relatedquestions'] as $relatedquestion) {
41+
if (isset($relatedquestion['relatedquestion_id']) && $relatedquestion['relatedquestion_id']) {
42+
$items[] = $this->questionRepository->getById($relatedquestion['relatedquestion_id']);
43+
}
44+
}
45+
return [
46+
'total_count' => count($items),
47+
'items' => $items
48+
];
49+
} else {
50+
return [];
51+
}
52+
}
53+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
namespace Lof\FaqGraphQl\Model\Resolver;
3+
4+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
5+
use Magento\Framework\GraphQl\Config\Element\Field;
6+
use Magento\Framework\GraphQl\Query\Resolver\Value;
7+
use Magento\Framework\GraphQl\Query\ResolverInterface;
8+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
9+
use Lof\Faq\Model\ResourceModel\Tag\Collection;
10+
11+
/**
12+
* Class to resolve custom attribute_set_name field in faq question GraphQL query
13+
*/
14+
class QuestionTagResolver implements ResolverInterface
15+
{
16+
17+
/**
18+
* @var Collection
19+
*/
20+
private $tagCollection;
21+
22+
public function __construct(
23+
Collection $tagCollection
24+
) {
25+
$this->tagCollection = $tagCollection;
26+
}
27+
28+
/**
29+
* @param Field $field
30+
* @param ContextInterface $context
31+
* @param ResolveInfo $info
32+
* @param array|null $value
33+
* @param array|null $args
34+
* @return array|Value|mixed
35+
*/
36+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
37+
{
38+
if (isset($value['question_id']) && $value['question_id']) {
39+
$collection = $this->tagCollection->addFieldToFilter('question_id', $value['question_id']);
40+
return [
41+
'total_count' => $collection->getSize(),
42+
'items' => $collection->getItems()
43+
];
44+
} else {
45+
return [];
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)