Skip to content

Commit

Permalink
LYNX-162: Add uids filters to entity queries custom_attriubtes fields (
Browse files Browse the repository at this point in the history
  • Loading branch information
loginesta authored May 24, 2023
1 parent 82ebf79 commit 2e845b8
Show file tree
Hide file tree
Showing 8 changed files with 837 additions and 388 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Resolver;

use Magento\CustomerGraphQl\Model\Customer\ExtractCustomerData;
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\Api\CustomAttributesDataInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* Resolver Custom Attribute filter
*/
class CustomAttributeFilter implements ResolverInterface
{
/**
* @var GetCustomer
*/
private GetCustomer $getCustomer;

/**
* @var ExtractCustomerData
*/
private ExtractCustomerData $extractCustomerData;

/**
* @param GetCustomer $getCustomer
* @param ExtractCustomerData $extractCustomerData
*/
public function __construct(
GetCustomer $getCustomer,
ExtractCustomerData $extractCustomerData,
) {
$this->getCustomer = $getCustomer;
$this->extractCustomerData = $extractCustomerData;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): array {
$customAttributes = $value[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES];
if (isset($args['uids']) && !empty($args['uids'])) {
$selectedUids = array_values($args['uids']);
return array_filter($customAttributes, function ($attr) use ($selectedUids) {
return in_array($attr['uid'], $selectedUids);
});
}

return $customAttributes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Resolver;

use Magento\CustomerGraphQl\Model\Customer\Address\ExtractCustomerAddressData;
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
use Magento\Framework\Api\CustomAttributesDataInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* Resolver Customer Address Custom Attribute filter
*/
class CustomerAddressCustomAttributeFilter implements ResolverInterface
{
/**
* @var GetCustomerAddress
*/
private GetCustomerAddress $getCustomerAddress;

/**
* @var ExtractCustomerAddressData
*/
private ExtractCustomerAddressData $extractCustomerAddressData;

/**
* @param GetCustomerAddress $getCustomerAddress
* @param ExtractCustomerAddressData $extractCustomerAddressData
*/
public function __construct(
GetCustomerAddress $getCustomerAddress,
ExtractCustomerAddressData $extractCustomerAddressData,
) {
$this->getCustomerAddress = $getCustomerAddress;
$this->extractCustomerAddressData = $extractCustomerAddressData;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): array {
$customAttributes = $value[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES . 'V2'];
if (isset($args['uids']) && !empty($args['uids'])) {
$selectedUids = array_values($args['uids']);
return array_filter($customAttributes, function ($attr) use ($selectedUids) {
return in_array($attr['uid'], $selectedUids);
});
}

return $customAttributes;
}
}
4 changes: 2 additions & 2 deletions app/code/Magento/CustomerGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ type Customer @doc(description: "Defines the customer name, addresses, and other
is_subscribed: Boolean @doc(description: "Indicates whether the customer is subscribed to the company's newsletter.") @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\IsSubscribed")
addresses: [CustomerAddress] @doc(description: "An array containing the customer's shipping and billing addresses.") @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\CustomerAddresses")
gender: Int @doc(description: "The customer's gender (Male - 1, Female - 2).")
custom_attributes: [AttributeValueInterface] @doc(description: "Customer's custom attributes.")
custom_attributes(uids: [ID!]): [AttributeValueInterface] @doc(description: "Customer's custom attributes.") @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\CustomAttributeFilter")
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(array $providers = [])
* @param string $entityType
* @param array $customAttribute
* @return array|null
* @throws RuntimeException
* @throws RuntimeException|LocalizedException
*/
public function execute(string $entityType, array $customAttribute): ?array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class CreateCustomerV2WithCustomAttributesTest extends GraphQlAbstract
password: "test123#"
custom_attributes: [
{
attribute_code: "%s",
attribute_code: "%s"
value: "%s"
}
{
Expand Down
Loading

0 comments on commit 2e845b8

Please sign in to comment.