I already checked Using custom DQL with groupby but copying what they did didn’t work.
I’m trying to get unique results with the count on fields of the Stems type, like Stems.brand, Stems.color, and so on.
I have this graphQL schema,
type Stems {
id: ID!
link: String!
image: String!
name: String! @search(by:[fulltext])
length: Int @search
rise: Int @search
clampDiameter: Float @search
steererDiameter: String @search(by:[hash])
color: String @search(by:[hash])
material: String! @search(by:[hash])
price: Float! @search
weight: Int @search
brand: String! @search(by:[hash])
}
I can do what I want using this DQL query in the DQL section of Dgraph Cloud:
{
distinctBrands(func: type(Stems)) @groupby(Stems.brand) {
count(uid)
}
}
Returns:
{
"data": {
"distinctBrands": [
{
"@groupby": [
{
"Stems.brand": "Thomson",
"count": 1
},
{
"Stems.brand": "Easton",
"count": 2
},
{
"Stems.brand": "FSA",
"count": 2
},
{
"Stems.brand": "Syntace",
"count": 2
},
{
"Stems.brand": "OneUp Components",
"count": 3
},
{
"Stems.brand": "TruVativ",
"count": 3
},
}
So I think what I’m supposed to do to get this to work in my Angular app that runs on web browsers, is to make a custom DQL query.
I have added this to the schema:
type BrandCount @remote {
brand: String
count: Int
}
type Query {
distinctBrands: [BrandCount] @custom(dql: """
query {
distinctBrands(func: type(Stems)) @groupby(Stems.brand) {
count(uid)
}
}
""")
}
but when I query like this
query MyQuery {
distinctBrands {
brand
count
}
}
I get the following
"data": {
"distinctBrands": [
{
"brand": null,
"count": null
}
]
},
Please help, been trying to do this for a long time.
Another thing is that adding “orderasc” makes it only return one group
{
distinctBrands(func: type(Stems), orderasc: Stems.brand) @groupby(Stems.brand) {
count(uid)
}
}
Returns:
{
"distinctBrands": [
{
"@groupby": [
{
"Stems.brand": "TruVativ",
"count": 1
}
]
}
]
}
Context:
TL:DR Is there a way to do this such that the groupby returns groups with counts of zero?
I want the unique fields and counts because I’m making a table with filter checkboxes, and they should have a count of how many items that would remain if you were to click that filter checkbox. The count that is returned by the DQL query is exactly what I need. The only issue aside from not being able to get this to work as a custom DQL query is the fact that if I add filtering to this query, then if the count on a group is zero, that group is not returned at all.