Skip to content

Commit e1f53d4

Browse files
author
vitaliyboyko
committed
graphql-ce-120: added store configs schema and resolver
1 parent 1d469f8 commit e1f53d4

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\StoreGraphQl\Model\Resolver\Store;
9+
10+
use Magento\Store\Api\Data\StoreConfigInterface;
11+
use Magento\Store\Api\StoreConfigManagerInterface;
12+
13+
/**
14+
* StoreConfig field data provider, used for GraphQL request processing.
15+
*/
16+
class StoreConfigsDataProvider
17+
{
18+
/**
19+
* @var StoreConfigManagerInterface
20+
*/
21+
private $storeConfigManager;
22+
23+
/**
24+
* @param StoreConfigManagerInterface $storeConfigManager
25+
*/
26+
public function __construct(
27+
StoreConfigManagerInterface $storeConfigManager
28+
) {
29+
$this->storeConfigManager = $storeConfigManager;
30+
}
31+
32+
/**
33+
* Get store configs by store codes
34+
*
35+
* @param array $storeCodes
36+
* @return array
37+
*/
38+
public function getStoreConfigsByStoreCodes(array $storeCodes = null) : array
39+
{
40+
$storeConfigs = $this->storeConfigManager->getStoreConfigs($storeCodes);
41+
42+
return ['items' => $this->hidrateStoreConfigs($storeConfigs)];
43+
}
44+
45+
/**
46+
* Transform StoreConfig objects to in array format
47+
*
48+
* @param StoreConfigInterface[] $storeConfigs
49+
* @return array
50+
*/
51+
private function hidrateStoreConfigs(array $storeConfigs) : array
52+
{
53+
$storeConfigsData = [];
54+
/** @var StoreConfigInterface $storeConfig */
55+
foreach ($storeConfigs as $storeConfig) {
56+
$storeConfigsData[] = [
57+
'id' => $storeConfig->getId(),
58+
'code' => $storeConfig->getCode(),
59+
'website_id' => $storeConfig->getWebsiteId(),
60+
'locale' => $storeConfig->getLocale(),
61+
'base_currency_code' => $storeConfig->getBaseCurrencyCode(),
62+
'default_display_currency_code' => $storeConfig->getDefaultDisplayCurrencyCode(),
63+
'timezone' => $storeConfig->getTimezone(),
64+
'weight_unit' => $storeConfig->getWeightUnit(),
65+
'base_url' => $storeConfig->getBaseUrl(),
66+
'base_link_url' => $storeConfig->getBaseLinkUrl(),
67+
'base_static_url' => $storeConfig->getSecureBaseStaticUrl(),
68+
'base_media_url' => $storeConfig->getBaseMediaUrl(),
69+
'secure_base_link_url' => $storeConfig->getSecureBaseLinkUrl(),
70+
'secure_base_static_url' => $storeConfig->getSecureBaseStaticUrl(),
71+
'secure_base_media_url' => $storeConfig->getSecureBaseMediaUrl()
72+
];
73+
}
74+
75+
return $storeConfigsData;
76+
}
77+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\StoreGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
12+
use Magento\Framework\GraphQl\Query\Resolver\Value;
13+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\StoreGraphQl\Model\Resolver\Store\StoreConfigsDataProvider;
17+
18+
/**
19+
* StoreConfig page field resolver, used for GraphQL request processing.
20+
*/
21+
class StoreConfigs implements ResolverInterface
22+
{
23+
/**
24+
* @var StoreConfigsDataProvider
25+
*/
26+
private $storeConfigsDataProvider;
27+
28+
/**
29+
* @var ValueFactory
30+
*/
31+
private $valueFactory;
32+
33+
/**
34+
* @param StoreConfigsDataProvider $storeConfigsDataProvider
35+
* @param ValueFactory $valueFactory
36+
*/
37+
public function __construct(
38+
StoreConfigsDataProvider $storeConfigsDataProvider,
39+
ValueFactory $valueFactory
40+
) {
41+
$this->valueFactory = $valueFactory;
42+
$this->storeConfigsDataProvider = $storeConfigsDataProvider;
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function resolve(
49+
Field $field,
50+
$context,
51+
ResolveInfo $info,
52+
array $value = null,
53+
array $args = null
54+
) : Value {
55+
56+
$storeCodes = $this->getStoreCodes($args);
57+
$storeConfigsData = $this->storeConfigsDataProvider->getStoreConfigsByStoreCodes($storeCodes);
58+
59+
$result = function () use ($storeConfigsData) {
60+
return !empty($storeConfigsData) ? $storeConfigsData : [];
61+
};
62+
63+
return $this->valueFactory->create($result);
64+
}
65+
66+
/**
67+
* Retrieve store codes
68+
*
69+
* @param array $args
70+
* @return array
71+
* @throws GraphQlInputException
72+
*/
73+
private function getStoreCodes($args) : array
74+
{
75+
if (isset($args['store_codes'])) {
76+
if (is_array($args['store_codes'])) {
77+
return $args['store_codes'];
78+
}
79+
throw new GraphQlInputException(__('"store codes should contain a valid array'));
80+
}
81+
82+
return null;
83+
}
84+
}

app/code/Magento/StoreGraphQl/etc/schema.graphqls

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Copyright © Magento, Inc. All rights reserved.
22
# See COPYING.txt for license details.
3+
type Query {
4+
storeConfigs (
5+
storeCodes: [String] @doc(description: "Store Codes of the store configs")
6+
): StoreConfigs
7+
@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")
8+
}
39

410
type Website @doc(description: "The type contains information about a website") {
511
id : Int @doc(description: "The ID number assigned to the website")
@@ -9,3 +15,26 @@ type Website @doc(description: "The type contains information about a website")
915
default_group_id : String @doc(description: "The default group id that the website has")
1016
is_default : Boolean @doc(description: "Specifies if this is the default website")
1117
}
18+
19+
type StoreConfigs @doc(description: "The Store Configs object") {
20+
items: [StoreConfig] @doc(description: "An array containing store configs")
21+
}
22+
23+
type StoreConfig @doc(description: "The type contains information about a store config") {
24+
id : Int @doc(description: "The ID number assigned to the store")
25+
code : String @doc(description: "A code assigned to the store to identify it")
26+
website_id : Int @doc(description: "The ID number assigned to the website store belongs")
27+
locale : String @doc(description: "Store locale")
28+
base_currency_code : String @doc(description: "Base currency code")
29+
default_display_currency_code : String @doc(description: "Default display currency code")
30+
timezone : String @doc(description: "Timezone of the store")
31+
weight_unit : String @doc(description: "The unit of weight")
32+
base_url : String @doc(description: "Base URL for the store")
33+
base_link_url : String @doc(description: "Base link URL for the store")
34+
base_static_url : String @doc(description: "Base static URL for the store")
35+
base_media_url : String @doc(description: "Base media URL for the store")
36+
secure_base_url : String @doc(description: "Secure base URL for the store")
37+
secure_base_link_url : String @doc(description: "Secure base link URL for the store")
38+
secure_base_static_url : String @doc(description: "Secure base static URL for the store")
39+
secure_base_media_url : String @doc(description: "Secure base media URL for the store")
40+
}

0 commit comments

Comments
 (0)