Skip to content

Commit a35e817

Browse files
ENGCOM-4921: GraphQL-630: Schema inconsistency of 'AvailableShippingMethod' declar… #645
- Merge Pull Request magento/graphql-ce#645 from magento/graphql-ce:graphql-630 - Merged commits: 1. c28268e 2. a253222 3. ea71848
2 parents 93a8162 + ea71848 commit a35e817

File tree

8 files changed

+143
-28
lines changed

8 files changed

+143
-28
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddress/AvailableShippingMethods.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77

88
namespace Magento\QuoteGraphQl\Model\Resolver\ShippingAddress;
99

10+
use Magento\Directory\Model\Currency;
1011
use Magento\Framework\Api\ExtensibleDataObjectConverter;
1112
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
1214
use Magento\Framework\GraphQl\Config\Element\Field;
1315
use Magento\Framework\GraphQl\Query\ResolverInterface;
1416
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1517
use Magento\Quote\Api\Data\ShippingMethodInterface;
1618
use Magento\Quote\Model\Cart\ShippingMethodConverter;
19+
use Magento\Store\Model\StoreManagerInterface;
1720

1821
/**
1922
* @inheritdoc
@@ -30,16 +33,24 @@ class AvailableShippingMethods implements ResolverInterface
3033
*/
3134
private $shippingMethodConverter;
3235

36+
/**
37+
* @var StoreManagerInterface
38+
*/
39+
private $storeManager;
40+
3341
/**
3442
* @param ExtensibleDataObjectConverter $dataObjectConverter
3543
* @param ShippingMethodConverter $shippingMethodConverter
44+
* @param StoreManagerInterface $storeManager
3645
*/
3746
public function __construct(
3847
ExtensibleDataObjectConverter $dataObjectConverter,
39-
ShippingMethodConverter $shippingMethodConverter
48+
ShippingMethodConverter $shippingMethodConverter,
49+
StoreManagerInterface $storeManager
4050
) {
4151
$this->dataObjectConverter = $dataObjectConverter;
4252
$this->shippingMethodConverter = $shippingMethodConverter;
53+
$this->storeManager = $storeManager;
4354
}
4455

4556
/**
@@ -65,13 +76,44 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6576
$shippingRates = $address->getGroupedAllShippingRates();
6677
foreach ($shippingRates as $carrierRates) {
6778
foreach ($carrierRates as $rate) {
68-
$methods[] = $this->dataObjectConverter->toFlatArray(
79+
$methodData = $this->dataObjectConverter->toFlatArray(
6980
$this->shippingMethodConverter->modelToDataObject($rate, $cart->getQuoteCurrencyCode()),
7081
[],
7182
ShippingMethodInterface::class
7283
);
84+
$methods[] = $this->processMoneyTypeData($methodData, $cart->getQuoteCurrencyCode());
7385
}
7486
}
7587
return $methods;
7688
}
89+
90+
/**
91+
* Process money type data
92+
*
93+
* @param array $data
94+
* @param string $quoteCurrencyCode
95+
* @return array
96+
* @throws NoSuchEntityException
97+
*/
98+
private function processMoneyTypeData(array $data, string $quoteCurrencyCode): array
99+
{
100+
if (isset($data['amount'])) {
101+
$data['amount'] = ['value' => $data['amount'], 'currency' => $quoteCurrencyCode];
102+
}
103+
104+
if (isset($data['base_amount'])) {
105+
/** @var Currency $currency */
106+
$currency = $this->storeManager->getStore()->getBaseCurrency();
107+
$data['base_amount'] = ['value' => $data['base_amount'], 'currency' => $currency->getCode()];
108+
}
109+
110+
if (isset($data['price_excl_tax'])) {
111+
$data['price_excl_tax'] = ['value' => $data['price_excl_tax'], 'currency' => $quoteCurrencyCode];
112+
}
113+
114+
if (isset($data['price_incl_tax'])) {
115+
$data['price_incl_tax'] = ['value' => $data['price_incl_tax'], 'currency' => $quoteCurrencyCode];
116+
}
117+
return $data;
118+
}
77119
}

app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddress/SelectedShippingMethod.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,34 @@
77

88
namespace Magento\QuoteGraphQl\Model\Resolver\ShippingAddress;
99

10+
use Magento\Directory\Model\Currency;
1011
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\Framework\GraphQl\Config\Element\Field;
1213
use Magento\Framework\GraphQl\Query\ResolverInterface;
1314
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1415
use Magento\Quote\Model\Quote\Address;
1516
use Magento\Quote\Model\Quote\Address\Rate;
17+
use Magento\Store\Model\StoreManagerInterface;
1618

1719
/**
1820
* @inheritdoc
1921
*/
2022
class SelectedShippingMethod implements ResolverInterface
2123
{
24+
/**
25+
* @var StoreManagerInterface
26+
*/
27+
private $storeManager;
28+
29+
/**
30+
* @param StoreManagerInterface $storeManager
31+
*/
32+
public function __construct(
33+
StoreManagerInterface $storeManager
34+
) {
35+
$this->storeManager = $storeManager;
36+
}
37+
2238
/**
2339
* @inheritdoc
2440
*/
@@ -35,6 +51,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
3551
list($carrierCode, $methodCode) = explode('_', $address->getShippingMethod(), 2);
3652
/** @var Rate $rate */
3753
$rate = current($rates);
54+
/** @var Currency $currency */
55+
$currency = $this->storeManager->getStore()->getBaseCurrency();
3856

3957
$data = [
4058
'carrier_code' => $carrierCode,
@@ -47,7 +65,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
4765
],
4866
'base_amount' => [
4967
'value' => $address->getBaseShippingAmount(),
50-
'currency' => $address->getQuote()->getBaseCurrencyCode(),
68+
'currency' => $currency->getCode(),
5169
],
5270
];
5371
} else {

app/code/Magento/QuoteGraphQl/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"magento/module-store": "*",
1212
"magento/module-customer": "*",
1313
"magento/module-customer-graph-ql": "*",
14-
"magento/module-sales": "*"
14+
"magento/module-sales": "*",
15+
"magento/module-directory": "*"
1516
},
1617
"suggest": {
1718
"magento/module-graph-ql": "*",

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,10 @@ type AvailableShippingMethod {
243243
method_code: String @doc(description: "Could be null if method is not available")
244244
method_title: String @doc(description: "Could be null if method is not available")
245245
error_message: String
246-
amount: Float!
247-
base_amount: Float @doc(description: "Could be null if method is not available")
248-
price_excl_tax: Float!
249-
price_incl_tax: Float!
246+
amount: Money!
247+
base_amount: Money @doc(description: "Could be null if method is not available")
248+
price_excl_tax: Money!
249+
price_incl_tax: Money!
250250
available: Boolean!
251251
}
252252

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/CheckoutEndToEndTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ private function setShippingAddress(string $cartId): array
309309
available_shipping_methods {
310310
carrier_code
311311
method_code
312-
amount
312+
amount {
313+
value
314+
}
313315
}
314316
}
315317
}
@@ -334,7 +336,8 @@ private function setShippingAddress(string $cartId): array
334336
self::assertNotEmpty($availableShippingMethod['method_code']);
335337

336338
self::assertArrayHasKey('amount', $availableShippingMethod);
337-
self::assertNotEmpty($availableShippingMethod['amount']);
339+
self::assertArrayHasKey('value', $availableShippingMethod['amount']);
340+
self::assertNotEmpty($availableShippingMethod['amount']['value']);
338341

339342
return $availableShippingMethod;
340343
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetAvailableShippingMethodsTest.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,27 @@ public function testGetAvailableShippingMethods()
5858
self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']);
5959

6060
$expectedAddressData = [
61-
'amount' => 10,
62-
'base_amount' => 10,
61+
'amount' => [
62+
'value' => 10,
63+
'currency' => 'USD',
64+
],
65+
'base_amount' => [
66+
'value' => 10,
67+
'currency' => 'USD',
68+
],
6369
'carrier_code' => 'flatrate',
6470
'carrier_title' => 'Flat Rate',
6571
'error_message' => '',
6672
'method_code' => 'flatrate',
6773
'method_title' => 'Fixed',
68-
'price_incl_tax' => 10,
69-
'price_excl_tax' => 10,
74+
'price_incl_tax' => [
75+
'value' => 10,
76+
'currency' => 'USD',
77+
],
78+
'price_excl_tax' => [
79+
'value' => 10,
80+
'currency' => 'USD',
81+
],
7082
];
7183
self::assertEquals(
7284
$expectedAddressData,
@@ -158,15 +170,27 @@ private function getQuery(string $maskedQuoteId): string
158170
cart (cart_id: "{$maskedQuoteId}") {
159171
shipping_addresses {
160172
available_shipping_methods {
161-
amount
162-
base_amount
173+
amount {
174+
value
175+
currency
176+
}
177+
base_amount {
178+
value
179+
currency
180+
}
163181
carrier_code
164182
carrier_title
165183
error_message
166184
method_code
167185
method_title
168-
price_excl_tax
169-
price_incl_tax
186+
price_excl_tax {
187+
value
188+
currency
189+
}
190+
price_incl_tax {
191+
value
192+
currency
193+
}
170194
}
171195
}
172196
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CheckoutEndToEndTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ private function setShippingAddress(string $cartId): array
269269
available_shipping_methods {
270270
carrier_code
271271
method_code
272-
amount
272+
amount {
273+
value
274+
}
273275
}
274276
}
275277
}
@@ -294,7 +296,8 @@ private function setShippingAddress(string $cartId): array
294296
self::assertNotEmpty($availableShippingMethod['method_code']);
295297

296298
self::assertArrayHasKey('amount', $availableShippingMethod);
297-
self::assertNotEmpty($availableShippingMethod['amount']);
299+
self::assertArrayHasKey('value', $availableShippingMethod['amount']);
300+
self::assertNotEmpty($availableShippingMethod['amount']['value']);
298301

299302
return $availableShippingMethod;
300303
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetAvailableShippingMethodsTest.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,27 @@ public function testGetAvailableShippingMethods()
5050
self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']);
5151

5252
$expectedAddressData = [
53-
'amount' => 10,
54-
'base_amount' => 10,
53+
'amount' => [
54+
'value' => 10,
55+
'currency' => 'USD',
56+
],
57+
'base_amount' => [
58+
'value' => 10,
59+
'currency' => 'USD',
60+
],
5561
'carrier_code' => 'flatrate',
5662
'carrier_title' => 'Flat Rate',
5763
'error_message' => '',
5864
'method_code' => 'flatrate',
5965
'method_title' => 'Fixed',
60-
'price_incl_tax' => 10,
61-
'price_excl_tax' => 10,
66+
'price_incl_tax' => [
67+
'value' => 10,
68+
'currency' => 'USD',
69+
],
70+
'price_excl_tax' => [
71+
'value' => 10,
72+
'currency' => 'USD',
73+
],
6274
];
6375
self::assertEquals(
6476
$expectedAddressData,
@@ -126,15 +138,27 @@ private function getQuery(string $maskedQuoteId): string
126138
cart (cart_id: "{$maskedQuoteId}") {
127139
shipping_addresses {
128140
available_shipping_methods {
129-
amount
130-
base_amount
141+
amount {
142+
value
143+
currency
144+
}
145+
base_amount {
146+
value
147+
currency
148+
}
131149
carrier_code
132150
carrier_title
133151
error_message
134152
method_code
135153
method_title
136-
price_excl_tax
137-
price_incl_tax
154+
price_excl_tax {
155+
value
156+
currency
157+
}
158+
price_incl_tax {
159+
value
160+
currency
161+
}
138162
}
139163
}
140164
}

0 commit comments

Comments
 (0)