From e1f53d45065a6fa292e2213b72a2742f81197a0b Mon Sep 17 00:00:00 2001 From: vitaliyboyko Date: Wed, 18 Jul 2018 06:50:46 +0000 Subject: [PATCH] graphql-ce-120: added store configs schema and resolver --- .../Store/StoreConfigsDataProvider.php | 77 +++++++++++++++++ .../Model/Resolver/StoreConfigs.php | 84 +++++++++++++++++++ .../Magento/StoreGraphQl/etc/schema.graphqls | 29 +++++++ 3 files changed, 190 insertions(+) create mode 100644 app/code/Magento/StoreGraphQl/Model/Resolver/Store/StoreConfigsDataProvider.php create mode 100644 app/code/Magento/StoreGraphQl/Model/Resolver/StoreConfigs.php diff --git a/app/code/Magento/StoreGraphQl/Model/Resolver/Store/StoreConfigsDataProvider.php b/app/code/Magento/StoreGraphQl/Model/Resolver/Store/StoreConfigsDataProvider.php new file mode 100644 index 0000000000000..4fb7dc9cdbb0e --- /dev/null +++ b/app/code/Magento/StoreGraphQl/Model/Resolver/Store/StoreConfigsDataProvider.php @@ -0,0 +1,77 @@ +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; + } +} diff --git a/app/code/Magento/StoreGraphQl/Model/Resolver/StoreConfigs.php b/app/code/Magento/StoreGraphQl/Model/Resolver/StoreConfigs.php new file mode 100644 index 0000000000000..e03ab72167f1e --- /dev/null +++ b/app/code/Magento/StoreGraphQl/Model/Resolver/StoreConfigs.php @@ -0,0 +1,84 @@ +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; + } +} diff --git a/app/code/Magento/StoreGraphQl/etc/schema.graphqls b/app/code/Magento/StoreGraphQl/etc/schema.graphqls index 6eea6da8fd6fa..67515a01fac93 100644 --- a/app/code/Magento/StoreGraphQl/etc/schema.graphqls +++ b/app/code/Magento/StoreGraphQl/etc/schema.graphqls @@ -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") @@ -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") +}