diff --git a/app/code/Magento/StoreGraphQl/Model/Resolver/Store/StoreConfigDataProvider.php b/app/code/Magento/StoreGraphQl/Model/Resolver/Store/StoreConfigDataProvider.php new file mode 100644 index 0000000000000..0252d7898beee --- /dev/null +++ b/app/code/Magento/StoreGraphQl/Model/Resolver/Store/StoreConfigDataProvider.php @@ -0,0 +1,94 @@ +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; + } +} diff --git a/app/code/Magento/StoreGraphQl/Model/Resolver/Store/StoreConfigsDataProvider.php b/app/code/Magento/StoreGraphQl/Model/Resolver/Store/StoreConfigsDataProvider.php deleted file mode 100644 index 0d7ce3aa1555f..0000000000000 --- a/app/code/Magento/StoreGraphQl/Model/Resolver/Store/StoreConfigsDataProvider.php +++ /dev/null @@ -1,80 +0,0 @@ -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_url' => $storeConfig->getSecureBaseUrl(), - 'secure_base_link_url' => $storeConfig->getSecureBaseLinkUrl(), - 'secure_base_static_url' => $storeConfig->getSecureBaseStaticUrl(), - 'secure_base_media_url' => $storeConfig->getSecureBaseMediaUrl() - ]; - } - - return $storeConfigsData; - } -} diff --git a/app/code/Magento/StoreGraphQl/Model/Resolver/StoreConfigResolver.php b/app/code/Magento/StoreGraphQl/Model/Resolver/StoreConfigResolver.php new file mode 100644 index 0000000000000..8e5bf0120b8b8 --- /dev/null +++ b/app/code/Magento/StoreGraphQl/Model/Resolver/StoreConfigResolver.php @@ -0,0 +1,63 @@ +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); + } +} diff --git a/app/code/Magento/StoreGraphQl/Model/Resolver/StoreConfigsResolver.php b/app/code/Magento/StoreGraphQl/Model/Resolver/StoreConfigsResolver.php deleted file mode 100644 index 609985b48e2be..0000000000000 --- a/app/code/Magento/StoreGraphQl/Model/Resolver/StoreConfigsResolver.php +++ /dev/null @@ -1,84 +0,0 @@ -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) - { - if (isset($args['storeCodes'])) { - if (is_array($args['storeCodes'])) { - return $args['storeCodes']; - } - throw new GraphQlInputException(__('"store codes should contain a valid array')); - } - - return null; - } -} diff --git a/app/code/Magento/StoreGraphQl/etc/schema.graphqls b/app/code/Magento/StoreGraphQl/etc/schema.graphqls index d0597de04b668..af79d0e3e28b7 100644 --- a/app/code/Magento/StoreGraphQl/etc/schema.graphqls +++ b/app/code/Magento/StoreGraphQl/etc/schema.graphqls @@ -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") { @@ -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") diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Store/StoreConfigsResolverTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Store/StoreConfigResolverTest.php similarity index 58% rename from dev/tests/api-functional/testsuite/Magento/GraphQl/Store/StoreConfigsResolverTest.php rename to dev/tests/api-functional/testsuite/Magento/GraphQl/Store/StoreConfigResolverTest.php index 5c3566ffe2e19..df86519ae3d1e 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Store/StoreConfigsResolverTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Store/StoreConfigResolverTest.php @@ -9,13 +9,15 @@ use Magento\Store\Api\Data\StoreConfigInterface; use Magento\Store\Api\StoreConfigManagerInterface; +use Magento\Store\Api\StoreRepositoryInterface; +use Magento\Store\Api\StoreResolverInterface; use Magento\TestFramework\ObjectManager; use Magento\TestFramework\TestCase\GraphQlAbstract; /** * Test the GraphQL endpoint's StoreConfigs query */ -class StoreConfigsResolverTest extends GraphQlAbstract +class StoreConfigResolverTest extends GraphQlAbstract { /** @var ObjectManager */ @@ -29,17 +31,22 @@ protected function setUp() /** * @magentoApiDataFixture Magento/Store/_files/store.php */ - public function testStoreConfigsFilteredByStoreCode() + public function testGetStoreConfig() { - $storeCode = 'test'; /** @var StoreConfigManagerInterface $storeConfigsManager */ $storeConfigsManager = $this->objectManager->get(StoreConfigManagerInterface::class); + /** @var StoreResolverInterface $storeResolver */ + $storeResolver = $this->objectManager->get(StoreResolverInterface::class); + /** @var StoreRepositoryInterface $storeRepository */ + $storeRepository = $this->objectManager->get(StoreRepositoryInterface::class); + $storeId = $storeResolver->getCurrentStoreId(); + $store = $storeRepository->getById($storeId); /** @var StoreConfigInterface $storeConfig */ - $storeConfig = current($storeConfigsManager->getStoreConfigs([$storeCode])); + $storeConfig = current($storeConfigsManager->getStoreConfigs([$store->getCode()])); $query = <<graphQlQuery($query); - $this->assertArrayHasKey('storeConfigs', $response); - $this->assertEquals(1, count($response['storeConfigs']['items'])); - $responseStoreConfig = current($response['storeConfigs']['items']); - $this->assertEquals($storeConfig->getId(), $responseStoreConfig['id']); - $this->assertEquals($storeConfig->getCode(), $responseStoreConfig['code']); - $this->assertEquals($storeConfig->getLocale(), $responseStoreConfig['locale']); - $this->assertEquals($storeConfig->getBaseCurrencyCode(), $responseStoreConfig['base_currency_code']); + $this->assertArrayHasKey('storeConfig', $response); + $this->assertEquals($storeConfig->getId(), $response['storeConfig']['id']); + $this->assertEquals($storeConfig->getCode(), $response['storeConfig']['code']); + $this->assertEquals($storeConfig->getLocale(), $response['storeConfig']['locale']); + $this->assertEquals($storeConfig->getBaseCurrencyCode(), $response['storeConfig']['base_currency_code']); $this->assertEquals( $storeConfig->getDefaultDisplayCurrencyCode(), - $responseStoreConfig['default_display_currency_code'] + $response['storeConfig']['default_display_currency_code'] ); - $this->assertEquals($storeConfig->getTimezone(), $responseStoreConfig['timezone']); - $this->assertEquals($storeConfig->getWeightUnit(), $responseStoreConfig['weight_unit']); - $this->assertEquals($storeConfig->getBaseUrl(), $responseStoreConfig['base_url']); - $this->assertEquals($storeConfig->getBaseLinkUrl(), $responseStoreConfig['base_link_url']); - $this->assertEquals($storeConfig->getBaseStaticUrl(), $responseStoreConfig['base_static_url']); - $this->assertEquals($storeConfig->getBaseMediaUrl(), $responseStoreConfig['base_media_url']); - $this->assertEquals($storeConfig->getSecureBaseUrl(), $responseStoreConfig['secure_base_url']); - $this->assertEquals($storeConfig->getSecureBaseLinkUrl(), $responseStoreConfig['secure_base_link_url']); - $this->assertEquals($storeConfig->getSecureBaseStaticUrl(), $responseStoreConfig['secure_base_static_url']); - $this->assertEquals($storeConfig->getSecureBaseMediaUrl(), $responseStoreConfig['secure_base_media_url']); - } - - /** - * @magentoApiDataFixture Magento/Store/_files/store.php - */ - public function testGetStoreConfigsWithoutStoreCodes() - { - $query - = <<graphQlQuery($query); - $this->assertArrayHasKey('storeConfigs', $response); - $this->assertEquals(2, count($response['storeConfigs']['items'])); + $this->assertEquals($storeConfig->getTimezone(), $response['storeConfig']['timezone']); + $this->assertEquals($storeConfig->getWeightUnit(), $response['storeConfig']['weight_unit']); + $this->assertEquals($storeConfig->getBaseUrl(), $response['storeConfig']['base_url']); + $this->assertEquals($storeConfig->getBaseLinkUrl(), $response['storeConfig']['base_link_url']); + $this->assertEquals($storeConfig->getBaseStaticUrl(), $response['storeConfig']['base_static_url']); + $this->assertEquals($storeConfig->getBaseMediaUrl(), $response['storeConfig']['base_media_url']); + $this->assertEquals($storeConfig->getSecureBaseUrl(), $response['storeConfig']['secure_base_url']); + $this->assertEquals($storeConfig->getSecureBaseLinkUrl(), $response['storeConfig']['secure_base_link_url']); + $this->assertEquals( + $storeConfig->getSecureBaseStaticUrl(), + $response['storeConfig']['secure_base_static_url'] + ); + $this->assertEquals($storeConfig->getSecureBaseMediaUrl(), $response['storeConfig']['secure_base_media_url']); } }