Skip to content

Commit 6cc68c6

Browse files
authored
Merge pull request #3892 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents ab66a14 + a738086 commit 6cc68c6

File tree

54 files changed

+1041
-789
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1041
-789
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/CategoryTree.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1212
use Magento\Framework\GraphQl\Config\Element\Field;
1313
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1415
use Magento\Framework\GraphQl\Query\ResolverInterface;
1516

1617
/**
@@ -72,11 +73,12 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7273

7374
$rootCategoryId = $this->getCategoryId($args);
7475
$categoriesTree = $this->categoryTree->getTree($info, $rootCategoryId);
75-
if (!empty($categoriesTree)) {
76-
$result = $this->extractDataFromCategoryTree->execute($categoriesTree);
77-
return current($result);
78-
} else {
79-
return null;
76+
77+
if (empty($categoriesTree) || ($categoriesTree->count() == 0)) {
78+
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
8079
}
80+
81+
$result = $this->extractDataFromCategoryTree->execute($categoriesTree);
82+
return current($result);
8183
}
8284
}

app/code/Magento/ConfigurableProductGraphQl/Model/Variant/Collection.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,17 @@ public function __construct(
9090
*/
9191
public function addParentProduct(Product $product) : void
9292
{
93-
if (isset($this->parentProducts[$product->getId()])) {
93+
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
94+
$productId = $product->getData($linkField);
95+
96+
if (isset($this->parentProducts[$productId])) {
9497
return;
9598
}
9699

97100
if (!empty($this->childrenMap)) {
98101
$this->childrenMap = [];
99102
}
100-
$this->parentProducts[$product->getId()] = $product;
103+
$this->parentProducts[$productId] = $product;
101104
}
102105

103106
/**
@@ -140,16 +143,12 @@ private function fetch() : array
140143
return $this->childrenMap;
141144
}
142145

143-
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
144146
foreach ($this->parentProducts as $product) {
145147
$attributeData = $this->getAttributesCodes($product);
146148
/** @var ChildCollection $childCollection */
147149
$childCollection = $this->childCollectionFactory->create();
148-
$childCollection->addAttributeToSelect($attributeData);
149-
150-
/** @var Product $product */
151-
$product->setData($linkField, $product->getId());
152150
$childCollection->setProductFilter($product);
151+
$childCollection->addAttributeToSelect($attributeData);
153152

154153
/** @var Product $childProduct */
155154
foreach ($childCollection->getItems() as $childProduct) {
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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\CustomerGraphQl\Model\Customer\Address;
9+
10+
use Magento\Customer\Api\AddressRepositoryInterface;
11+
use Magento\Customer\Api\Data\AddressInterface;
12+
use Magento\Customer\Api\Data\AddressInterfaceFactory;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
15+
use Magento\Framework\Api\DataObjectHelper;
16+
17+
/**
18+
* Create customer address
19+
*/
20+
class CreateCustomerAddress
21+
{
22+
/**
23+
* @var GetAllowedAddressAttributes
24+
*/
25+
private $getAllowedAddressAttributes;
26+
27+
/**
28+
* @var AddressInterfaceFactory
29+
*/
30+
private $addressFactory;
31+
32+
/**
33+
* @var AddressRepositoryInterface
34+
*/
35+
private $addressRepository;
36+
37+
/**
38+
* @var DataObjectHelper
39+
*/
40+
private $dataObjectHelper;
41+
42+
/**
43+
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes
44+
* @param AddressInterfaceFactory $addressFactory
45+
* @param AddressRepositoryInterface $addressRepository
46+
* @param DataObjectHelper $dataObjectHelper
47+
*/
48+
public function __construct(
49+
GetAllowedAddressAttributes $getAllowedAddressAttributes,
50+
AddressInterfaceFactory $addressFactory,
51+
AddressRepositoryInterface $addressRepository,
52+
DataObjectHelper $dataObjectHelper
53+
) {
54+
$this->getAllowedAddressAttributes = $getAllowedAddressAttributes;
55+
$this->addressFactory = $addressFactory;
56+
$this->addressRepository = $addressRepository;
57+
$this->dataObjectHelper = $dataObjectHelper;
58+
}
59+
60+
/**
61+
* Create customer address
62+
*
63+
* @param int $customerId
64+
* @param array $data
65+
* @return AddressInterface
66+
* @throws GraphQlInputException
67+
*/
68+
public function execute(int $customerId, array $data): AddressInterface
69+
{
70+
$this->validateData($data);
71+
72+
/** @var AddressInterface $address */
73+
$address = $this->addressFactory->create();
74+
$this->dataObjectHelper->populateWithArray($address, $data, AddressInterface::class);
75+
76+
if (isset($data['region']['region_id'])) {
77+
$address->setRegionId($address->getRegion()->getRegionId());
78+
}
79+
$address->setCustomerId($customerId);
80+
81+
try {
82+
$this->addressRepository->save($address);
83+
} catch (LocalizedException $e) {
84+
throw new GraphQlInputException(__($e->getMessage()), $e);
85+
}
86+
return $address;
87+
}
88+
89+
/**
90+
* Validate customer address create data
91+
*
92+
* @param array $addressData
93+
* @return void
94+
* @throws GraphQlInputException
95+
*/
96+
public function validateData(array $addressData): void
97+
{
98+
$attributes = $this->getAllowedAddressAttributes->execute();
99+
$errorInput = [];
100+
101+
foreach ($attributes as $attributeName => $attributeInfo) {
102+
if ($attributeInfo->getIsRequired()
103+
&& (!isset($addressData[$attributeName]) || empty($addressData[$attributeName]))
104+
) {
105+
$errorInput[] = $attributeName;
106+
}
107+
}
108+
109+
if ($errorInput) {
110+
throw new GraphQlInputException(
111+
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
112+
);
113+
}
114+
}
115+
}

app/code/Magento/CustomerGraphQl/Model/Customer/Address/CustomerAddressCreateDataValidator.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

app/code/Magento/CustomerGraphQl/Model/Customer/Address/CustomerAddressUpdateDataValidator.php

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\CustomerGraphQl\Model\Customer\Address;
9+
10+
use Magento\Customer\Api\AddressRepositoryInterface;
11+
use Magento\Customer\Api\Data\AddressInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
15+
/**
16+
* Delete customer address
17+
*/
18+
class DeleteCustomerAddress
19+
{
20+
/**
21+
* @var AddressRepositoryInterface
22+
*/
23+
private $addressRepository;
24+
25+
/**
26+
* @param AddressRepositoryInterface $addressRepository
27+
*/
28+
public function __construct(
29+
AddressRepositoryInterface $addressRepository
30+
) {
31+
$this->addressRepository = $addressRepository;
32+
}
33+
34+
/**
35+
* Delete customer address
36+
*
37+
* @param AddressInterface $address
38+
* @return void
39+
* @throws GraphQlInputException
40+
*/
41+
public function execute(AddressInterface $address): void
42+
{
43+
if ($address->isDefaultBilling()) {
44+
throw new GraphQlInputException(
45+
__('Customer Address %1 is set as default billing address and can not be deleted', [$address->getId()])
46+
);
47+
}
48+
if ($address->isDefaultShipping()) {
49+
throw new GraphQlInputException(
50+
__('Customer Address %1 is set as default shipping address and can not be deleted', [$address->getId()])
51+
);
52+
}
53+
54+
try {
55+
$this->addressRepository->delete($address);
56+
} catch (LocalizedException $e) {
57+
throw new GraphQlInputException(__($e->getMessage()), $e);
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)