Skip to content

Commit

Permalink
Merge pull request #18 from magefan/6821-pwa-blog-integration
Browse files Browse the repository at this point in the history
Add PWA studio 12 compatibility
  • Loading branch information
magefan authored Aug 17, 2022
2 parents 5bb754e + 3d2630a commit 806a60b
Show file tree
Hide file tree
Showing 14 changed files with 286 additions and 13 deletions.
31 changes: 31 additions & 0 deletions Model/BlogArchiveTypeResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magefan ([email protected]). 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;

use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;

/**
* @inheritdoc
*/
class BlogArchiveTypeResolver implements TypeResolverInterface
{
const MF_BLOG_ARCHIVE = 'MF_BLOG_ARCHIVE';
const TYPE_RESOLVER = 'blogPostsOutput';

/**
* @inheritdoc
*/
public function resolveType(array $data) : string
{
if (isset($data['type_id']) && $data['type_id'] == self::MF_BLOG_ARCHIVE) {
return self::TYPE_RESOLVER;
}
return '';
}
}
31 changes: 31 additions & 0 deletions Model/BlogAuthorTypeResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magefan ([email protected]). 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;

use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;

/**
* @inheritdoc
*/
class BlogAuthorTypeResolver implements TypeResolverInterface
{
const MF_BLOG_AUTHOR = 'MF_BLOG_AUTHOR';
const TYPE_RESOLVER = 'BlogAuthor';

/**
* @inheritdoc
*/
public function resolveType(array $data) : string
{
if (isset($data['type_id']) && $data['type_id'] == self::MF_BLOG_AUTHOR) {
return self::TYPE_RESOLVER;
}
return '';
}
}
31 changes: 31 additions & 0 deletions Model/BlogCategoryTypeResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magefan ([email protected]). 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;

use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;

/**
* @inheritdoc
*/
class BlogCategoryTypeResolver implements TypeResolverInterface
{
const MF_BLOG_CATEGORY = 'MF_BLOG_CATEGORY';
const TYPE_RESOLVER = 'BlogCategory';

/**
* @inheritdoc
*/
public function resolveType(array $data) : string
{
if (isset($data['type_id']) && $data['type_id'] == self::MF_BLOG_CATEGORY) {
return self::TYPE_RESOLVER;
}
return '';
}
}
31 changes: 31 additions & 0 deletions Model/BlogIndexTypeResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magefan ([email protected]). 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;

use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;

/**
* @inheritdoc
*/
class BlogIndexTypeResolver implements TypeResolverInterface
{
const MF_BLOG_INDEX = 'MF_BLOG_INDEX';
const TYPE_RESOLVER = 'blogPostsOutput';

/**
* @inheritdoc
*/
public function resolveType(array $data) : string
{
if (isset($data['type_id']) && $data['type_id'] == self::MF_BLOG_INDEX) {
return self::TYPE_RESOLVER;
}
return '';
}
}
31 changes: 31 additions & 0 deletions Model/BlogPostTypeResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magefan ([email protected]). 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;

use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;

/**
* @inheritdoc
*/
class BlogPostTypeResolver implements TypeResolverInterface
{
const MF_BLOG_POST = 'MF_BLOG_POST';
const TYPE_RESOLVER = 'BlogPost';

/**
* @inheritdoc
*/
public function resolveType(array $data) : string
{
if (isset($data['type_id']) && $data['type_id'] == self::MF_BLOG_POST) {
return self::TYPE_RESOLVER;
}
return '';
}
}
31 changes: 31 additions & 0 deletions Model/BlogSearchTypeResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magefan ([email protected]). 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;

use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;

/**
* @inheritdoc
*/
class BlogSearchTypeResolver implements TypeResolverInterface
{
const MF_BLOG_SEARCH = 'MF_BLOG_SEARCH';
const TYPE_RESOLVER = 'blogPostsOutput';

/**
* @inheritdoc
*/
public function resolveType(array $data) : string
{
if (isset($data['type_id']) && $data['type_id'] == self::MF_BLOG_SEARCH) {
return self::TYPE_RESOLVER;
}
return '';
}
}
31 changes: 31 additions & 0 deletions Model/BlogTagTypeResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magefan ([email protected]). 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;

use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;

/**
* @inheritdoc
*/
class BlogTagTypeResolver implements TypeResolverInterface
{
const MF_BLOG_TAG = 'MF_BLOG_TAG';
const TYPE_RESOLVER = 'BlogTag';

/**
* @inheritdoc
*/
public function resolveType(array $data) : string
{
if (isset($data['type_id']) && $data['type_id'] == self::MF_BLOG_TAG) {
return self::TYPE_RESOLVER;
}
return '';
}
}
2 changes: 2 additions & 0 deletions Model/Resolver/DataProvider/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public function getDynamicData($category, $fields = null)

$breadcrumbs[] = [
'category_id' => $categoryData->getId(),
'category_uid' => $categoryData->getId(),
'category_name' => $categoryData->getTitle(),
'category_level' => $categoryData->getLevel(),
'category_url_key' => $categoryData->getIdentifier(),
Expand All @@ -162,6 +163,7 @@ public function getDynamicData($category, $fields = null)
$categoryData = $category;
$breadcrumbs[] = [
'category_id' => $categoryData->getId(),
'category_uid' => $categoryData->getId(),
'category_name' => $categoryData->getTitle(),
'category_level' => $categoryData->getLevel(),
'category_url_key' => $categoryData->getIdentifier(),
Expand Down
6 changes: 3 additions & 3 deletions Model/Resolver/DataProvider/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function __construct(
* @return array
* @throws NoSuchEntityException
*/
public function getData(string $postId, $fields = null): array
public function getData(string $postId, $fields = null, $storeId = null): array
{
$post = $this->postRepository->getFactory()->create();
$post->getResource()->load($post, $postId);
Expand All @@ -121,14 +121,14 @@ public function getData(string $postId, $fields = null): array
$data = [];
$this->state->emulateAreaCode(
Area::AREA_FRONTEND,
function () use ($post, $fields, &$data) {
function () use ($post, $fields, &$data, $storeId) {
$themeId = $this->scopeConfig->getValue(
'design/theme/theme_id',
ScopeInterface::SCOPE_STORE
);
$theme = $this->themeProvider->getThemeById($themeId);
$this->design->setDesignTheme($theme, Area::AREA_FRONTEND);

$post->setStoreId($storeId);
$data = $this->getDynamicData($post, $fields);

return $data;
Expand Down
3 changes: 2 additions & 1 deletion Model/Resolver/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ public function resolve(
array $value = null,
array $args = null
) {
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
$postId = $this->getPostId($args);
$fields = $info ? $info->getFieldSelection(10) : null;

try {
$postData = $this->postDataProvider->getData($postId, $fields);
$postData = $this->postDataProvider->getData($postId, $fields, $storeId);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
}
Expand Down
4 changes: 3 additions & 1 deletion Model/Resolver/Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function resolve(
array $value = null,
array $args = null
) {
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
$searchCriteria = $this->searchCriteriaBuilder->build('di_build_magefan_blog_post', $args);
$statusFilter = $this->filterBuilder
->setField('is_active')
Expand Down Expand Up @@ -161,7 +162,8 @@ public function resolve(
foreach ($items as $k => $data) {
$items[$k] = $this->postDataProvider->getData(
$data['post_id'],
isset($fields['items']) ? $fields['items'] : null
isset($fields['items']) ? $fields['items'] : null,
$storeId
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ public function afterResolve(

$result = [
'id' => $blogPage['id'],
'type' => $type
'type' => $type,
'type_id' => $type,
'relative_url' => $path,
'redirect_code' => 0
];

return $result;
Expand Down
50 changes: 49 additions & 1 deletion etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,53 @@
</argument>
</arguments>
</type>

<type name="Magento\UrlRewriteGraphQl\Model\RoutableInterfaceTypeResolver">
<arguments>
<argument name="productTypeNameResolvers" xsi:type="array">
<item name="mf_blog_index_type_resolver" xsi:type="object">Magefan\BlogGraphQl\Model\BlogIndexTypeResolver</item>
</argument>
</arguments>
</type>
<type name="Magento\UrlRewriteGraphQl\Model\RoutableInterfaceTypeResolver">
<arguments>
<argument name="productTypeNameResolvers" xsi:type="array">
<item name="mf_blog_post_type_resolver" xsi:type="object">Magefan\BlogGraphQl\Model\BlogPostTypeResolver</item>
</argument>
</arguments>
</type>
<type name="Magento\UrlRewriteGraphQl\Model\RoutableInterfaceTypeResolver">
<arguments>
<argument name="productTypeNameResolvers" xsi:type="array">
<item name="mf_blog_category_type_resolver" xsi:type="object">Magefan\BlogGraphQl\Model\BlogCategoryTypeResolver</item>
</argument>
</arguments>
</type>
<type name="Magento\UrlRewriteGraphQl\Model\RoutableInterfaceTypeResolver">
<arguments>
<argument name="productTypeNameResolvers" xsi:type="array">
<item name="mf_blog_tag_type_resolver" xsi:type="object">Magefan\BlogGraphQl\Model\BlogTagTypeResolver</item>
</argument>
</arguments>
</type>
<type name="Magento\UrlRewriteGraphQl\Model\RoutableInterfaceTypeResolver">
<arguments>
<argument name="productTypeNameResolvers" xsi:type="array">
<item name="mf_blog_author_type_resolver" xsi:type="object">Magefan\BlogGraphQl\Model\BlogAuthorTypeResolver</item>
</argument>
</arguments>
</type>
<type name="Magento\UrlRewriteGraphQl\Model\RoutableInterfaceTypeResolver">
<arguments>
<argument name="productTypeNameResolvers" xsi:type="array">
<item name="mf_blog_archive_type_resolver" xsi:type="object">Magefan\BlogGraphQl\Model\BlogArchiveTypeResolver</item>
</argument>
</arguments>
</type>
<type name="Magento\UrlRewriteGraphQl\Model\RoutableInterfaceTypeResolver">
<arguments>
<argument name="productTypeNameResolvers" xsi:type="array">
<item name="mf_blog_search_type_resolver" xsi:type="object">Magefan\BlogGraphQl\Model\BlogSearchTypeResolver</item>
</argument>
</arguments>
</type>
</config>
Loading

0 comments on commit 806a60b

Please sign in to comment.