Skip to content

Commit 2e845b8

Browse files
authored
LYNX-162: Add uids filters to entity queries custom_attriubtes fields (#117)
1 parent 82ebf79 commit 2e845b8

File tree

8 files changed

+837
-388
lines changed

8 files changed

+837
-388
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\Resolver;
9+
10+
use Magento\CustomerGraphQl\Model\Customer\ExtractCustomerData;
11+
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
12+
use Magento\Framework\Api\CustomAttributesDataInterface;
13+
use Magento\Framework\GraphQl\Config\Element\Field;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
17+
/**
18+
* Resolver Custom Attribute filter
19+
*/
20+
class CustomAttributeFilter implements ResolverInterface
21+
{
22+
/**
23+
* @var GetCustomer
24+
*/
25+
private GetCustomer $getCustomer;
26+
27+
/**
28+
* @var ExtractCustomerData
29+
*/
30+
private ExtractCustomerData $extractCustomerData;
31+
32+
/**
33+
* @param GetCustomer $getCustomer
34+
* @param ExtractCustomerData $extractCustomerData
35+
*/
36+
public function __construct(
37+
GetCustomer $getCustomer,
38+
ExtractCustomerData $extractCustomerData,
39+
) {
40+
$this->getCustomer = $getCustomer;
41+
$this->extractCustomerData = $extractCustomerData;
42+
}
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
public function resolve(
48+
Field $field,
49+
$context,
50+
ResolveInfo $info,
51+
array $value = null,
52+
array $args = null
53+
): array {
54+
$customAttributes = $value[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES];
55+
if (isset($args['uids']) && !empty($args['uids'])) {
56+
$selectedUids = array_values($args['uids']);
57+
return array_filter($customAttributes, function ($attr) use ($selectedUids) {
58+
return in_array($attr['uid'], $selectedUids);
59+
});
60+
}
61+
62+
return $customAttributes;
63+
}
64+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\Resolver;
9+
10+
use Magento\CustomerGraphQl\Model\Customer\Address\ExtractCustomerAddressData;
11+
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
12+
use Magento\Framework\Api\CustomAttributesDataInterface;
13+
use Magento\Framework\GraphQl\Config\Element\Field;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
17+
/**
18+
* Resolver Customer Address Custom Attribute filter
19+
*/
20+
class CustomerAddressCustomAttributeFilter implements ResolverInterface
21+
{
22+
/**
23+
* @var GetCustomerAddress
24+
*/
25+
private GetCustomerAddress $getCustomerAddress;
26+
27+
/**
28+
* @var ExtractCustomerAddressData
29+
*/
30+
private ExtractCustomerAddressData $extractCustomerAddressData;
31+
32+
/**
33+
* @param GetCustomerAddress $getCustomerAddress
34+
* @param ExtractCustomerAddressData $extractCustomerAddressData
35+
*/
36+
public function __construct(
37+
GetCustomerAddress $getCustomerAddress,
38+
ExtractCustomerAddressData $extractCustomerAddressData,
39+
) {
40+
$this->getCustomerAddress = $getCustomerAddress;
41+
$this->extractCustomerAddressData = $extractCustomerAddressData;
42+
}
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
public function resolve(
48+
Field $field,
49+
$context,
50+
ResolveInfo $info,
51+
array $value = null,
52+
array $args = null
53+
): array {
54+
$customAttributes = $value[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES . 'V2'];
55+
if (isset($args['uids']) && !empty($args['uids'])) {
56+
$selectedUids = array_values($args['uids']);
57+
return array_filter($customAttributes, function ($attr) use ($selectedUids) {
58+
return in_array($attr['uid'], $selectedUids);
59+
});
60+
}
61+
62+
return $customAttributes;
63+
}
64+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ type Customer @doc(description: "Defines the customer name, addresses, and other
139139
is_subscribed: Boolean @doc(description: "Indicates whether the customer is subscribed to the company's newsletter.") @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\IsSubscribed")
140140
addresses: [CustomerAddress] @doc(description: "An array containing the customer's shipping and billing addresses.") @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\CustomerAddresses")
141141
gender: Int @doc(description: "The customer's gender (Male - 1, Female - 2).")
142-
custom_attributes: [AttributeValueInterface] @doc(description: "Customer's custom attributes.")
142+
custom_attributes(uids: [ID!]): [AttributeValueInterface] @doc(description: "Customer's custom attributes.") @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\CustomAttributeFilter")
143143
}
144144

145145
type CustomerAddress @doc(description: "Contains detailed information about a customer's billing or shipping address."){
@@ -164,7 +164,7 @@ type CustomerAddress @doc(description: "Contains detailed information about a cu
164164
default_shipping: Boolean @doc(description: "Indicates whether the address is the customer's default shipping address.")
165165
default_billing: Boolean @doc(description: "Indicates whether the address is the customer's default billing address.")
166166
custom_attributes: [CustomerAddressAttribute] @deprecated(reason: "Use custom_attributesV2 instead.")
167-
custom_attributesV2: [AttributeValueInterface!]! @doc(description: "Custom attributes assigned to the customer address.")
167+
custom_attributesV2(uids: [ID!]): [AttributeValueInterface!]! @doc(description: "Custom attributes assigned to the customer address.") @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\CustomerAddressCustomAttributeFilter")
168168
extension_attributes: [CustomerAddressAttribute] @doc(description: "Contains any extension attributes for the address.")
169169
}
170170

app/code/Magento/EavGraphQl/Model/GetAttributeValueComposite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(array $providers = [])
3434
* @param string $entityType
3535
* @param array $customAttribute
3636
* @return array|null
37-
* @throws RuntimeException
37+
* @throws RuntimeException|LocalizedException
3838
*/
3939
public function execute(string $entityType, array $customAttribute): ?array
4040
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class CreateCustomerV2WithCustomAttributesTest extends GraphQlAbstract
9898
password: "test123#"
9999
custom_attributes: [
100100
{
101-
attribute_code: "%s",
101+
attribute_code: "%s"
102102
value: "%s"
103103
}
104104
{

0 commit comments

Comments
 (0)