magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.54k stars 9.31k forks source link

Magento2.4.7 Graphql products aggregation error #38998

Closed lano-vargas closed 2 months ago

lano-vargas commented 2 months ago

Preconditions and environment

Steps to reproduce

{
  products(filter: {color: {eq: "52"}}, sort: {name: DESC}) {
    aggregations {
      attribute_code
      count
      label
      options {
        label
        value
        count
      }
    }
    items {
      name
      sku
      price_range {
        minimum_price {
          regular_price {
            value
            currency
          }
        }
      }
    }
    page_info {
      page_size
    }
  }
}

Expected result

Not to return error:

"path": [
                "products",
                "aggregations"
            ]

aggregations to return data but it's null

Actual result

{
    "errors": [
        {
            "message": "Internal server error",
            "locations": [
                {
                    "line": 3,
                    "column": 5
                }
            ],
            "path": [
                "products",
                "aggregations"
            ]
        }
    ],
    "data": {
        "products": {
            "aggregations": null,
            "items": [
                {
                    "name": "Tie Gift",
                    "sku": "I03966",
                    "price_range": {......

Additional information

Upon checking exception log:

GraphQL (3:5)
2:   products(filter: {brand: {eq: "409"}}, sort: {name: DESC}) {
3:     aggregations {
       ^
4:       attribute_code
 {"exception":"[object] (GraphQL\\Error\\Error(code: 0): Warning: Undefined array key \"attribute_type\" in /var/www/vhosts/test.com/htdocs/vendor/magento/module-catalog-graph-ql/DataProvider/Product/LayeredNavigation/Builder/Attribute.php on line 100 at /var/www/vhosts/test.com/htdocs/vendor/webonyx/graphql-php/src/Error/Error.php:170)
[previous exception] [object] (Exception(code: 0): Warning: Undefined array key \"attribute_type\" in /var/www/vhosts/test.com/htdocs/vendor/magento/module-catalog-graph-ql/DataProvider/Product/LayeredNavigation/Builder/Attribute.php on line 100 at /var/www/vhosts/test.com/htdocs/vendor/magento/framework/App/ErrorHandler.php:62)"} []test.com

Release note

No response

Triage and priority

m2-assistant[bot] commented 2 months ago

Hi @lano-vargas. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

lano-vargas commented 2 months ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 2 months ago

Hi @lano-vargas. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 2 months ago

Hi @lano-vargas, here is your Magento Instance: https://75e4640aa749e38caecb37dbbf0ae9ef.instances-prod.magento-community.engineering Admin access: https://75e4640aa749e38caecb37dbbf0ae9ef.instances-prod.magento-community.engineering/admin_e513 Login: 20061308 Password: e9e9ba250872

lano-vargas commented 2 months ago

What is this version Magento ver. dev-develop. The issue is replicable on 2.4.7 can you give version 2.4.7 ? The issue might be fixed on your dev-develop version

lano-vargas commented 2 months ago

@magento give me 2.4.7 instance

m2-assistant[bot] commented 2 months ago

Hi @engcom-Bravo. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Bravo commented 2 months ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 2 months ago

Hi @engcom-Bravo. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 2 months ago

Hi @engcom-Bravo, here is your Magento Instance: https://75e4640aa749e38caecb37dbbf0ae9ef.instances-prod.magento-community.engineering Admin access: https://75e4640aa749e38caecb37dbbf0ae9ef.instances-prod.magento-community.engineering/admin_ba1b Login: 6e3b3aaf Password: 2793e56bf25e

engcom-Bravo commented 2 months ago

Hi @lano-vargas,

Thanks for your reporting and collaboration.

We have verified the issue in Latest 2.4-develop instance and the issue is not reproducible.Kindly refer the screenshots.

Screenshot 2024-08-06 at 09 56 07

We are not getting any aggregation error.It might be possible issue got fixed in Latest 2.4-develop instance.

Thanks.

lano-vargas commented 2 months ago

Something weird going on here. It's a specific scenario issue here, I'm not entirely sure how to completely replicate this but I have done is mess about the attribute+options creation/deletion and products attribute options assignment creation/deletion

It appears that when an attribute option is deleted but there are still products assigned to it the graphl returns some extra array of object which appear to be the product that were assigned to previous attribute that was deleted.

It return the attribute id on both field e.g:

{
       "count": 1,
       "label": "417",
        "value": "417",
        "__typename": "AggregationOption"
}

in Label it should return a string name such as colour etc...

engcom-Bravo commented 2 months ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 2 months ago

Hi @engcom-Bravo. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 2 months ago

Hi @engcom-Bravo, here is your Magento Instance: https://75e4640aa749e38caecb37dbbf0ae9ef.instances-prod.magento-community.engineering Admin access: https://75e4640aa749e38caecb37dbbf0ae9ef.instances-prod.magento-community.engineering/admin_3a6b Login: 1a20dbae Password: 05c5bb00b9f1

engcom-Bravo commented 2 months ago

Hi @lano-vargas,

Thanks for your update.

We have verified the issue in Latest 2.4-develop instance and the issue is not reproducible.Kindly refer the screenshots.

We have created attribute and assigned to some of the products and after that deleted that attribute.

Screenshot 2024-08-27 at 11 53 54

There was no error.We are getting aggregations.

Hence we are closing this issue.

Thanks.

hnsr commented 3 weeks ago

Just wanted to chime in and let you now that I am also observing this exact same error on a 2.4.7 instance. Will try and see if I can help with the reproduction.

hnsr commented 3 weeks ago

Looks like this error was happening due to certain products having non-existent option ID references for the color attribute. This was probably the result of a botched data migration in our clients project.

raoulguillermo commented 1 week ago

I have(had) the same issue. Fixed it by changing the code.

/vendor/magento/module-catalog-graph-ql/DataProvider/Product/LayeredNavigation/Builder/Attribute.php line 100

public function build(AggregationInterface $aggregation, ?int $storeId): array
{
    $attributeOptions = $this->getAttributeOptions($aggregation, $storeId);
    $result = [];
    foreach ($this->getAttributeBuckets($aggregation) as $bucket) {
        $bucketName = $bucket->getName();
        $attributeCode = \preg_replace('~_bucket$~', '', $bucketName);
        $attribute = $attributeOptions[$attributeCode] ?? [];

        $result[$bucketName] = $this->layerFormatter->buildLayer(
            $attribute['attribute_label'] ?? $bucketName,
            \count($bucket->getValues()),
            $attribute['attribute_code'] ?? $bucketName,
            isset($attribute['position']) ? $attribute['position'] : null
        );

        $options = $this->getSortedOptions($bucket, isset($attribute['options']) ? $attribute['options'] : []);
        foreach ($options as $option) {
            $result[$bucketName]['options'][] = $this->layerFormatter->buildItem(
                $option['label'],
                $option['value'],
                $option['count']
            );
        }
    }

    return $result;
}

I don't think this is the way to go. But works for now