Skip to content

Commit 274bb5e

Browse files
ENGCOM-4853: GraphQl-621: BillingAddress and ShippingAddress should have different fields but share the common interface #637
- Merge Pull Request magento/graphql-ce#637 from magento/graphql-ce:graphql-621 - Merged commits: 1. 604e223
2 parents 3ccf676 + 604e223 commit 274bb5e

File tree

11 files changed

+74
-43
lines changed

11 files changed

+74
-43
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/ExtractQuoteAddressData.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,7 @@ public function execute(QuoteAddress $address): array
4141
$addressData = $this->dataObjectConverter->toFlatArray($address, [], AddressInterface::class);
4242
$addressData['model'] = $address;
4343

44-
if ($address->getAddressType() == AbstractAddress::TYPE_SHIPPING) {
45-
$addressType = 'SHIPPING';
46-
} elseif ($address->getAddressType() == AbstractAddress::TYPE_BILLING) {
47-
$addressType = 'BILLING';
48-
} else {
49-
$addressType = null;
50-
}
51-
5244
$addressData = array_merge($addressData, [
53-
'address_type' => $addressType,
5445
'country' => [
5546
'code' => $address->getCountryId(),
5647
'label' => $address->getCountry()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Resolver;
9+
10+
use Magento\Customer\Model\Address\AbstractAddress;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;
13+
use Magento\Quote\Model\Quote\Address;
14+
15+
/**
16+
* @inheritdoc
17+
*/
18+
class CartAddressTypeResolver implements TypeResolverInterface
19+
{
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function resolveType(array $data) : string
24+
{
25+
if (!isset($data['model'])) {
26+
throw new LocalizedException(__('Missing key "model" in cart address data'));
27+
}
28+
/** @var Address $address */
29+
$address = $data['model'];
30+
31+
if ($address->getAddressType() == AbstractAddress::TYPE_SHIPPING) {
32+
$addressType = 'ShippingCartAddress';
33+
} elseif ($address->getAddressType() == AbstractAddress::TYPE_BILLING) {
34+
$addressType = 'BillingCartAddress';
35+
} else {
36+
throw new LocalizedException( __('Unsupported cart address type'));
37+
}
38+
return $addressType;
39+
}
40+
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,14 @@ type Cart {
183183
items: [CartItemInterface] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartItems")
184184
applied_coupon: AppliedCoupon @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\AppliedCoupon")
185185
email: String @resolver (class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartEmail")
186-
shipping_addresses: [CartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddresses")
187-
billing_address: CartAddress! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress")
186+
shipping_addresses: [ShippingCartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddresses")
187+
billing_address: BillingCartAddress! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress")
188188
available_payment_methods: [AvailablePaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods")
189189
selected_payment_method: SelectedPaymentMethod @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SelectedPaymentMethod")
190190
prices: CartPrices @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartPrices")
191191
}
192192

193-
type CartAddress {
193+
interface CartAddressInterface @typeResolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartAddressTypeResolver") {
194194
firstname: String
195195
lastname: String
196196
company: String
@@ -200,14 +200,19 @@ type CartAddress {
200200
postcode: String
201201
country: CartAddressCountry
202202
telephone: String
203-
address_type: AdressTypeEnum
203+
customer_notes: String
204+
}
205+
206+
type ShippingCartAddress implements CartAddressInterface {
204207
available_shipping_methods: [AvailableShippingMethod] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddress\\AvailableShippingMethods")
205208
selected_shipping_method: SelectedShippingMethod @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddress\\SelectedShippingMethod")
206209
items_weight: Float
207-
customer_notes: String
208210
cart_items: [CartItemQuantity]
209211
}
210212

213+
type BillingCartAddress implements CartAddressInterface {
214+
}
215+
211216
type CartItemQuantity {
212217
cart_item_id: Int!
213218
quantity: Float!
@@ -257,11 +262,6 @@ type SelectedPaymentMethod {
257262
type SelectedPaymentMethodAdditionalData {
258263
}
259264

260-
enum AdressTypeEnum {
261-
SHIPPING
262-
BILLING
263-
}
264-
265265
type AppliedCoupon {
266266
code: String!
267267
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ private function setBillingAddress(string $cartId): void
266266
) {
267267
cart {
268268
billing_address {
269-
address_type
269+
__typename
270270
}
271271
}
272272
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function testGeSpecifiedBillingAddress()
7171
'label' => 'US',
7272
],
7373
'telephone' => '3468676',
74-
'address_type' => 'BILLING',
74+
'__typename' => 'BillingCartAddress',
7575
'customer_notes' => null,
7676
];
7777
self::assertEquals($expectedBillingAddressData, $response['cart']['billing_address']);
@@ -110,7 +110,7 @@ public function testGeSpecifiedBillingAddressIfBillingAddressIsNotSet()
110110
'label' => null,
111111
],
112112
'telephone' => null,
113-
'address_type' => 'BILLING',
113+
'__typename' => null,
114114
'customer_notes' => null,
115115
];
116116
self::assertEquals($expectedBillingAddressData, $response['cart']['billing_address']);
@@ -197,7 +197,7 @@ private function getQuery(string $maskedQuoteId): string
197197
label
198198
}
199199
telephone
200-
address_type
200+
__typename
201201
customer_notes
202202
}
203203
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function testSetNewBillingAddress()
9999
code
100100
label
101101
}
102-
address_type
102+
__typename
103103
}
104104
}
105105
}
@@ -159,7 +159,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter()
159159
code
160160
label
161161
}
162-
address_type
162+
__typename
163163
}
164164
shipping_addresses {
165165
firstname
@@ -173,7 +173,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter()
173173
code
174174
label
175175
}
176-
address_type
176+
__typename
177177
}
178178
}
179179
}
@@ -188,7 +188,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter()
188188
self::assertArrayHasKey('shipping_addresses', $cartResponse);
189189
$shippingAddressResponse = current($cartResponse['shipping_addresses']);
190190
$this->assertNewAddressFields($billingAddressResponse);
191-
$this->assertNewAddressFields($shippingAddressResponse, 'SHIPPING');
191+
$this->assertNewAddressFields($shippingAddressResponse, 'ShippingCartAddress');
192192
}
193193

194194
/**
@@ -560,7 +560,7 @@ public function testSetNewBillingAddressWithRedundantStreetLine()
560560
* @param array $addressResponse
561561
* @param string $addressType
562562
*/
563-
private function assertNewAddressFields(array $addressResponse, string $addressType = 'BILLING'): void
563+
private function assertNewAddressFields(array $addressResponse, string $addressType = 'BillingCartAddress'): void
564564
{
565565
$assertionMap = [
566566
['response_field' => 'firstname', 'expected_value' => 'test firstname'],
@@ -571,7 +571,7 @@ private function assertNewAddressFields(array $addressResponse, string $addressT
571571
['response_field' => 'postcode', 'expected_value' => '887766'],
572572
['response_field' => 'telephone', 'expected_value' => '88776655'],
573573
['response_field' => 'country', 'expected_value' => ['code' => 'US', 'label' => 'US']],
574-
['response_field' => 'address_type', 'expected_value' => $addressType]
574+
['response_field' => '__typename', 'expected_value' => $addressType]
575575
];
576576

577577
$this->assertResponseFields($addressResponse, $assertionMap);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function testSetNewShippingAddressOnCartWithSimpleProduct()
101101
label
102102
code
103103
}
104-
address_type
104+
__typename
105105
}
106106
}
107107
}
@@ -548,7 +548,7 @@ private function assertNewShippingAddressFields(array $shippingAddressResponse):
548548
['response_field' => 'postcode', 'expected_value' => '887766'],
549549
['response_field' => 'telephone', 'expected_value' => '88776655'],
550550
['response_field' => 'country', 'expected_value' => ['code' => 'US', 'label' => 'US']],
551-
['response_field' => 'address_type', 'expected_value' => 'SHIPPING']
551+
['response_field' => '__typename', 'expected_value' => 'ShippingCartAddress']
552552
];
553553

554554
$this->assertResponseFields($shippingAddressResponse, $assertionMap);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ private function setBillingAddress(string $cartId): void
226226
) {
227227
cart {
228228
billing_address {
229-
address_type
229+
__typename
230230
}
231231
}
232232
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testGeSpecifiedBillingAddress()
6363
'label' => 'US',
6464
],
6565
'telephone' => '3468676',
66-
'address_type' => 'BILLING',
66+
'__typename' => 'BillingCartAddress',
6767
];
6868
self::assertEquals($expectedBillingAddressData, $response['cart']['billing_address']);
6969
}
@@ -100,7 +100,7 @@ public function testGeSpecifiedBillingAddressIfBillingAddressIsNotSet()
100100
'label' => null,
101101
],
102102
'telephone' => null,
103-
'address_type' => 'BILLING',
103+
'__typename' => 'BillingCartAddress',
104104
];
105105
self::assertEquals($expectedBillingAddressData, $response['cart']['billing_address']);
106106
}
@@ -161,7 +161,7 @@ private function getQuery(string $maskedQuoteId): string
161161
label
162162
}
163163
telephone
164-
address_type
164+
__typename
165165
}
166166
}
167167
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function testSetNewBillingAddress()
7070
code
7171
label
7272
}
73-
address_type
73+
__typename
7474
}
7575
}
7676
}
@@ -129,7 +129,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter()
129129
code
130130
label
131131
}
132-
address_type
132+
__typename
133133
}
134134
shipping_addresses {
135135
firstname
@@ -143,7 +143,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter()
143143
code
144144
label
145145
}
146-
address_type
146+
__typename
147147
}
148148
}
149149
}
@@ -158,7 +158,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter()
158158
self::assertArrayHasKey('shipping_addresses', $cartResponse);
159159
$shippingAddressResponse = current($cartResponse['shipping_addresses']);
160160
$this->assertNewAddressFields($billingAddressResponse);
161-
$this->assertNewAddressFields($shippingAddressResponse, 'SHIPPING');
161+
$this->assertNewAddressFields($shippingAddressResponse, 'ShippingCartAddress');
162162
}
163163

164164
/**
@@ -380,7 +380,7 @@ public function testSetNewBillingAddressRedundantStreetLine()
380380
* @param array $addressResponse
381381
* @param string $addressType
382382
*/
383-
private function assertNewAddressFields(array $addressResponse, string $addressType = 'BILLING'): void
383+
private function assertNewAddressFields(array $addressResponse, string $addressType = 'BillingCartAddress'): void
384384
{
385385
$assertionMap = [
386386
['response_field' => 'firstname', 'expected_value' => 'test firstname'],
@@ -391,7 +391,7 @@ private function assertNewAddressFields(array $addressResponse, string $addressT
391391
['response_field' => 'postcode', 'expected_value' => '887766'],
392392
['response_field' => 'telephone', 'expected_value' => '88776655'],
393393
['response_field' => 'country', 'expected_value' => ['code' => 'US', 'label' => 'US']],
394-
['response_field' => 'address_type', 'expected_value' => $addressType]
394+
['response_field' => '__typename', 'expected_value' => $addressType]
395395
];
396396

397397
$this->assertResponseFields($addressResponse, $assertionMap);

0 commit comments

Comments
 (0)