Recursive query with aggregation

Hello.

I have the following structs that I use to mutate data to Dgraph (the Category struct is used for product categories e their children, product subcategories):

// Categories and subcategories
type Category struct {
	Uid              string     `json:"uid,omitempty"`
	Name             string     `json:"categoryName,omitempty"`
	Image            string     `json:"categoryImage,omitempty"`
	Featured         *bool      `json:"categoryFeatured,omitempty"`
	HasSubcategories *bool      `json:"categoryHasSubcategories,omitempty"`
	Subcategories    []Category `json:"categorySubcategories,omitempty"`
	HasProducts      *bool      `json:"categoryHasProducts,omitempty"`
	ProductsCount    *int       `json:"categoryProductsCount,omitempty"`
	Products         []Product  `json:"categoryProducts,omitempty"`
}

// Products
type Product struct {
	Uid             string     `json:"uid,omitempty"`
	Name            string     `json:"productName,omitempty"`
	Image           string     `json:"productImage,omitempty"`
	EAN             string     `json:"productEAN,omitempty"`
	Dimension       *Dimension `json:"productDimension,omitempty"`
	Prices          []Price    `json:"productPrices,omitempty"`
	RelatedProducts []Product  `json:"productRelatedProducts,omitempty"`
}

And I have the following query to get the number of products for each category/subcategory, what would be used to fill the ProductsCount property value, that I use to avoid execute a query every time I list the categories/subcategories.

{
	categories(func: has(categoryName @.)) @normalize {
		id: uid
		name : categoryName@.
		categorySubcategories {
			preProductsCount1 as count(categoryProducts)
			productsCount2Sum as sum(val(productsCount2))
			productsCount1 as math(preProductsCount1+productsCount2Sum)
			categorySubcategories {
				preProductsCount2 as count(categoryProducts)
				productsCount3Sum as sum(val(productsCount3))
				productsCount2 as math(preProductsCount2+productsCount3Sum)
				categorySubcategories {
					preProductsCount3 as count(categoryProducts)
					productsCount4Sum as sum(val(productsCount4))
					productsCount3 as math(preProductsCount3+productsCount4Sum)
					categorySubcategories {
						preProductsCount4 as count(categoryProducts)
						productsCount5Sum as sum(val(productsCount5))
						productsCount4 as math(preProductsCount4+productsCount5Sum)
						categorySubcategories {
							productsCount5 as count(categoryProducts)
						}
					}
				}
			}
		}
		preProductsCount as count(categoryProducts)
		productsCount1Sum as sum(val(productsCount1))
		productsCount : math(preProductsCount+productsCount1Sum)
	}
}

In this query, I have fixed 5 levels of subcategories. I would like to know if there is a way to have a unlimites number of levels of subcategories. I tried the following query, but it gives me the following error:

: Attribute “val” is not scalar-type

I figured out that is because the productsCount is of list type. Here is the query that throws this error:

{
	categories(func: has(categoryName @.)) @recurse(loop: false) {
		id: uid
    name : categoryName@.
		categorySubcategories {
			productsCount as count(categoryProducts)
		}
		productsCount: sum(val(productsCount))
	}
}

For a recurse query you can only have the have predicates which have children, so categorySubcategories cannot have children. It’s not possible to do aggregation with @recurse for now.

Though what you have is an interesting special use case and we might support it at some point.

Thanks Pawan!

It would be a very good feature.