How to filter product collection with default graphql products query in Magento 2?

How to filter product collection with default GraphQl products query in Magento 2? – This topic is very important when it comes to the layered navigation. Normally we need to filter our product collection with custom product attributes and default product attributes.

The native GraphQl products query not include color, size, and custom attributes. So today we are going to see how we can get this done by the use of a custom module.

You can view all my Magento 2 GraphQL tutorials below.

  1. What is GraphQl in Magento 2 and how to access it?
  2. How to create GraphQl schema for Magento 2 custom module with custom table?
  3. How to create a GraphQl schema by passing argument for custom magento 2 module with custom table ?
  4. How to write GrapgQl mutation to create and integrate the contact us page functionality in Magento 2?
  5. How to filter product collection with default GraphQl products query in Magento 2?
  6. How to write custom search using GraphQl in Magento 2?
  7. How to get the store configuration values using GraphQl in magento 2?

Custom Module’s Structure.

Custom Product Filter with GraphQl
Module Structure

module.xml file

We want to define, undefined(in Native GraphQl) products attribute in the di.xml(Ayakil\CatalogGraphQl\ figraphql\di.xml) file first. As well as our custom product attributes too. This module I will handle color, size(default attribute), region, language(custom Attribute).

After this, we want to define the custom attribute and native product attribute as FilterTypeInput to ProductFilterInput in the schema.graphqls file like below.

After this, we want to create ProductAttributeFilter.php file under the Model directory. As we defined in the di.xml file(path is Ayakil\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\). I followed the default Magento’s module-catlog-graph-ql module to achieve this task.

This will help you to filter the product collection with product attributes that are not defined in native products GraphQl. So we can use the default products GraphQl query to get the result. Below is the GrapQl query which I am passing with the custom attributes to get the collection.

graphql query

In this query, I have passed the region, language, color as filters to the native products GrapQl query to get the results. Below is the sample result I have received from the query.

magento 2 graphql
graphql result

That’s it for today’s article. The summery for the entire article is

  1. Create custom module.
  2. Define the attributes in di.xml.
  3. Add the attributes to ProductFilterInput.
  4. Create the ProductAttributeFilter.php file in the relevant directory as defined in di.xml.
  5. Run the graphql query to get the result.

Always welcome the corrections, suggestions, and improvements to my blog. You can read my previous articles here.
Have a nice day. Enjoy coding, Learn, Experience, Teach, and Help.

7 thoughts on “How to filter product collection with default graphql products query in Magento 2?”

  1. I’m unable to run graphql after deploying my code which was most similar to your usecase.

    Can you share your module please.

Leave a Reply

Your email address will not be published. Required fields are marked *