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/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/composer.json b/app/code/Magento/StoreGraphQl/composer.json index 91f79b39c023a..d03d759babd22 100644 --- a/app/code/Magento/StoreGraphQl/composer.json +++ b/app/code/Magento/StoreGraphQl/composer.json @@ -4,7 +4,8 @@ "type": "magento2-module", "require": { "php": "~7.1.3||~7.2.0", - "magento/framework": "*" + "magento/framework": "*", + "magento/module-store": "*" }, "suggest": { "magento/module-graph-ql": "*", diff --git a/app/code/Magento/StoreGraphQl/etc/schema.graphqls b/app/code/Magento/StoreGraphQl/etc/schema.graphqls index 6eea6da8fd6fa..af79d0e3e28b7 100644 --- a/app/code/Magento/StoreGraphQl/etc/schema.graphqls +++ b/app/code/Magento/StoreGraphQl/etc/schema.graphqls @@ -1,5 +1,8 @@ # Copyright © Magento, Inc. All rights reserved. # See COPYING.txt for license details. +type 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") { id : Int @doc(description: "The ID number assigned to the website") @@ -9,3 +12,22 @@ 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 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") +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Store/StoreConfigResolverTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Store/StoreConfigResolverTest.php new file mode 100644 index 0000000000000..4657a1e763ae1 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Store/StoreConfigResolverTest.php @@ -0,0 +1,93 @@ +objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + } + + /** + * @magentoApiDataFixture Magento/Store/_files/store.php + */ + public function testGetStoreConfig() + { + /** @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([$store->getCode()])); + $query + = <<graphQlQuery($query); + $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(), + $response['storeConfig']['default_display_currency_code'] + ); + $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']); + } +}