Skip to content

Commit

Permalink
graphql-ce-120: added store configs schema and resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaliyboyko committed Jul 18, 2018
1 parent 1d469f8 commit e1f53d4
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?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;

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

/**
* @param StoreConfigManagerInterface $storeConfigManager
*/
public function __construct(
StoreConfigManagerInterface $storeConfigManager
) {
$this->storeConfigManager = $storeConfigManager;
}

/**
* Get store configs by store codes
*
* @param array $storeCodes
* @return array
*/
public function getStoreConfigsByStoreCodes(array $storeCodes = null) : array
{
$storeConfigs = $this->storeConfigManager->getStoreConfigs($storeCodes);

return ['items' => $this->hidrateStoreConfigs($storeConfigs)];
}

/**
* Transform StoreConfig objects to in array format
*
* @param StoreConfigInterface[] $storeConfigs
* @return array
*/
private function hidrateStoreConfigs(array $storeConfigs) : array
{
$storeConfigsData = [];
/** @var StoreConfigInterface $storeConfig */
foreach ($storeConfigs as $storeConfig) {
$storeConfigsData[] = [
'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_link_url' => $storeConfig->getSecureBaseLinkUrl(),
'secure_base_static_url' => $storeConfig->getSecureBaseStaticUrl(),
'secure_base_media_url' => $storeConfig->getSecureBaseMediaUrl()
];
}

return $storeConfigsData;
}
}
84 changes: 84 additions & 0 deletions app/code/Magento/StoreGraphQl/Model/Resolver/StoreConfigs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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\Exception\GraphQlInputException;
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\StoreConfigsDataProvider;

/**
* StoreConfig page field resolver, used for GraphQL request processing.
*/
class StoreConfigs implements ResolverInterface
{
/**
* @var StoreConfigsDataProvider
*/
private $storeConfigsDataProvider;

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

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

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

$storeCodes = $this->getStoreCodes($args);
$storeConfigsData = $this->storeConfigsDataProvider->getStoreConfigsByStoreCodes($storeCodes);

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

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

/**
* Retrieve store codes
*
* @param array $args
* @return array
* @throws GraphQlInputException
*/
private function getStoreCodes($args) : array
{
if (isset($args['store_codes'])) {
if (is_array($args['store_codes'])) {
return $args['store_codes'];
}
throw new GraphQlInputException(__('"store codes should contain a valid array'));
}

return null;
}
}
29 changes: 29 additions & 0 deletions app/code/Magento/StoreGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# 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\\StoreConfigs") @doc(description: "The products query searches for products that match the criteria specified in the search and filter attributes")
}

type Website @doc(description: "The type contains information about a website") {
id : Int @doc(description: "The ID number assigned to the website")
Expand All @@ -9,3 +15,26 @@ type Website @doc(description: "The type contains information about a website")
default_group_id : String @doc(description: "The default group id that the website has")
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")
website_id : Int @doc(description: "The ID number assigned to the website store belongs")
locale : String @doc(description: "Store locale")
base_currency_code : String @doc(description: "Base currency code")
default_display_currency_code : String @doc(description: "Default display currency code")
timezone : String @doc(description: "Timezone of the store")
weight_unit : String @doc(description: "The unit of weight")
base_url : String @doc(description: "Base URL for the store")
base_link_url : String @doc(description: "Base link URL for the store")
base_static_url : String @doc(description: "Base static URL for the store")
base_media_url : String @doc(description: "Base media URL for the store")
secure_base_url : String @doc(description: "Secure base URL for the store")
secure_base_link_url : String @doc(description: "Secure base link URL for the store")
secure_base_static_url : String @doc(description: "Secure base static URL for the store")
secure_base_media_url : String @doc(description: "Secure base media URL for the store")
}

0 comments on commit e1f53d4

Please sign in to comment.