4c7e8444cf
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).
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import gql from 'graphql-tag';
|
|
import { capitalize } from 'twenty-shared/utils';
|
|
|
|
type GroupByOperationFactoryParams = {
|
|
objectMetadataSingularName: string;
|
|
objectMetadataPluralName: string;
|
|
groupBy: object[];
|
|
filter?: object;
|
|
orderBy?: object[];
|
|
viewId?: string;
|
|
gqlFields?: string;
|
|
};
|
|
|
|
export const groupByOperationFactory = ({
|
|
objectMetadataSingularName,
|
|
objectMetadataPluralName,
|
|
groupBy,
|
|
filter = {},
|
|
orderBy = [],
|
|
viewId,
|
|
gqlFields,
|
|
}: GroupByOperationFactoryParams) => ({
|
|
query: gql`
|
|
query ${capitalize(objectMetadataPluralName)}GroupBy($groupBy: [${capitalize(objectMetadataSingularName)}GroupByInput!]!, $filter: ${capitalize(objectMetadataSingularName)}FilterInput, $orderBy: [${capitalize(objectMetadataSingularName)}OrderByWithGroupByInput!], $viewId: UUID) {
|
|
${objectMetadataPluralName}GroupBy(groupBy: $groupBy, filter: $filter, orderBy: $orderBy, viewId: $viewId) {
|
|
${gqlFields ? gqlFields : ''}
|
|
groupByDimensionValues
|
|
totalCount
|
|
}
|
|
}
|
|
`,
|
|
variables: {
|
|
groupBy,
|
|
filter,
|
|
orderBy,
|
|
...(viewId && { viewId }),
|
|
},
|
|
});
|