Skip to content

Commit 6f54e40

Browse files
ENGCOM-5913: GraphQL-893 improved strict typing in SelectedShippingMethod #900
- Merge Pull Request magento/graphql-ce#900 from sergiy-v/graphql-ce:893-improve-strict-typing-in-SelectedShippingMethod - Merged commits: 1. 830e0c1 2. 945a1a7 3. 9e6adc8 4. 891146c
2 parents 97a2027 + 891146c commit 6f54e40

File tree

3 files changed

+33
-37
lines changed

3 files changed

+33
-37
lines changed

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

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,37 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
3131
/** @var Address $address */
3232
$address = $value['model'];
3333
$rates = $address->getAllShippingRates();
34-
$carrierTitle = null;
35-
$methodTitle = null;
34+
$carrierTitle = '';
35+
$methodTitle = '';
3636

37-
if (count($rates) > 0 && !empty($address->getShippingMethod())) {
38-
list($carrierCode, $methodCode) = explode('_', $address->getShippingMethod(), 2);
37+
if (!count($rates) || empty($address->getShippingMethod())) {
38+
return null;
39+
}
3940

40-
/** @var Rate $rate */
41-
foreach ($rates as $rate) {
42-
if ($rate->getCode() == $address->getShippingMethod()) {
43-
$carrierTitle = $rate->getCarrierTitle();
44-
$methodTitle = $rate->getMethodTitle();
45-
break;
46-
}
47-
}
41+
list($carrierCode, $methodCode) = explode('_', $address->getShippingMethod(), 2);
4842

49-
$data = [
50-
'carrier_code' => $carrierCode,
51-
'method_code' => $methodCode,
52-
'carrier_title' => $carrierTitle,
53-
'method_title' => $methodTitle,
54-
'amount' => [
55-
'value' => $address->getShippingAmount(),
56-
'currency' => $address->getQuote()->getQuoteCurrencyCode(),
57-
],
58-
/** @deprecated The field should not be used on the storefront */
59-
'base_amount' => null,
60-
];
61-
} else {
62-
$data = null;
43+
/** @var Rate $rate */
44+
foreach ($rates as $rate) {
45+
if ($rate->getCode() == $address->getShippingMethod()) {
46+
$carrierTitle = $rate->getCarrierTitle();
47+
$methodTitle = $rate->getMethodTitle();
48+
break;
49+
}
6350
}
51+
52+
$data = [
53+
'carrier_code' => $carrierCode,
54+
'method_code' => $methodCode,
55+
'carrier_title' => $carrierTitle,
56+
'method_title' => $methodTitle,
57+
'amount' => [
58+
'value' => $address->getShippingAmount(),
59+
'currency' => $address->getQuote()->getQuoteCurrencyCode(),
60+
],
61+
/** @deprecated The field should not be used on the storefront */
62+
'base_amount' => null,
63+
];
64+
6465
return $data;
6566
}
6667
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,11 @@ type CartAddressCountry {
242242
}
243243

244244
type SelectedShippingMethod {
245-
carrier_code: String
246-
method_code: String
247-
carrier_title: String
248-
method_title: String
249-
amount: Money
245+
carrier_code: String!
246+
method_code: String!
247+
carrier_title: String!
248+
method_title: String!
249+
amount: Money!
250250
base_amount: Money @deprecated(reason: "The field should not be used on the storefront")
251251
}
252252

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,7 @@ public function testGetGetSelectedShippingMethodIfShippingMethodIsNotSet()
133133

134134
$shippingAddress = current($response['cart']['shipping_addresses']);
135135
self::assertArrayHasKey('selected_shipping_method', $shippingAddress);
136-
137-
self::assertNull($shippingAddress['selected_shipping_method']['carrier_code']);
138-
self::assertNull($shippingAddress['selected_shipping_method']['method_code']);
139-
self::assertNull($shippingAddress['selected_shipping_method']['carrier_title']);
140-
self::assertNull($shippingAddress['selected_shipping_method']['method_title']);
141-
self::assertNull($shippingAddress['selected_shipping_method']['amount']);
136+
self::assertNull($shippingAddress['selected_shipping_method']);
142137
}
143138

144139
/**

0 commit comments

Comments
 (0)