0

I need to override default graphq products search to allow typos, so that queries like that:

{
    products(
        search: "test"
    ) {
        items {
            name
        }
    }
}
{
    products(
        search: "tets"
    ) {
        items {
            name
        }
    }
}

return same results, I also need them to work with synonyms as well

I'm getting the idea I could replace the elastic query with something like that:

{
    "query": {
        "fuzzy": {
            "name": "tets"
        }
    }
}
  1. What classes should I override to accomplish that?

  2. How to make it work with synonyms too

1
  • do you want to override graphql? endpoint
    – Waqar Ali
    Jun 24, 2022 at 10:37

1 Answer 1

0

Magento 2.4.x

You can check in magento default vendor. Please check this schema.graphqls file at magentoRoot/vendor/magento/module-catalog-graph-ql/etc/schema.graphqls. In this file there are many query and mutation are define which is by default magento provides. In this file we see below code:

products (
        search: String @doc(description: "Performs a full-text search using the specified key words."),
        filter: ProductAttributeFilterInput @doc(description: "Identifies which product attributes to search for and return."),
        pageSize: Int = 20 @doc(description: "Specifies the maximum number of results to return at once. This attribute is optional."),
        currentPage: Int = 1 @doc(description: "Specifies which page of results to return. The default value is 1."),
        sort: ProductAttributeSortInput @doc(description: "Specifies which attributes to sort on, and whether to return the results in ascending or descending order.")
    ): Products
    @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Products") @doc(description: "The products query searches for products that match the criteria specified in the search and filter attributes.") @cache(cacheIdentity: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\Identity")

For synonyms, you can modify this query as per your requirement. You can also add one more parameter for adding synonyms or you can modify the resolver file.

Resolver File Path: magentoRoot/vendor/magento/module-catalog-graph-ql/Model/Resolver/Products.php

Example: Overwrite GraphQL Resolver File

app/code/VendoreName/GraphQLCustomize/etc/graphql

di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    
    <preference for="Magento\CatalogGraphQl\Model\Resolver\Products" type="VendoreName\GraphQLCustomize\Model\Resolver\Products"/>

</config>

app/code/VendoreName/GraphQLCustomize/Model/Resolver

Products.php

<?php

declare(strict_types=1);

namespace VendoreName\GraphQLCustomize\Model\Resolver;

use Magento\CatalogGraphQl\Model\Resolver\Products\Query\ProductQueryInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\CatalogGraphQl\DataProvider\Product\SearchCriteriaBuilder;

/**
 * Products field resolver, used for GraphQL request processing.
 */
class Products implements ResolverInterface
{
    /**
     * @var ProductQueryInterface
     */
    private $searchQuery;

    /**
     * @var SearchCriteriaBuilder
     */
    private $searchApiCriteriaBuilder;

    /**
     * @param ProductQueryInterface $searchQuery
     * @param SearchCriteriaBuilder|null $searchApiCriteriaBuilder
     */
    public function __construct(
        ProductQueryInterface $searchQuery,
        SearchCriteriaBuilder $searchApiCriteriaBuilder = null
    ) {
        $this->searchQuery = $searchQuery;
        $this->searchApiCriteriaBuilder = $searchApiCriteriaBuilder ??
            \Magento\Framework\App\ObjectManager::getInstance()->get(SearchCriteriaBuilder::class);
    }

    /**
     * @inheritdoc
     */
    public function resolve(
        Field $field,
        $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        $data = [];
        $data[] = "You have been overrited resolver file";
        return $data;
    }
}

For more Click Here:

  1. https://magento.stackexchange.com/a/353491/82670
  2. https://magento.stackexchange.com/a/353494/82670

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.