graphql-engine icon indicating copy to clipboard operation
graphql-engine copied to clipboard

order_by aggregated fields don't use filter

Open rafaelugolini opened this issue 6 years ago • 20 comments

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

rafaelugolini avatar Jan 09 '19 21:01 rafaelugolini

@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
        }
    }      
  }
}

0x777 avatar Jan 10 '19 08:01 0x777

@0x777 that would work 👍

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

rafaelugolini avatar Jan 10 '19 08:01 rafaelugolini

hi @rafaelugolini : I'm facing the same issue like yours. How did you solve that?

hoanv810 avatar Jul 17 '19 08:07 hoanv810

@hoanv810 hey :) from what I remember we created a view

rafaelugolini avatar Jul 17 '19 11:07 rafaelugolini

This is definitely a feature we could use. Hopefully once 1.0 is out things like this can get looked at for a future roadmap.

hastarin avatar Jul 18 '19 01:07 hastarin

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.

karibertils avatar Oct 21 '19 13:10 karibertils

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

manuFL avatar Dec 02 '20 11:12 manuFL

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

korengrip avatar Mar 15 '21 09:03 korengrip

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

karibertils avatar Mar 15 '21 14:03 karibertils

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!

manuFL avatar May 21 '21 10:05 manuFL

@manuFL Couln't you solve your problem with a Postgres function? Just pass the $where variable to that function, and let it handle the return order.

norwindijkman avatar Jun 11 '21 14:06 norwindijkman

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...

manuFL avatar Jun 14 '21 11:06 manuFL

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

Any updates on this?

ananni13 avatar Jun 20 '21 16:06 ananni13

We could really do with this too

prag-matt-ic avatar Jul 23 '21 16:07 prag-matt-ic

Ran into this as well!

RodriguezLucha avatar Nov 17 '21 19:11 RodriguezLucha

Any ETA on this? Would be extremely useful

austin-coffman avatar Jan 13 '22 15:01 austin-coffman

Any update on this feature?

akhilrajvc avatar Mar 02 '22 04:03 akhilrajvc

Is this anywhere on the roadmap (3 years later)? This is a crucially missing piece of the puzzle. This would solve it: https://github.com/hasura/graphql-engine/issues/7473

rccoe avatar Jun 17 '22 21:06 rccoe

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

Really important for my use case.

karibertils avatar Oct 04 '22 18:10 karibertils

Any updates here? Would be very helpful for my project too. Currently, I have to create two different views just as a workaround for this issue.

RayyanRiaz avatar Oct 09 '22 18:10 RayyanRiaz

Any updates?

swtmply avatar May 16 '23 16:05 swtmply

Hey folks. As a little sneak preview: Native Queries will be out of beta and available in Hasura CE for PostgreSQL with the release of v2.28, which is imminent. Native Queries allow you to express your query as arbitrary SQL, while still allowing you to extend them with filtering, ordering, etc. on the Hasura side.

This means you can work around this issue by writing the query, which might look something like this:

SELECT author.id, author.name, count(article.id) AS article_count
FROM author
JOIN article ON author.id = article.author_id
WHERE <your where clause>
GROUP BY author.id, author.name
ORDER BY count(article.id) DESC

Then you can query as usual with Hasura, and apply successive filters in GraphQL which will not impact the article count.

SamirTalwar avatar Jun 20 '23 09:06 SamirTalwar