graphile / pg-aggregates

Aggregates for PostGraphile connections
83 stars 17 forks source link

Order by totalCount with per-query custom filtering applied #36

Closed jiqiang90 closed 1 year ago

jiqiang90 commented 1 year ago

Summary

Allow order by with totalCount

Additional context

I think this will expand on this issue https://github.com/graphile/pg-aggregates/issues/25

Currently we can filter with aggregate result, but not allow output to order by it.

For example, we have schema as follow:

type Author @entity{
  id:ID!
}

type Book @entity{
  id:ID!
  author: Author! //Fk author_id
  year: Int!
}

And we want to sort authors by the amount of books written after a certain year.

With pg-aggregates and connection-filter I could query list of authors and books written after year 1995,

However, the BOOKS_COUNT_DESC does not order by filtered result, it counts all books belong to author and order desc.


query {
  authors(
    orderBy: BOOKS_COUNT_DESC
    filter: {
      books: { 
        aggregates: { 
          distinctCount: { id: { greaterThan:"0" } } } 
      }
    }
  ) {
    nodes {
      id
      books(filter:{year:{greaterThanOrEqualTo:1995}}){
        totalCount
        nodes{
          id,
          year
        }
      }
    }
  }
}

Is there a generic solution for this? Rather than something like create additional plugin and make sortable on compute column. Thank you !

benjie commented 1 year ago

I don't think so.

benjie commented 1 year ago

[semi-automated message] Thanks for your question. This doesn't currently seem to be a bug in the library so I'm going to close the issue, but please feel free to keep requesting help below and if it does turn out to be a bug we can definitely re-open it 👍

You can also ask for help in the #help-and-support channel in our Discord chat.