Magento 2 How to get Category List in GraphQL
In this artical we will show you how to get category list. This query searches for categories that match the criteria specified in filters, which allowed search by category ID only. If you don't provide any filter input , the query returns Root Category
. To return multiple category levels in a single call. Category is deprecated. Use “CategoryList” query instead of category.
This query supports following types of filters. You can specify multiple filters in a query.
- Category ID
- Category Name
- URL Key
- URL Path
Use the breadcrumbs attribute to return information about the parent categories of the queried category.
Syntax
categoryList (
filters: CategoryFilterInput
): CategoryTree
The following query returns information about category IDs 3 and 20 two levels of subcategories.
Request
saveCopyzoom_out_map{
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
saveCopyzoom_out_map{
"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
}
]
}
]
}
]
}
}
That's it!!
If you are looking for more GraphQL solutions then check out our another article which is List of GraphQL Queries and Mutations for Magento 2.
I hope this article helps you to find what you were looking for.
Bookmark it for your future reference. Do comment below if you have any other questions on that.
P.S. Do share this article with your team.