Inter-groups orderBy on groupByDimension values (#15024)

Closes https://github.com/twentyhq/core-team-issues/issues/1563.

We now have two inter-group orderBy criteria: one on the aggregate
values, the other on the dimension values.
Ex: I am grouping companies by city and querying the average number of
employees for each group. I can either order the groups by the city
name, or by the average number of employees, or by one then the other.
I could actually also order groups by an aggregated value that I did not
ask for (ex: average ARR), but I cannot order groups by a field value I
did not group records by (ex: country).
[See discord
discussion](https://discord.com/channels/1130383047699738754/1425438213555753050/1425438223097921659)

An example of query variables: 
```
{
  "groupBy": [
    {
      "createdAt": {
        "granularity": "QUARTER_OF_THE_YEAR"
      }
    },
    {"city": true}
  ],
  "orderBy": [
    { 
      "aggregate": {
        "avgEmployees": "DescNullsLast"
      }
    },
    { 
      "aggregate": {
        "percentyEmptyEmployees": "DescNullsLast"
      }
    },
    {
      "city": "AscNullsLast"
    },
    {
      "createdAt": {
        "orderBy": "AscNullsLast",
        "granularity": "QUARTER_OF_THE_YEAR",
      }
    }
  ]
}
```

The aggregate orderBy criteria had already been implemented, but I
updated the implementation to add an "aggregate" key to prefix them, in
order to avoid confusion between the two + for the schema generation not
to break (otherwise we would have an issue when generating
OrderByWithGroupByInput if a user has created a field that has the same
name as an aggregate field, such as avgEmployees).
This commit is contained in:
Marie
2025-10-13 11:51:42 +02:00
committed by GitHub
parent 168e7b16ec
commit 4c7e8444cf
15 changed files with 1386 additions and 292 deletions
@@ -5,6 +5,7 @@ import {
type ObjectRecordFilter,
type ObjectRecordGroupBy,
type ObjectRecordOrderBy,
type OrderByWithGroupBy,
} from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface';
import { type workspaceResolverBuilderMethodNames } from 'src/engine/api/graphql/workspace-resolver-builder/factories/factories';
@@ -70,7 +71,7 @@ export interface GroupByResolverArgs<Filter = ObjectRecordFilter> {
filter?: Filter;
groupBy: ObjectRecordGroupBy;
viewId?: string;
orderBy?: ObjectRecordOrderBy;
orderBy?: OrderByWithGroupBy;
}
export interface UpdateOneResolverArgs<