How To Get Layered Navigation Data / Search Filter Data Using GraphQL in Magento 2.x

The following below Magento 2.x GraphQL Query to get Layered Navigation Data / Search Filter Data

Syntax

{
  products(filter: { Filter Query}) {

  Filter Data
  }
}  

Below GraphQL query to get all Search Filter Data , where Category ID is 10

{
  products(filter: { category_id: { eq: "10" } }) {
    total_count
    aggregations{
      attribute_code
      label
      count
      options{
        count
        label
        value
      }
    }
  }
}

Response ::

{
  "data": {
    "products": {
      "total_count": 11,
      "aggregations": [
        {
          "attribute_code": "price",
          "label": "Price",
          "count": 5,
          "options": [
            {
              "count": 4,
              "label": "40-50",
              "value": "40_50"
            },
            {
              "count": 2,
              "label": "50-60",
              "value": "50_60"
            },
            {
              "count": 3,
              "label": "60-70",
              "value": "60_70"
            },
            {
              "count": 1,
              "label": "70-80",
              "value": "70_80"
            },
            {
              "count": 1,
              "label": "90-*",
              "value": "90_*"
            }
          ]
        },
        {
          "attribute_code": "category_id",
          "label": "Category",
          "count": 3,
          "options": [
            {
              "count": 11,
              "label": "Tops",
              "value": "12"
            },
            {
              "count": 11,
              "label": "Jackets",
              "value": "14"
            },
            {
              "count": 2,
              "label": "Eco Friendly",
              "value": "36"
            }
          ]
        },
        {
          "attribute_code": "climate",
          "label": "Climate",
          "count": 10,
          "options": [
            {
              "count": 10,
              "label": "Spring",
              "value": "207"
            },
            {
              "count": 9,
              "label": "Cool",
              "value": "203"
            },
            {
              "count": 9,
              "label": "Windy",
              "value": "209"
            },
            {
              "count": 5,
              "label": "All-Weather",
              "value": "201"
            },
            {
              "count": 5,
              "label": "Rainy",
              "value": "206"
            },
            {
              "count": 4,
              "label": "Mild",
              "value": "205"
            },
            {
              "count": 3,
              "label": "Wintry",
              "value": "210"
            },
            {
              "count": 2,
              "label": "Cold",
              "value": "202"
            },
            {
              "count": 2,
              "label": "Indoor",
              "value": "204"
            },
            {
              "count": 1,
              "label": "Warm",
              "value": "208"
            }
          ]
        },
        {
          "attribute_code": "color",
          "label": "Color",
          "count": 9,
          "options": [
            {
              "count": 6,
              "label": "Black",
              "value": "49"
            },
            {
              "count": 6,
              "label": "Red",
              "value": "58"
            },
            {
              "count": 5,
              "label": "Blue",
              "value": "50"
            },
            {
              "count": 5,
              "label": "Green",
              "value": "53"
            },
            {
              "count": 4,
              "label": "Orange",
              "value": "56"
            },
            {
              "count": 3,
              "label": "Yellow",
              "value": "60"
            },
            {
              "count": 2,
              "label": "Purple",
              "value": "57"
            },
            {
              "count": 1,
              "label": "Gray",
              "value": "52"
            },
            {
              "count": 1,
              "label": "White",
              "value": "59"
            }
          ]
        },
        {
          "attribute_code": "eco_collection",
          "label": "Eco Collection",
          "count": 1,
          "options": [
            {
              "count": 2,
              "label": "1",
              "value": "1"
            }
          ]
        },
        {
          "attribute_code": "erin_recommends",
          "label": "Erin Recommends",
          "count": 1,
          "options": [
            {
              "count": 2,
              "label": "1",
              "value": "1"
            }
          ]
        },
        {
          "attribute_code": "material",
          "label": "Material",
          "count": 10,
          "options": [
            {
              "count": 9,
              "label": "Polyester",
              "value": "38"
            },
            {
              "count": 5,
              "label": "Fleece",
              "value": "144"
            },
            {
              "count": 5,
              "label": "Nylon",
              "value": "37"
            },
            {
              "count": 2,
              "label": "Cocona® performance fabric",
              "value": "142"
            },
            {
              "count": 2,
              "label": "LumaTech™",
              "value": "147"
            },
            {
              "count": 1,
              "label": "Hemp",
              "value": "145"
            },
            {
              "count": 1,
              "label": "Lycra®",
              "value": "148"
            },
            {
              "count": 1,
              "label": "Spandex",
              "value": "150"
            },
            {
              "count": 1,
              "label": "Wool",
              "value": "158"
            },
            {
              "count": 1,
              "label": "Cotton",
              "value": "33"
            }
          ]
        },
        {
          "attribute_code": "new",
          "label": "New",
          "count": 1,
          "options": [
            {
              "count": 2,
              "label": "1",
              "value": "1"
            }
          ]
        },
        {
          "attribute_code": "pattern",
          "label": "Pattern",
          "count": 1,
          "options": [
            {
              "count": 11,
              "label": "Solid",
              "value": "196"
            }
          ]
        },
        {
          "attribute_code": "performance_fabric",
          "label": "Performance Fabric",
          "count": 1,
          "options": [
            {
              "count": 3,
              "label": "1",
              "value": "1"
            }
          ]
        },
        {
          "attribute_code": "sale",
          "label": "Sale",
          "count": 1,
          "options": [
            {
              "count": 2,
              "label": "1",
              "value": "1"
            }
          ]
        },
        {
          "attribute_code": "size",
          "label": "Size",
          "count": 5,
          "options": [
            {
              "count": 11,
              "label": "XS",
              "value": "166"
            },
            {
              "count": 11,
              "label": "S",
              "value": "167"
            },
            {
              "count": 11,
              "label": "M",
              "value": "168"
            },
            {
              "count": 11,
              "label": "L",
              "value": "169"
            },
            {
              "count": 11,
              "label": "XL",
              "value": "170"
            }
          ]
        },
        {
          "attribute_code": "style_general",
          "label": "Style General",
          "count": 12,
          "options": [
            {
              "count": 7,
              "label": "¼ zip",
              "value": "127"
            },
            {
              "count": 6,
              "label": "Lightweight",
              "value": "119"
            },
            {
              "count": 6,
              "label": "Windbreaker",
              "value": "125"
            },
            {
              "count": 5,
              "label": "Insulated",
              "value": "116"
            },
            {
              "count": 5,
              "label": "Rain Coat",
              "value": "122"
            },
            {
              "count": 5,
              "label": "Hard Shell",
              "value": "123"
            },
            {
              "count": 5,
              "label": "Soft Shell",
              "value": "124"
            },
            {
              "count": 5,
              "label": "Full Zip",
              "value": "128"
            },
            {
              "count": 3,
              "label": "Hooded",
              "value": "120"
            },
            {
              "count": 2,
              "label": "Heavy Duty",
              "value": "121"
            },
            {
              "count": 1,
              "label": "Jacket",
              "value": "117"
            },
            {
              "count": 1,
              "label": "Reversible",
              "value": "129"
            }
          ]
        }
      ]
    }

Below GraphQL query to get all Search Filter Data , where Category ID is 10 and Price range from 10 To 500 USD

{
      products(filter: { category_id: { in: "10" }, price: {from: "10" to: "500"} }) {


    total_count
    aggregations{
      attribute_code
      label
      count
      options{
        count
        label
        value
      }
    }
  }
}

Response::

{
  "data": {
    "products": {
      "total_count": 4,
      "aggregations": [
        {
          "attribute_code": "price",
          "label": "Price",
          "count": 2,
          "options": [
            {
              "count": 3,
              "label": "10-20",
              "value": "10_20"
            },
            {
              "count": 1,
              "label": "20-*",
              "value": "20_*"
            }
          ]
        },
        {
          "attribute_code": "category_id",
          "label": "Category",
          "count": 2,
          "options": [
            {
              "count": 4,
              "label": "Training",
              "value": "9"
            },
            {
              "count": 4,
              "label": "Video Download",
              "value": "10"
            }
          ]
        },
        {
          "attribute_code": "activity",
          "label": "Activity",
          "count": 5,
          "options": [
            {
              "count": 4,
              "label": "Athletic",
              "value": "16"
            },
            {
              "count": 3,
              "label": "Gym",
              "value": "11"
            },
            {
              "count": 2,
              "label": "Sports",
              "value": "17"
            },
            {
              "count": 2,
              "label": "Yoga",
              "value": "8"
            },
            {
              "count": 1,
              "label": "Outdoor",
              "value": "5"
            }
          ]
        },
        {
          "attribute_code": "format",
          "label": "Format",
          "count": 1,
          "options": [
            {
              "count": 4,
              "label": "Download",
              "value": "102"
            }
          ]
        }
      ]
    }
  }
}

Leave a Reply

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