Skip to content

[groupBy] Fix order by nested date field#16135

Merged
ijreilly merged 2 commits intomainfrom
fix--group-by-order-by-related-date
Nov 28, 2025
Merged

[groupBy] Fix order by nested date field#16135
ijreilly merged 2 commits intomainfrom
fix--group-by-order-by-related-date

Conversation

@ijreilly
Copy link
Copy Markdown
Contributor

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Nov 27, 2025

Greptile Overview

Greptile Summary

This PR fixes ordering by nested date fields when using groupBy functionality. The core issue was that when ordering by a relation's date field in a grouped query, the system was using the regular OrderBy input type instead of the OrderByWithGroupBy input type, which supports date granularity options.

Key Changes:

  • Renamed orderByDateGranularity parameter to the more semantic isForGroupBy across the order-by generation chain
  • Modified RelationFieldMetadataGqlInputTypeGenerator.generateSimpleRelationFieldOrderByInputType() to accept isForGroupBy parameter and select the appropriate input type (OrderByWithGroupBy vs OrderBy) for nested relation fields
  • Updated import statements to use internal UserInputError from src/engine/core-modules/graphql/utils/graphql-errors.util instead of apollo-server-core

The fix ensures that when ordering by date fields in nested relations within grouped queries, the correct GraphQL input type with date granularity support is used.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The changes are well-structured and focused on fixing a specific bug. The parameter rename improves code clarity, and the logic correctly propagates the isForGroupBy flag to select the appropriate GraphQL input type for nested relation fields. The import updates align with internal error handling patterns. No breaking changes or security concerns detected.
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
packages/twenty-server/src/engine/api/graphql/workspace-schema-builder/graphql-type-generators/input-types/order-by-input/object-metadata-order-by-base.generator.ts 5/5 Renamed parameter from orderByDateGranularity to isForGroupBy for clarity and passed to relation field generator
packages/twenty-server/src/engine/api/graphql/workspace-schema-builder/graphql-type-generators/input-types/relation-field-metadata-gql-type.generator.ts 4/5 Added isForGroupBy parameter to properly select OrderByWithGroupBy or OrderBy input type for nested relation fields

Sequence Diagram

sequenceDiagram
    participant Client
    participant Schema as Workspace Schema Builder
    participant OrderByBase as OrderByBaseGenerator
    participant RelationGen as RelationFieldMetadataGqlInputTypeGenerator
    participant Parser as GraphQL Query Parser
    
    Note over Schema,OrderByBase: Schema Generation Phase
    Client->>Schema: Build OrderByWithGroupBy Input Type
    Schema->>OrderByBase: generateFields(isForGroupBy=true)
    OrderByBase->>OrderByBase: Process atomic & composite fields
    OrderByBase->>RelationGen: generateSimpleRelationFieldOrderByInputType(isForGroupBy=true)
    RelationGen->>RelationGen: Check if isForGroupBy flag is set
    alt isForGroupBy is true
        RelationGen->>RelationGen: Use OrderByWithGroupBy input type
        Note right of RelationGen: Supports date granularity for nested date fields
    else isForGroupBy is false
        RelationGen->>RelationGen: Use regular OrderBy input type
    end
    RelationGen-->>OrderByBase: Return relation order input fields
    OrderByBase-->>Schema: Return all generated fields
    
    Note over Parser: Query Execution Phase
    Client->>Parser: Execute query with orderBy on nested date field
    Parser->>Parser: prepareForOrderByRelationFieldParsing()
    Parser->>Parser: Validate nested field is in groupBy
    Parser->>Parser: Apply correct order with date granularity
Loading

Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Nov 27, 2025

🚀 Preview Environment Ready!

Your preview environment is available at: http://bore.pub:32978

This environment will automatically shut down when the PR is closed or after 5 hours.

Copy link
Copy Markdown
Member

@Weiko Weiko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ijreilly ijreilly enabled auto-merge (squash) November 28, 2025 08:59
@ijreilly ijreilly merged commit fd9ea2f into main Nov 28, 2025
53 checks passed
@ijreilly ijreilly deleted the fix--group-by-order-by-related-date branch November 28, 2025 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ordering group-by results on relation date fields fails because backend doesn’t support granularity-aware order for relations

2 participants