Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Add queries for directory information #313

Merged
merged 3 commits into from
Feb 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions app/code/Magento/DirectoryGraphQl/Model/Resolver/Countries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\DirectoryGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\Reflection\DataObjectProcessor;
use Magento\Directory\Api\CountryInformationAcquirerInterface;
use Magento\Directory\Api\Data\CountryInformationInterface;

/**
* Countries field resolver, used for GraphQL request processing.
*/
class Countries implements ResolverInterface
{
/**
* @var DataObjectProcessor
*/
private $dataProcessor;

/**
* @var CountryInformationAcquirerInterface
*/
private $countryInformationAcquirer;

/**
* @param DataObjectProcessor $dataProcessor
* @param CountryInformationAcquirerInterface $countryInformationAcquirer
*/
public function __construct(
DataObjectProcessor $dataProcessor,
CountryInformationAcquirerInterface $countryInformationAcquirer
) {
$this->dataProcessor = $dataProcessor;
$this->countryInformationAcquirer = $countryInformationAcquirer;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$countries = $this->countryInformationAcquirer->getCountriesInfo();

$output = [];
foreach ($countries as $country) {
$output[] = $this->dataProcessor->buildOutputDataArray($country, CountryInformationInterface::class);
}

return $output;
}
}
67 changes: 67 additions & 0 deletions app/code/Magento/DirectoryGraphQl/Model/Resolver/Country.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\DirectoryGraphQl\Model\Resolver;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\Reflection\DataObjectProcessor;
use Magento\Directory\Api\CountryInformationAcquirerInterface;
use Magento\Directory\Api\Data\CountryInformationInterface;

/**
* Country field resolver, used for GraphQL request processing.
*/
class Country implements ResolverInterface
{
/**
* @var DataObjectProcessor
*/
private $dataProcessor;

/**
* @var CountryInformationAcquirerInterface
*/
private $countryInformationAcquirer;

/**
* @param DataObjectProcessor $dataProcessor
* @param CountryInformationAcquirerInterface $countryInformationAcquirer
*/
public function __construct(
DataObjectProcessor $dataProcessor,
CountryInformationAcquirerInterface $countryInformationAcquirer
) {
$this->dataProcessor = $dataProcessor;
$this->countryInformationAcquirer = $countryInformationAcquirer;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
try {
$country = $this->countryInformationAcquirer->getCountryInfo($args['id']);
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(__($exception->getMessage()), $exception);
}

return $this->dataProcessor->buildOutputDataArray(
$country,
CountryInformationInterface::class
);
}
}
59 changes: 59 additions & 0 deletions app/code/Magento/DirectoryGraphQl/Model/Resolver/Currency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\DirectoryGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\Reflection\DataObjectProcessor;
use Magento\Directory\Api\CurrencyInformationAcquirerInterface;
use Magento\Directory\Api\Data\CurrencyInformationInterface;

/**
* Currency field resolver, used for GraphQL request processing.
*/
class Currency implements ResolverInterface
{
/**
* @var DataObjectProcessor
*/
private $dataProcessor;

/**
* @var CurrencyInformationAcquirerInterface
*/
private $currencyInformationAcquirer;

/**
* @param DataObjectProcessor $dataProcessor
* @param CurrencyInformationAcquirerInterface $currencyInformationAcquirer
*/
public function __construct(
DataObjectProcessor $dataProcessor,
CurrencyInformationAcquirerInterface $currencyInformationAcquirer
) {
$this->dataProcessor = $dataProcessor;
$this->currencyInformationAcquirer = $currencyInformationAcquirer;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
return $this->dataProcessor->buildOutputDataArray(
$this->currencyInformationAcquirer->getCurrencyInfo(),
CurrencyInformationInterface::class
);
}
}
4 changes: 4 additions & 0 deletions app/code/Magento/DirectoryGraphQl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# DirectoryGraphQl

**DirectoryGraphQl** provides type and resolver information for the GraphQl module
to generate directory information endpoints.
3 changes: 3 additions & 0 deletions app/code/Magento/DirectoryGraphQl/Test/Mftf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Directory Graph Ql Functional Tests

The Functional Test Module for **Magento Directory Graph Ql** module.
25 changes: 25 additions & 0 deletions app/code/Magento/DirectoryGraphQl/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "magento/module-directory-graph-ql",
"description": "N/A",
"type": "magento2-module",
"require": {
"php": "~7.1.3||~7.2.0",
"magento/module-directory": "*",
"magento/framework": "*"
},
"suggest": {
"magento/module-graph-ql": "*"
},
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\\DirectoryGraphQl\\": ""
}
}
}
10 changes: 10 additions & 0 deletions app/code/Magento/DirectoryGraphQl/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_DirectoryGraphQl"/>
</config>
37 changes: 37 additions & 0 deletions app/code/Magento/DirectoryGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.

type Query {
currency: Currency @resolver(class: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Currency") @doc(description: "The currency query returns information about store currency.")
countries: [Country] @resolver(class: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Countries") @doc(description: "The countries query provides information for all countries.")
country (id: String): Country @resolver(class: "Magento\\DirectoryGraphQl\\Model\\Resolver\\Country") @doc(description: "The countries query provides information for a single country.")
}

type Currency {
base_currency_code: String
base_currency_symbol: String
default_display_currecy_code: String
default_display_currecy_symbol: String
available_currency_codes: [String]
exchange_rates: [ExchangeRate]
}

type ExchangeRate {
currency_to: String
rate: Float
}

type Country {
id: String
two_letter_abbreviation: String
three_letter_abbreviation: String
full_name_locale: String
full_name_english: String
available_regions: [Region]
}

type Region {
id: Int
code: String
name: String
}
9 changes: 9 additions & 0 deletions app/code/Magento/DirectoryGraphQl/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_DirectoryGraphQl', __DIR__);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
"magento/module-developer": "*",
"magento/module-dhl": "*",
"magento/module-directory": "*",
"magento/module-directory-graph-ql": "*",
"magento/module-downloadable": "*",
"magento/module-downloadable-graph-ql": "*",
"magento/module-downloadable-import-export": "*",
Expand Down
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Directory;

use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test the GraphQL endpoint's Coutries query
*/
class CountriesTest extends GraphQlAbstract
{
public function testGetCountries()
{
$query = <<<QUERY
query {
countries {
id
two_letter_abbreviation
three_letter_abbreviation
full_name_locale
full_name_english
available_regions {
id
code
name
}
}
}
QUERY;

$result = $this->graphQlQuery($query);
$this->assertArrayHasKey('countries', $result);
$this->assertArrayHasKey('id', $result['countries'][0]);
$this->assertArrayHasKey('two_letter_abbreviation', $result['countries'][0]);
$this->assertArrayHasKey('three_letter_abbreviation', $result['countries'][0]);
$this->assertArrayHasKey('full_name_locale', $result['countries'][0]);
$this->assertArrayHasKey('full_name_english', $result['countries'][0]);
$this->assertArrayHasKey('available_regions', $result['countries'][0]);
}
}
Loading