-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Graphql products query returns only ids for select or multiselect fields #31622
Open
shikhamis11
wants to merge
5
commits into
magento:2.4-develop
Choose a base branch
from
shikhamis11:28200_custom_attribute
base: 2.4-develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
111f2ff
#28200:Graphql products query returns only ids for select or multisel…
shikhamis11 77f7735
Graphql products query returns only ids for select or multiselect fields
9d7f91e
Merge branch '2.4-develop' into 28200_custom_attribute
cpartica 3a7e198
Merge branch '2.4-develop' into 28200_custom_attribute
6b230c5
#28200 fixed static test
shikhamis11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
app/code/Magento/CatalogGraphQl/Model/Product/Attributes/Collection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Product\Attributes; | ||
|
||
use Magento\CatalogGraphQl\Model\Resolver\Products\Attributes\Collection as AttributesCollection; | ||
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product as ProductDataProvider; | ||
use Magento\Eav\Model\Entity\Attribute\Frontend\DefaultFrontend; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\Framework\Phrase; | ||
|
||
/** | ||
* Collection for fetching custom attributes for products in filter. | ||
*/ | ||
class Collection | ||
{ | ||
/** | ||
* @var int[] | ||
*/ | ||
private $productIds = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $attributeValueMap = []; | ||
|
||
/** | ||
* @var AttributesCollection | ||
*/ | ||
private $attributesCollection; | ||
|
||
/** | ||
* @var ProductDataProvider | ||
*/ | ||
private $productDataProvider; | ||
|
||
/** | ||
* @var SearchCriteriaBuilder | ||
*/ | ||
private $searchCriteriaBuilder; | ||
|
||
/** | ||
* @var DefaultFrontend | ||
*/ | ||
private $frontend; | ||
|
||
/** | ||
* @param AttributesCollection $attributesCollection | ||
* @param ProductDataProvider $productDataProvider | ||
* @param SearchCriteriaBuilder $searchCriteriaBuilder | ||
* @param DefaultFrontend $frontend | ||
*/ | ||
public function __construct( | ||
AttributesCollection $attributesCollection, | ||
ProductDataProvider $productDataProvider, | ||
SearchCriteriaBuilder $searchCriteriaBuilder, | ||
DefaultFrontend $frontend | ||
) { | ||
$this->attributesCollection = $attributesCollection; | ||
$this->productDataProvider = $productDataProvider; | ||
$this->searchCriteriaBuilder = $searchCriteriaBuilder; | ||
$this->frontend = $frontend; | ||
} | ||
|
||
/** | ||
* Add product id to attribute collection filter. | ||
* | ||
* @param int $productId | ||
*/ | ||
public function addProductId(int $productId): void | ||
{ | ||
if (!in_array($productId, $this->productIds)) { | ||
$this->productIds[] = $productId; | ||
} | ||
} | ||
|
||
/** | ||
* Retrieve attributes values for given product id or empty array | ||
* | ||
* @param int $productId | ||
* @return array | ||
*/ | ||
public function getAttributesValueByProductId(int $productId): array | ||
{ | ||
$attributes = $this->fetch(); | ||
if (!isset($attributes[$productId])) { | ||
return []; | ||
} | ||
|
||
return $attributes[$productId]; | ||
} | ||
|
||
/** | ||
* Fetch attribute data | ||
* | ||
* @return array | ||
*/ | ||
private function fetch(): array | ||
{ | ||
if (empty($this->productIds) || !empty($this->attributeValueMap)) { | ||
return $this->attributeValueMap; | ||
} | ||
|
||
$attributes = $this->attributesCollection->getAttributes(); | ||
|
||
$this->searchCriteriaBuilder->addFilter('entity_id', $this->productIds, 'in'); | ||
$products = $this->productDataProvider->getList( | ||
$this->searchCriteriaBuilder->create(), | ||
['*'], | ||
false, | ||
true | ||
)->getItems(); | ||
|
||
foreach ($products as $productId => $product) { | ||
$data = []; | ||
foreach ($attributes as $attribute) { | ||
$value = $this->frontend->setAttribute($attribute)->getValue($product); | ||
if ($value instanceof Phrase) { | ||
$value = (string) $value; | ||
} | ||
$value = $value ? $value : null; | ||
$data[] = [ | ||
'value' => $value, | ||
'code' => $attribute->getAttributeCode(), | ||
]; | ||
} | ||
$this->attributeValueMap[$productId] = $data; | ||
} | ||
return $this->attributeValueMap; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
app/code/Magento/CatalogGraphQl/Model/Resolver/Product/CustomAttributes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Product; | ||
|
||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\CatalogGraphQl\Model\Product\Attributes\Collection as AttributesCollection; | ||
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; | ||
|
||
/** | ||
* Retrieves CustomAttributes | ||
*/ | ||
class CustomAttributes implements ResolverInterface | ||
{ | ||
/** | ||
* @var AttributesCollection | ||
*/ | ||
private $attributesCollection; | ||
|
||
/** | ||
* @var ValueFactory | ||
*/ | ||
private $valueFactory; | ||
|
||
/** | ||
* @param AttributesCollection $attributesCollection | ||
* @param ValueFactory $valueFactory | ||
*/ | ||
public function __construct( | ||
AttributesCollection $attributesCollection, | ||
ValueFactory $valueFactory | ||
) { | ||
$this->attributesCollection = $attributesCollection; | ||
$this->valueFactory = $valueFactory; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
if (!isset($value['model'])) { | ||
throw new LocalizedException(__('"model" value should be specified')); | ||
} | ||
$product = $value['model']; | ||
|
||
$this->attributesCollection->addProductId((int)$product->getId()); | ||
$productId = $product->getId(); | ||
$result = function () use ($productId) { | ||
return $this->attributesCollection->getAttributesValueByProductId((int)$productId); | ||
}; | ||
return $this->valueFactory->create($result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...sts/api-functional/testsuite/Magento/GraphQl/Catalog/ProductsWithCustomAttributesTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare (strict_types = 1); | ||
|
||
namespace Magento\GraphQl\Catalog; | ||
|
||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
|
||
/** | ||
* Test class for product with custom attributes | ||
*/ | ||
class ProductsWithCustomAttributesTest extends GraphQlAbstract | ||
{ | ||
/** | ||
* @magentoApiDataFixture Magento/Catalog/_files/products_with_multiselect_attribute.php | ||
*/ | ||
public function testQueryProductWithMultiSelectCustomAttribute() | ||
{ | ||
if (!$this->cleanCache()) { | ||
$this->fail('Cache could not be cleaned properly.'); | ||
} | ||
|
||
$multiSelectAttribute = "multiselect_attribute"; | ||
$value = 'Option 2, Option 3, Option 4 "!@#$%^&*'; | ||
$query = <<<QUERY | ||
{ | ||
products(filter: {sku: {eq: "simple_ms_2" }}) { | ||
items { | ||
name | ||
custom_attributes{ | ||
code | ||
value | ||
} | ||
} | ||
} | ||
} | ||
QUERY; | ||
$response = $this->graphQlQuery($query); | ||
$this->assertArrayHasKey('products', $response); | ||
$this->assertArrayHasKey('items', $response['products']); | ||
$this->assertCount(1, $response['products']['items']); | ||
$this->assertArrayHasKey(0, $response['products']['items']); | ||
$this->assertArrayHasKey('custom_attributes', $response['products']['items'][0]); | ||
$customAttributes = $response['products']['items'][0]['custom_attributes']; | ||
$this->assertCustomAttribute($customAttributes, $multiSelectAttribute, $value); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_custom_attribute.php | ||
*/ | ||
public function testQueryProductWithTextCustomAttribute() | ||
{ | ||
if (!$this->cleanCache()) { | ||
$this->fail('Cache could not be cleaned properly.'); | ||
} | ||
$prductSku = 'simple'; | ||
$textAttribute = "attribute_code_custom"; | ||
$value = 'customAttributeValue'; | ||
$query = <<<QUERY | ||
{ | ||
products(filter: {sku: {eq: "{$prductSku}"}}) | ||
{ | ||
items | ||
{ | ||
custom_attributes{ | ||
code | ||
value | ||
} | ||
} | ||
} | ||
} | ||
QUERY; | ||
|
||
$response = $this->graphQlQuery($query); | ||
$this->assertArrayHasKey('products', $response); | ||
$this->assertArrayHasKey('items', $response['products']); | ||
$this->assertCount(1, $response['products']['items']); | ||
$this->assertArrayHasKey(0, $response['products']['items']); | ||
$this->assertArrayHasKey('custom_attributes', $response['products']['items'][0]); | ||
$customAttributes = $response['products']['items'][0]['custom_attributes']; | ||
$this->assertCustomAttribute($customAttributes, $textAttribute, $value); | ||
} | ||
|
||
/** | ||
* @param array $customAttributes | ||
* @param string $searchAttribute | ||
* @param string $actualValue | ||
*/ | ||
private function assertCustomAttribute($customAttributes, $searchAttribute, $actualValue) | ||
{ | ||
$flag = false; | ||
$customAttributeValue = ""; | ||
foreach ($customAttributes as $attributes) { | ||
if ($attributes['code'] == $searchAttribute) { | ||
$flag = true; | ||
$customAttributeValue = $attributes['value']; | ||
break; | ||
} | ||
} | ||
$this->assertStringContainsString($actualValue, $customAttributeValue); | ||
$this->assertEquals(true, $flag); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this conflicts with https://github.com/magento/magento2-pwa/blob/main/CatalogGraphQlAux/etc/schema.graphqls#L23