How To Get Category List Data Using GraphQL in Magento 2.x

The following below Magento 2.x GraphQL Query to get Category List Data

categoryList (
   filters: CategoryFilterInput
): CategoryTree

This Query supports following below Query

  1. By Using Category ID
  2. By Using Category Name
  3. By Using URL Key
  4. By Using URL Path
  1. By Using Category ID
{
  categoryList(filters: {ids: {in: ["3", "20"]}}) {
    children_count
    children {
      id
      level
      name
      path
      url_path
      url_key
      image
      description
      children {
        id
        level
        name
        path
        url_path
        url_key
        image
        description
      }
    }
  }
}

Response::

{
  "data": {
    "categoryList": [
      {
        "children_count": "3",
        "children": [
          {
            "id": 4,
            "level": 3,
            "name": "Bags",
            "path": "1/2/3/4",
            "url_path": "gear/bags",
            "url_key": "bags",
            "image": null,
            "description": null,
            "children": []
          },
          {
            "id": 5,
            "level": 3,
            "name": "Fitness Equipment",
            "path": "1/2/3/5",
            "url_path": "gear/fitness-equipment",
            "url_key": "fitness-equipment",
            "image": null,
            "description": null,
            "children": []
          },
          {
            "id": 6,
            "level": 3,
            "name": "Watches",
            "path": "1/2/3/6",
            "url_path": "gear/watches",
            "url_key": "watches",
            "image": null,
            "description": null,
            "children": []
          }
        ]
      },
      {
        "children_count": "8",
        "children": [
          {
            "id": 22,
            "level": 3,
            "name": "Bottoms",
            "path": "1/2/20/22",
            "url_path": "women/bottoms-women",
            "url_key": "bottoms-women",
            "image": null,
            "description": null,
            "children": [
              {
                "id": 27,
                "level": 4,
                "name": "Pants",
                "path": "1/2/20/22/27",
                "url_path": "women/bottoms-women/pants-women",
                "url_key": "pants-women",
                "image": null,
                "description": null
              },
              {
                "id": 28,
                "level": 4,
                "name": "Shorts",
                "path": "1/2/20/22/28",
                "url_path": "women/bottoms-women/shorts-women",
                "url_key": "shorts-women",
                "image": null,
                "description": null
              }
            ]
          },
          {
            "id": 21,
            "level": 3,
            "name": "Tops",
            "path": "1/2/20/21",
            "url_path": "women/tops-women",
            "url_key": "tops-women",
            "image": null,
            "description": null,
            "children": [
              {
                "id": 23,
                "level": 4,
                "name": "Jackets",
                "path": "1/2/20/21/23",
                "url_path": "women/tops-women/jackets-women",
                "url_key": "jackets-women",
                "image": null,
                "description": null
              },
              {
                "id": 24,
                "level": 4,
                "name": "Hoodies & Sweatshirts",
                "path": "1/2/20/21/24",
                "url_path": "women/tops-women/hoodies-and-sweatshirts-women",
                "url_key": "hoodies-and-sweatshirts-women",
                "image": null,
                "description": null
              },
              {
                "id": 25,
                "level": 4,
                "name": "Tees",
                "path": "1/2/20/21/25",
                "url_path": "women/tops-women/tees-women",
                "url_key": "tees-women",
                "image": null,
                "description": null
              },
              {
                "id": 26,
                "level": 4,
                "name": "Bras & Tanks",
                "path": "1/2/20/21/26",
                "url_path": "women/tops-women/tanks-women",
                "url_key": "tanks-women",
                "image": null,
                "description": null
              }
            ]
          }
        ]
      }
    ]

[2] – By Using Category Name

{
	categoryList(filters: { name: { match: "Gear" } }) {
	products {
	total_count
		page_info {
		current_page
		page_size
		}
	 }
	children_count
		children {
		id
		level
		name
		path
			children {
			id
			level
			name
			path
				children {
				id
				level
				name
				path
				children {
				id
				level
				name
				path
				}
				}
			}
		}
	}
}

Response ::

{
  "data": {
    "categoryList": [
      {
        "products": {
          "total_count": 34,
          "page_info": {
            "current_page": 1,
            "page_size": 20
          }
        },
        "children_count": "3",
        "children": [
          {
            "id": 4,
            "level": 3,
            "name": "Bags",
            "path": "1/2/3/4",
            "children": []
          },
          {
            "id": 5,
            "level": 3,
            "name": "Fitness Equipment",
            "path": "1/2/3/5",
            "children": []
          },
          {
            "id": 6,
            "level": 3,
            "name": "Watches",
            "path": "1/2/3/6",
            "children": []
          }
        ]
      }
    ]
  }
}

By Using Root Category 2

{
	category(id: 2) {
	products {
	total_count
		page_info {
		current_page
		page_size
		}
	 }
	children_count
		children {
		id
		level
		name
		path
			children {
			id
			level
			name
			path
				children {
				id
				level
				name
				path
				children {
				id
				level
				name
				path
				}
				}
			}
		}
	}
}

Above Query Response :: All List of Categories & Sub Categories Data

{
  "data": {
    "category": {
      "products": {
        "total_count": 187,
        "page_info": {
          "current_page": 1,
          "page_size": 20
        }
      },
      "children_count": "38",
      "children": [
        {
          "id": 20,
          "level": 2,
          "name": "Women",
          "path": "1/2/20",
          "children": [
            {
              "id": 22,
              "level": 3,
              "name": "Bottoms",
              "path": "1/2/20/22",
              "children": [
                {
                  "id": 27,
                  "level": 4,
                  "name": "Pants",
                  "path": "1/2/20/22/27",
                  "children": []
                },
                {
                  "id": 28,
                  "level": 4,
                  "name": "Shorts",
                  "path": "1/2/20/22/28",
                  "children": []
                }
              ]
            },
            {
              "id": 21,
              "level": 3,
              "name": "Tops",
              "path": "1/2/20/21",
              "children": [
                {
                  "id": 23,
                  "level": 4,
                  "name": "Jackets",
                  "path": "1/2/20/21/23",
                  "children": []
                },
                {
                  "id": 24,
                  "level": 4,
                  "name": "Hoodies & Sweatshirts",
                  "path": "1/2/20/21/24",
                  "children": []
                },
                {
                  "id": 25,
                  "level": 4,
                  "name": "Tees",
                  "path": "1/2/20/21/25",
                  "children": []
                },
                {
                  "id": 26,
                  "level": 4,
                  "name": "Bras & Tanks",
                  "path": "1/2/20/21/26",
                  "children": []
                }
              ]
            }
          ]
        },
        {
          "id": 11,
          "level": 2,
          "name": "Men",
          "path": "1/2/11",
          "children": [
            {
              "id": 12,
              "level": 3,
              "name": "Tops",
              "path": "1/2/11/12",
              "children": [
                {
                  "id": 14,
                  "level": 4,
                  "name": "Jackets",
                  "path": "1/2/11/12/14",
                  "children": []
                },
                {
                  "id": 15,
                  "level": 4,
                  "name": "Hoodies & Sweatshirts",
                  "path": "1/2/11/12/15",
                  "children": []
                },
                {
                  "id": 16,
                  "level": 4,
                  "name": "Tees",
                  "path": "1/2/11/12/16",
                  "children": []
                },
                {
                  "id": 17,
                  "level": 4,
                  "name": "Tanks",
                  "path": "1/2/11/12/17",
                  "children": []
                }
              ]
            },
            {
              "id": 13,
              "level": 3,
              "name": "Bottoms",
              "path": "1/2/11/13",
              "children": [
                {
                  "id": 18,
                  "level": 4,
                  "name": "Pants",
                  "path": "1/2/11/13/18",
                  "children": []
                },
                {
                  "id": 19,
                  "level": 4,
                  "name": "Shorts",
                  "path": "1/2/11/13/19",
                  "children": []
                }
              ]
            }
          ]
        },
        {
          "id": 9,
          "level": 2,
          "name": "Training",
          "path": "1/2/9",
          "children": [
            {
              "id": 10,
              "level": 3,
              "name": "Video Download",
              "path": "1/2/9/10",
              "children": []
            }
          ]
        },
        {
          "id": 3,
          "level": 2,
          "name": "Gear",
          "path": "1/2/3",
          "children": [
            {
              "id": 4,
              "level": 3,
              "name": "Bags",
              "path": "1/2/3/4",
              "children": []
            },
            {
              "id": 5,
              "level": 3,
              "name": "Fitness Equipment",
              "path": "1/2/3/5",
              "children": []
            },
            {
              "id": 6,
              "level": 3,
              "name": "Watches",
              "path": "1/2/3/6",
              "children": []
            }
          ]
        },
        {
          "id": 38,
          "level": 2,
          "name": "What's New",
          "path": "1/2/38",
          "children": []
        },
        {
          "id": 37,
          "level": 2,
          "name": "Sale",
          "path": "1/2/37",
          "children": []
        }
      ]
    }
  }
}

Leave a Reply

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