We can get the category and keywords list using GraphiQL APIs in many ways.
Option 1:
This approach retrieves all categories and keywords in a single query, providing a comprehensive view of the taxonomy hierarchy. However, be cautious about the potential performance impact, especially in environments with a large number of categories and keywords.
{
categories(namespaceId: 1, publicationId: 10) {
edges {
node {
id
itemType
itemId
description
key
title
totalRelatedItems
children {
edges {
node {
...KeywordFieldsLevel1
children {
edges {
node {
...KeywordFieldsLevel1
}
}
}
}
}
}
}
}
}
}
fragment KeywordFieldsLevel2 on Keyword {
title
description
itemId
key
hasChildren
}
fragment KeywordFieldsLevel1 on Keyword {
title
key
description
hasChildren
itemId
children {
edges {
node {
...KeywordFieldsLevel2
}
}
}
}
Option 2: The advantage of using this approach it is more targeted, allowing you to fetch specific categories and their first-level keywords individually. This can improve performance when you only need information about a subset of the taxonomy.
query GetCountryKeywords {
k1: keyword(namespaceId: 1, publicationId: 10, categoryId: 46, keywordId: 1066) {
...ParentKeyword
}
k2: keyword(namespaceId: 1, publicationId: 10, categoryId: 46, keywordId: 2430) {
...ParentKeyword
}
k3: keyword(namespaceId: 1, publicationId: 10, categoryId: 46, keywordId: 2433) {
...ParentKeyword
}
k4: keyword(namespaceId: 1, publicationId: 10, categoryId: 46, keywordId: 2446) {
...ParentKeyword
}
k5: keyword(namespaceId: 1, publicationId: 10, categoryId: 46, keywordId: 2449) {
...ParentKeyword
}
}
fragment ParentKeyword on Keyword {
id
itemType
itemId
description
key
title
hasChildren
children {
edges {
node {
...KeywordFieldsLevel1
children {
edges {
node {
...KeywordFieldsLevel1
}
}
}
}
}
}
}
fragment KeywordFieldsLevel2 on Keyword {
title
description
itemId
key
hasChildren
customMetas {
edges {
node {
key
value
}
}
}
}
fragment KeywordFieldsLevel1 on Keyword {
title
key
description
hasChildren
itemId
children {
edges {
node {
...KeywordFieldsLevel2
}
}
}
customMetas {
edges {
node {
key
value
}
}
}
}
I hope it helps.