-
-
Notifications
You must be signed in to change notification settings - Fork 593
feat(schema): add lexicographic schema sorting option #3149 #4006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Reviewer's GuideThis PR introduces a new boolean sort_schema option in StrawberryConfig and updates the print_schema function to invoke GraphQL Core’s lexicographic_sort_schema when enabled; accompanying documentation and release notes are updated, a comprehensive test suite is added, and the pre-commit tool is included in development dependencies. File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the Here's a preview of the changelog: This release adds support for lexicographic (alphabetical) sorting of GraphQL Added a Usage Example: import strawberry
from strawberry.schema.config import StrawberryConfig
@strawberry.type
class Query:
user_by_name: str
user_by_id: str
user_by_email: str
# Enable schema sorting
schema = strawberry.Schema(query=Query, config=StrawberryConfig(sort_schema=True))
# The printed schema will now have fields sorted alphabetically:
# type Query {
# userByEmail: String!
# userById: String!
# userByName: String!
# } Features:
This makes GraphQL introspection UIs (typically at Here's the tweet text:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Greptile Overview
Summary
This PR introduces a new `sort_schema` configuration option to `StrawberryConfig` that enables lexicographic (alphabetical) sorting of GraphQL schema elements. The feature addresses a common developer pain point with GraphQL introspection UIs where related fields (like `userById`, `userByEmail`, `userByName`) are scattered throughout the interface based on their definition order in code rather than being grouped alphabetically for easier navigation.The implementation leverages GraphQL Core's built-in lexicographic_sort_schema
function to sort all schema elements including types, fields, enums, interfaces, unions, and more. The feature is disabled by default (sort_schema: bool = False
) to maintain backward compatibility, allowing existing schemas to continue working unchanged while giving developers the option to enable alphabetical sorting when desired.
The changes integrate cleanly with Strawberry's existing configuration system and schema printing functionality. When enabled, the sorting is applied during the schema printing phase in the print_schema
function, ensuring it only affects the printed representation without changing runtime behavior or execution semantics. The feature works seamlessly with other configuration options like auto_camel_case
and supports all GraphQL constructs including mutations, subscriptions, interfaces, and unions.
Important Files Changed
Changed Files
Filename | Score | Overview |
---|---|---|
strawberry/schema/config.py | 5/5 | Added sort_schema: bool = False configuration option to StrawberryConfig class |
strawberry/printer/printer.py | 5/5 | Imported lexicographic_sort_schema and applied sorting when config option is enabled |
pyproject.toml | 5/5 | Added pre-commit = "^4.3.0" development dependency for code quality tooling |
tests/test_printer/test_sort_schema.py | 5/5 | Comprehensive test suite validating schema sorting across all GraphQL constructs |
RELEASE.md | 5/5 | Well-structured release notes documenting the new feature with examples |
docs/types/schema-configurations.md | 5/5 | Added thorough documentation for the sort_schema configuration option |
Confidence score: 5/5
- This PR is safe to merge with minimal risk as it adds an optional feature with comprehensive testing
- Score reflects well-tested implementation using GraphQL Core's standard functionality and backward compatibility preservation
- No files require special attention as all changes follow established patterns and include proper testing
Sequence Diagram
sequenceDiagram
participant User
participant StrawberrySchema
participant StrawberryConfig
participant Printer
participant GraphQLCore
User->>StrawberryConfig: "Create config with sort_schema=True"
User->>StrawberrySchema: "Create schema with config"
StrawberrySchema->>StrawberryConfig: "Store configuration"
User->>Printer: "Call print_schema(schema)"
Printer->>StrawberrySchema: "Get _schema (GraphQL Core schema)"
Printer->>StrawberryConfig: "Check config.sort_schema"
alt sort_schema is True
Printer->>GraphQLCore: "Call lexicographic_sort_schema(schema)"
GraphQLCore-->>Printer: "Return sorted schema"
else sort_schema is False
Printer->>Printer: "Use original schema order"
end
Printer->>Printer: "Generate schema string with sorted types and fields"
Printer-->>User: "Return formatted GraphQL schema string"
Context used:
Context from dashboard
- When documenting features, ensure that explanations are clear and provide context on why certain fun... (source)
6 files reviewed, 1 comment
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks dope!
I wonder if we should make sorted schemas by default? and add "breaking change" notice for people?
I like the idea of doing the right thing by default, but maybe it's not that big of a deal in this case?
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #4006 +/- ##
==========================================
+ Coverage 94.24% 94.27% +0.02%
==========================================
Files 531 532 +1
Lines 34829 34998 +169
Branches 1847 1848 +1
==========================================
+ Hits 32826 32993 +167
- Misses 1698 1701 +3
+ Partials 305 304 -1 🚀 New features to boost your workflow:
|
CodSpeed Performance ReportMerging #4006 will not alter performanceComparing Summary
|
thanks for the feedback!! I agree on making sorted schemas as default since it makes most sense. I am ready to tweak my PR depending on what your team decides. |
@Nalin7parihar let's make it the default! |
Sounds Great! ill start working on my PR but do I need to do any doc changes along with this? Please let me know. |
Kinda. It would be nice if you updated the new docs from your PR to mention that lexicographic sorting is the default. It would also be nice to have a new breaking change entry in this directory: https://github.com/strawberry-graphql/strawberry/tree/main/docs/breaking-changes |
Hey! Quick question before I finalize the PR: While implementing the default sorting change, I noticed that lexicographic_sort_schema from graphql-core seems to strip directives applied to enum values. Test test_print_directive_on_enum_value was failing because the @sensitive directive completely disappeared when sorting was enabled. I fixed it by adding sort_schema=False to that specific test. Is this a known limitation? Should I document this caveat anywhere, or is it rare enough to ignore? |
that feels like a bug? 👀 Maybe we can fix it in graphql-core itself /cc @erikwrede |
Description
This PR adds support for lexicographic (alphabetical) sorting of GraphQL schema fields through a new
sort_schema
configuration option inStrawberryConfig
.By default, Strawberry generates GraphQL schemas with fields in the order they're defined in code. This makes introspection UIs (typically at
/graphql
) difficult to navigate because related fields (likeuserById
,userByEmail
,userByName
) are scattered throughout the list rather than grouped alphabetically.This change adds a
sort_schema
boolean configuration option that, when enabled, uses GraphQL Core's built-inlexicographic_sort_schema
function to sort all schema elements alphabetically - including types, fields, enums, interfaces, unions, and more.Usage Example: