magento2: Suggestions is not working in GraphQL

Preconditions and environment

Magento version (2.4.6-p1 CE) Magento add the suggestions attribute in GraphQL for the product Type Query

image

But when we fetch the data, suggestions are not showing even if the requested query has results because I have checked Magento to add suggestions when the search result has zero $totalcount in results.

https://github.com/magento/magento2/blob/2.4.6/app/code/Magento/CatalogGraphQl/Model/Resolver/Products/Query/Search.php#L161

Steps to reproduce

I am using Altair GraphQL chrome extension. Query is

{
  products(
	search: "metro"

   ) {
	page_info{
  	current_page
  	page_size
  	total_pages
	},
	suggestions {
  	search
	}
	items {
  	name
  	sku
  	calculated_sqm_price
  }
}
}

When the query has result :

image

When Query has no result

image

Expected result

I want to give matching terms in suggestion as Magento frontend return

image

I have debugged when the total count is zero and then triggered this resolver Magento\CatalogGraphQl\Model\Resolver\Products\Query\Suggestions::execute

https://github.com/magento/magento2/blob/d846142a3ab8b49597dfb8bd7508d875efdab19a/app/code/Magento/CatalogGraphQl/Model/Resolver/Products/Query/Suggestions.php#L52

But the $suggestionItems are empty.

Actual result

The Actual result is suggestions are null

image

I have check

Additional information

No response

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 2
  • Comments: 18 (4 by maintainers)

Most upvoted comments

hello @engcom-Dash as clearly seen in your screenshot as well, those suggestions are null and you are saying the issue is not reproducible. let me reiterate my query regarding suggestions The suggestions remain empty in both cases, either we have a matching result referring to screenshot1 OR we have no result referring to screenshot2

Does “suggestions” mean in Graphqls product() query that to show related products name Please guide me in this regard

I have fixed it on my end, but I am waiting for Magento end to fix it permanently.

In the custom module “schema.graphqls” file overrides Magento product()

type Products @doc(description: "Contains the results of a `products` query.") {
    suggestions (search: String!): [SearchSuggestion] @doc(description: "An array of search suggestions for case when search query")
    @resolver(class: "Vendor\\CustomModule\\Model\\Resolver\\SearchSuggestions")
}

type SearchSuggestion @doc(description: "A string that contains search suggestion") {
    term: String @doc(description: "Suggested term")
    count: Int @doc(description: "Number of results for the term")
}

Resolver class :

<?php

namespace Vendor\\CustomModule\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Smile\ElasticsuiteCore\Model\Autocomplete\Terms\DataProvider as AutocompleteDataProvider;
use Magento\Search\Model\QueryFactory;

class SearchSuggestions implements ResolverInterface
{
    private $autocompleteDataProvider;
    private $queryFactory;

    public function __construct(
        AutocompleteDataProvider $autocompleteDataProvider,
        QueryFactory $queryFactory
    ) {
        $this->autocompleteDataProvider = $autocompleteDataProvider;
        $this->queryFactory = $queryFactory;
    }

    public function resolve(
        Field       $field,
                    $context,
        ResolveInfo $info,
        array       $value = null,
        array       $args = null
    ) {
        $this->validateArgs($args);
        $this->queryFactory->get()->setQueryText($args['search']);
        $suggestions = $this->autocompleteDataProvider->getItems();
        return $this->formatSuggestions($suggestions);
    }

    private function formatSuggestions($suggestions)
    {
        $formattedSuggestions = [];
        foreach ($suggestions as $suggestion) {
            $formattedSuggestions[] = [
                'term' => $suggestion->getTitle(),
                'count' => $suggestion->getNumResults(),
            ];
        }
        return $formattedSuggestions;
    }

    private function validateArgs($args)
    {
        if (!isset($args['search'])) {
            throw new GraphQlInputException(
                __("'search' input argument is required.")
            );
        }
    }
}