Magento, Magento 2

How to Get Products by Category using GraphQL in Magento 2

How to Get Products by Category using GraphQL in Magento 2

In this tutorial, Today I will explain to how to get products by category using GraphQL in Magento 2. After Magento 2.3.x, we can use GraphQL which is useful as an alternate option of REST API and SOAP API.

GraphQL is one of the query language for the API which is used to load only requested data from server. In REST API, we can fetch products by category. But, if we want to fetch products by category in GraphQL then, how can we fetch?

Let’s follow the below steps :

You may also like this :

Execute URL : {baseURL}/graphql

Where Base URL : http://127.0.0.1/m235/ (your store base url) and End Point : graphql

In Request Body, You need to pass below data.

Request :

{
  products(
    filter: {category_id: {eq: "3"}},
    sort: {name: ASC},
    pageSize: 2,
    currentPage: 1
  ) {
    total_count
    items {
      name
      sku
      price_range {
        minimum_price {
          regular_price {
            value
            currency
          }
          final_price {
            value
            currency
          }
          discount {
            amount_off
            percent_off
          }
        }
        maximum_price {
          regular_price {
            value
            currency
          }
          final_price {
            value
            currency
          }
          discount {
            amount_off
            percent_off
          }
        }
      }
    }
  }
}

Here, you need to pass category id in graphql query request.

Output :

{
  "data": {
    "products": {
      "total_count": 35,
      "items": [
        {
          "name": "Affirm Water Bottle ",
          "sku": "24-UG06",
          "price_range": {
            "minimum_price": {
              "regular_price": {
                "value": 7,
                "currency": "USD"
              },
              "final_price": {
                "value": 7,
                "currency": "USD"
              },
              "discount": {
                "amount_off": 0,
                "percent_off": 0
              }
            },
            "maximum_price": {
              "regular_price": {
                "value": 7,
                "currency": "USD"
              },
              "final_price": {
                "value": 7,
                "currency": "USD"
              },
              "discount": {
                "amount_off": 0,
                "percent_off": 0
              }
            }
          }
        },
        {
          "name": "Aim Analog Watch",
          "sku": "24-MG04",
          "price_range": {
            "minimum_price": {
              "regular_price": {
                "value": 45,
                "currency": "USD"
              },
              "final_price": {
                "value": 45,
                "currency": "USD"
              },
              "discount": {
                "amount_off": 0,
                "percent_off": 0
              }
            },
            "maximum_price": {
              "regular_price": {
                "value": 45,
                "currency": "USD"
              },
              "final_price": {
                "value": 45,
                "currency": "USD"
              },
              "discount": {
                "amount_off": 0,
                "percent_off": 0
              }
            }
          }
        }
      ]
    }
  }
}

Products GraphQL Magento 2

That’s it !!!

I hope this blog is easy to understand about how to get products by category using GraphQL in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Stay Safe and Stay Connected !!

Tagged , ,