graphql-engine: order_by aggregated fields don't use filter

Hello,

I’m having an issue while using order_by on aggregate fields.

{
  author (order_by: {articles_aggregate: { count: desc }}) {
    id
    name
    articles_aggregate(where: $where) {
        aggregate {
            count
        }
    }      
  }
}

It looks like the SQL query doesn’t apply the filter used in the aggregated count on the order_by count. Am I doing something wrong?

#1039 #1042

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 20
  • Comments: 22 (2 by maintainers)

Most upvoted comments

@rafaelugolini This is currently the expected behavior. The filters used on *_aggregate fields in the query are not applied at the top level. For example, you can have such a query:

{
  author (order_by: {articles_aggregate: { count: desc }}) {
    id
    name
    count1: articles_aggregate(where: $where1) {
        aggregate {
            count
        }
    }    
    count2: articles_aggregate(where: $where2) {
        aggregate {
            count
        }
    }      
  }
}

In this case, we wouldn’t know which filter to apply at the top level.

Your use case however is very valid. Maybe we can extend the order_by syntax to allow specifying the where clause?

{
  author (order_by: {articles_aggregate: { where: $where using: {count: desc} }) {
    id
    name
    articles_aggregate(where: $where) {
        aggregate {
            count
        }
    }      
  }
}

Any news on this ?

I am working with custom reports where the filters are quite dynamic and it is not possible to solve my case with views.

Having the possibility to add where filter to the order_by clause like @0x777 mentioned would solve this perfectly.

With all the amazing improvements in hasura recently, I find this to be the only area where hasura is still lacking.

Hi, any news, ETA on this? Hasura is terrific, but the lacks of this feature (as suggested from @0x777 would be great) breaks all our server side pagination/reorder, thus preventing us from be able to deploy with large datasets…

Thank you in advance

We need this feature. Anyone solved this? working on it? Thanks in advance

We could really do with this too

The solution proposed by @0x777 (https://github.com/hasura/graphql-engine/issues/1357#issuecomment-453004951) seems perfect.

Any updates on this?

Since #1498 has been solved, I am wondering if we might be seeing this one implemented as well ?

Really important for my use case.

Any update on this feature?

Any ETA on this? Would be extremely useful

Hi 0x777, cloud you kindly provide us some more information of hasura willingness to implement a solution like the one you suggested? It is really much need; without, most of the pagination logic is broken, and with large dataset it is an important problem. If there is something we have to pay to have this fix implemented OK, but please let us know… thank you!

@hoanv810 hey 😃 from what I remember we created a view

Hi @norwindijkman, we are are using a lot of postgres functions as a workaround for this specific issue but they are really unhandy… you must implement all the filtering and ordering logic from scratch, and keep it updated each time you need a new filter etc.

Most of our postgres functions wouldn’t be necessary with this feature available, letting us use hasura at full power with native queries…

@0x777 that would work 👍

btw thanks for the awesome response time, I ❤️ hasura