Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions Model/Resolver/Authors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/
declare(strict_types=1);

namespace Magefan\BlogGraphQl\Model\Resolver;

use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\Search\FilterGroupBuilder;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\Builder as SearchCriteriaBuilder;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magefan\Blog\Api\AuthorRepositoryInterface;
use Magento\Framework\Api\SortOrderBuilder;
use Magento\Framework\App\ScopeResolverInterface;

class Authors implements ResolverInterface
{
/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;
/**
* @var AuthorRepositoryInterface
*/
private $authorRepository;

/**
* @var SortOrderBuilder
*/
protected $sortOrderBuilder;

/**
* @var DataProvider\Author
*/
protected $authorDataProvider;
/**
* @var FilterBuilder
*/
protected $filterBuilder;
/**
* @var FilterGroupBuilder
*/
protected $filterGroupBuilder;

/**
* @var ScopeResolverInterface
*/
private $scopeResolver;

/**
* Authors constructor.
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param AuthorRepositoryInterface $authorRepository
* @param SortOrderBuilder $sortOrderBuilder
* @param DataProvider\Author $authorDataProvider
* @param FilterBuilder $filterBuilder
* @param FilterGroupBuilder $filterGroupBuilder
* @param ScopeResolverInterface $scopeResolver
*/
public function __construct(
SearchCriteriaBuilder $searchCriteriaBuilder,
AuthorRepositoryInterface $authorRepository,
SortOrderBuilder $sortOrderBuilder,
DataProvider\Author $authorDataProvider,
FilterBuilder $filterBuilder,
FilterGroupBuilder $filterGroupBuilder,
ScopeResolverInterface $scopeResolver
) {
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->authorRepository = $authorRepository;
$this->sortOrderBuilder = $sortOrderBuilder;
$this->authorDataProvider = $authorDataProvider;
$this->filterBuilder = $filterBuilder;
$this->filterGroupBuilder = $filterGroupBuilder;
$this->scopeResolver = $scopeResolver;
}
/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
?array $value = null,
?array $args = null
) {
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
$searchCriteria = $this->searchCriteriaBuilder->build('di_build_magefan_blog_authot', $args);
$statusFilter = $this->filterBuilder
->setField('is_active')
->setValue(1)
->setConditionType('eq')
->create();

$filterGroups = $searchCriteria->getFilterGroups();
$filterGroups[] = $this->filterGroupBuilder->addFilter($statusFilter)->create();

$scope = $this->scopeResolver->getScope()->getId();

$scopeFilter = $this->filterBuilder
->setField('store_id')
->setValue($args['storeId'] ?? $scope)
->setConditionType('eq')
->create();
$filterGroups[] = $this->filterGroupBuilder->addFilter($scopeFilter)->create();

if (isset($args['filter']['author_id']['in'])) {
$authorIdFilter = $this->filterBuilder
->setField('author_id')
->setValue($args['filter']['author_id']['in'])
->setConditionType('in')
->create();
$filterGroups[] = $this->filterGroupBuilder->addFilter($authorIdFilter)->create();
}

$searchCriteria->setFilterGroups($filterGroups);


if (isset($args['sort'])) {
$sortOrder = $this->sortOrderBuilder
->setField(isset($args['sortFiled']) ? $args['sortFiled'] : 'update_time')
->setDirection($args['sort'][0])
->create();
$searchCriteria->setSortOrders([$sortOrder]);
}

$searchResult = $this->authorRepository->getList($searchCriteria);

$items = $searchResult->getItems();
$fields = $info ? $info->getFieldSelection(10) : null;

foreach ($items as $k => $data) {
$items[$k] = $this->authorDataProvider->getData(
$data,
isset($fields['items']) ? $fields['items'] : null,
$storeId
);
}

return [
'total_count' => $searchResult->getTotalCount(),
'items' => $items
];
}
}
2 changes: 1 addition & 1 deletion Model/Resolver/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function resolve(

foreach ($items as $k => $data) {
$items[$k] = $this->categoryDataProvider->getData(
$data['category_id'],
$data,
isset($fields['items']) ? $fields['items'] : null
);
}
Expand Down
2 changes: 1 addition & 1 deletion Model/Resolver/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function resolve(

foreach ($items as $k => $data) {
$items[$k] = $this->commentDataProvider->getData(
$data['comment_id'],
$data,
isset($fields['items']) ? $fields['items'] : null
);
}
Expand Down
46 changes: 28 additions & 18 deletions Model/Resolver/DataProvider/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,22 @@ public function __construct(
/**
* Get author data
*
* @param string $authorId
* @param mixed $authorId
* @param null $fields
* @return array
* @throws NoSuchEntityException
*/
public function getData(string $authorId): array
public function getData($authorId, $fields = null): array
{
$author = $this->authorRepository->getFactory()->create();
$author->getResource()->load($author, $authorId);
if (is_object($authorId)) {
$author = $authorId;
} else {
try {
$author = $this->authorRepository->getById((int)$authorId);
} catch (\Exception $e) {
throw new NoSuchEntityException();
}
}

if (!$author->isActive()) {
throw new NoSuchEntityException();
Expand All @@ -93,15 +101,15 @@ public function getData(string $authorId): array
$data = [];
$this->state->emulateAreaCode(
Area::AREA_FRONTEND,
function () use ($author, &$data) {
function () use ($author, $fields, &$data) {
$themeId = $this->scopeConfig->getValue(
'design/theme/theme_id',
ScopeInterface::SCOPE_STORE
);
$theme = $this->themeProvider->getThemeById($themeId);
$this->design->setDesignTheme($theme, Area::AREA_FRONTEND);

$data = $this->getDynamicData($author);
$data = $this->getDynamicData($author, $fields);

return $data;
}
Expand Down Expand Up @@ -136,18 +144,20 @@ public function getDynamicData($author, $fields = null)
$data['author_id'] = $author->getId();

foreach ($keys as $key) {
$method = 'get' . str_replace(
'_',
'',
ucwords($key, '_')
);
$data[$key] = $author->$method();
if ($key === 'author_url') {
$data[$key] = str_replace(
'/' . $this->scopeResolver->getScope()->getCode() . '/',
'/',
$data[$key]
);
if (null === $fields || array_key_exists($key, $fields)) {
$method = 'get' . str_replace(
'_',
'',
ucwords($key, '_')
);
$data[$key] = $author->$method();
if ($key === 'author_url') {
$data[$key] = str_replace(
'/' . $this->scopeResolver->getScope()->getCode() . '/',
'/',
$data[$key]
);
}
}
}

Expand Down
15 changes: 11 additions & 4 deletions Model/Resolver/DataProvider/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,22 @@ public function __construct(
/**
* Get category data
*
* @param string $categoryId
* @param mixed $categoryId
* @param null|array $fields
* @return array
* @throws NoSuchEntityException
*/
public function getData(string $categoryId, $fields = null): array
public function getData($categoryId, $fields = null): array
{
$category = $this->categoryRepository->getFactory()->create();
$category->getResource()->load($category, $categoryId);
if (is_object($categoryId)) {
$category = $categoryId;
} else {
try {
$category = $this->categoryRepository->getById((int)$categoryId);
} catch (\Exception $e) {
throw new NoSuchEntityException();
}
}

if (!$category->isActive()) {
throw new NoSuchEntityException();
Expand Down
15 changes: 11 additions & 4 deletions Model/Resolver/DataProvider/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,22 @@ public function __construct(
/**
* Get comment data
*
* @param string $commentId
* @param mixed $commentId
* @param null|array $fields
* @return array
* @throws NoSuchEntityException
*/
public function getData(string $commentId, $fields = null): array
public function getData($commentId, $fields = null): array
{
$comment = $this->commentRepository->getFactory()->create();
$comment->getResource()->load($comment, $commentId);
if (is_object($commentId)) {
$comment = $commentId;
} else {
try {
$comment = $this->commentRepository->getById((int)$commentId);
} catch (\Exception $e) {
throw new NoSuchEntityException();
}
}

if (!$comment->isActive()) {
throw new NoSuchEntityException();
Expand Down
15 changes: 11 additions & 4 deletions Model/Resolver/DataProvider/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,23 @@ public function __construct(
/**
* Get post data
*
* @param string $postId
* @param mixed $postId
* @param array|null $fields
* @param null|int|string $storeId
* @return array
* @throws NoSuchEntityException
*/
public function getData(string $postId, $fields = null, $storeId = null): array
public function getData($postId, $fields = null, $storeId = null): array
{
$post = $this->postRepository->getFactory()->create();
$post->getResource()->load($post, $postId);
if (is_object($postId)) {
$post = $postId;
} else {
try {
$post = $this->postRepository->getById((int)$postId);
} catch (\Exception $e) {
throw new NoSuchEntityException();
}
}

if (!$post->isActive()) {
throw new NoSuchEntityException();
Expand Down
15 changes: 11 additions & 4 deletions Model/Resolver/DataProvider/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,21 @@ public function __construct(
/**
* Get tag data
*
* @param string $tagId
* @param mixed $tagId
* @return array
* @throws NoSuchEntityException
*/
public function getData(string $tagId): array
public function getData($tagId): array
{
$tag = $this->tagRepository->getFactory()->create();
$tag->getResource()->load($tag, $tagId);
if (is_object($tagId)) {
$tag = $tagId;
} else {
try {
$tag = $this->tagRepository->getById((int)$tagId);
} catch (\Exception $e) {
throw new NoSuchEntityException();
}
}

if (!$tag->isActive()) {
throw new NoSuchEntityException();
Expand Down
2 changes: 1 addition & 1 deletion Model/Resolver/Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function resolve(

foreach ($items as $k => $data) {
$items[$k] = $this->postDataProvider->getData(
$data['post_id'],
$data,
isset($fields['items']) ? $fields['items'] : null,
$storeId
);
Expand Down
2 changes: 1 addition & 1 deletion Model/Resolver/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function resolve(
$items = $searchResult->getItems();

foreach ($items as $k => $data) {
$items[$k] = $this->tagDataProvider->getData($data['tag_id']);
$items[$k] = $this->tagDataProvider->getData($data);
}

return [
Expand Down
8 changes: 8 additions & 0 deletions etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type Query {
blogCategories(
storeId: Int = 0 @doc(description: "Specifies the store view ID to filter the results. The default value is 0, meaning no specific store filter applied.")
): blogCategoriesOutput @resolver(class: "\\Magefan\\BlogGraphQl\\Model\\Resolver\\Categories") @doc(description: "")
blogAuthors(
storeId: Int = 0 @doc(description: "Specifies the store view ID to filter the results. The default value is 0, meaning no specific store filter applied.")
): blogAuthorsOutput @resolver(class: "\\Magefan\\BlogGraphQl\\Model\\Resolver\\Authors") @doc(description: "")
}

type Mutation {
Expand Down Expand Up @@ -103,6 +106,11 @@ type blogCategoriesOutput {
items: [BlogCategory] @doc(description: "")
}

type blogAuthorsOutput {
total_count: Int @doc(description: "")
items: [BlogAuthor] @doc(description: "")
}

type BlogPost implements RoutableInterface {
post_id: Int @doc(description: "Id of the Blog Post")
post_url: String @doc(description: "Blog Post URL")
Expand Down