Skip to content

Commit

Permalink
graphql-ce-120: get single store config instead multiple
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaliyboyko committed Jul 19, 2018
1 parent b5a2a98 commit e85a6e5
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 215 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\StoreGraphQl\Model\Resolver\Store;

use Magento\Store\Api\Data\StoreConfigInterface;
use Magento\Store\Api\StoreConfigManagerInterface;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\Api\StoreResolverInterface;

/**
* StoreConfig field data provider, used for GraphQL request processing.
*/
class StoreConfigDataProvider
{
/**
* @var StoreConfigManagerInterface
*/
private $storeConfigManager;

/**
* @var StoreResolverInterface
*/
private $storeResolver;

/**
* @var StoreRepositoryInterface
*/
private $storeRepository;

/**
* @param StoreConfigManagerInterface $storeConfigManager
* @param StoreResolverInterface $storeResolver
* @param StoreRepositoryInterface $storeRepository
*/
public function __construct(
StoreConfigManagerInterface $storeConfigManager,
StoreResolverInterface $storeResolver,
StoreRepositoryInterface $storeRepository
) {
$this->storeConfigManager = $storeConfigManager;
$this->storeResolver = $storeResolver;
$this->storeRepository = $storeRepository;
}

/**
* Get store config for current store
*
* @return array
*/
public function getStoreConfig() : array
{
$storeId = $this->storeResolver->getCurrentStoreId();
$store = $this->storeRepository->getById($storeId);
$storeConfig = current($this->storeConfigManager->getStoreConfigs([$store->getCode()]));

return $this->hidrateStoreConfig($storeConfig);
}

/**
* Transform StoreConfig object to in array format
*
* @param StoreConfigInterface $storeConfig
* @return array
*/
private function hidrateStoreConfig($storeConfig): array
{
/** @var StoreConfigInterface $storeConfig */
$storeConfigData = [
'id' => $storeConfig->getId(),
'code' => $storeConfig->getCode(),
'website_id' => $storeConfig->getWebsiteId(),
'locale' => $storeConfig->getLocale(),
'base_currency_code' => $storeConfig->getBaseCurrencyCode(),
'default_display_currency_code' => $storeConfig->getDefaultDisplayCurrencyCode(),
'timezone' => $storeConfig->getTimezone(),
'weight_unit' => $storeConfig->getWeightUnit(),
'base_url' => $storeConfig->getBaseUrl(),
'base_link_url' => $storeConfig->getBaseLinkUrl(),
'base_static_url' => $storeConfig->getSecureBaseStaticUrl(),
'base_media_url' => $storeConfig->getBaseMediaUrl(),
'secure_base_url' => $storeConfig->getSecureBaseUrl(),
'secure_base_link_url' => $storeConfig->getSecureBaseLinkUrl(),
'secure_base_static_url' => $storeConfig->getSecureBaseStaticUrl(),
'secure_base_media_url' => $storeConfig->getSecureBaseMediaUrl()
];

return $storeConfigData;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\StoreGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\StoreGraphQl\Model\Resolver\Store\StoreConfigDataProvider;

/**
* StoreConfig page field resolver, used for GraphQL request processing.
*/
class StoreConfigResolver implements ResolverInterface
{
/**
* @var StoreConfigDataProvider
*/
private $storeConfigDataProvider;

/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @param StoreConfigDataProvider $storeConfigsDataProvider
* @param ValueFactory $valueFactory
*/
public function __construct(
StoreConfigDataProvider $storeConfigsDataProvider,
ValueFactory $valueFactory
) {
$this->valueFactory = $valueFactory;
$this->storeConfigDataProvider = $storeConfigsDataProvider;
}

/**
* {@inheritdoc}
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) : Value {

$storeConfigData = $this->storeConfigDataProvider->getStoreConfig();

$result = function () use ($storeConfigData) {
return !empty($storeConfigData) ? $storeConfigData : [];
};

return $this->valueFactory->create($result);
}
}

This file was deleted.

9 changes: 1 addition & 8 deletions app/code/Magento/StoreGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.
type Query {
storeConfigs (
storeCodes: [String] @doc(description: "Store Codes of the store configs")
): StoreConfigs
@resolver(class: "Magento\\StoreGraphQl\\Model\\Resolver\\StoreConfigsResolver") @doc(description: "The store configs query")
storeConfig : StoreConfig @resolver(class: "Magento\\StoreGraphQl\\Model\\Resolver\\StoreConfigResolver") @doc(description: "The store config query")
}

type Website @doc(description: "The type contains information about a website") {
Expand All @@ -16,10 +13,6 @@ type Website @doc(description: "The type contains information about a website")
is_default : Boolean @doc(description: "Specifies if this is the default website")
}

type StoreConfigs @doc(description: "The Store Configs object") {
items: [StoreConfig] @doc(description: "An array containing store configs")
}

type StoreConfig @doc(description: "The type contains information about a store config") {
id : Int @doc(description: "The ID number assigned to the store")
code : String @doc(description: "A code assigned to the store to identify it")
Expand Down
Loading

0 comments on commit e85a6e5

Please sign in to comment.