Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Release type: minor

This release adds support for lexicographic (alphabetical) sorting of GraphQL
schema fields through a new configuration option.

Added a `sort_schema` configuration option to `StrawberryConfig` that allows
users to enable alphabetical sorting of schema types and fields. When enabled,
the schema uses GraphQL's built-in `lexicographic_sort_schema` function to sort
all types, fields, and other schema elements alphabetically, making the
introspection UI easier to navigate.

**Usage Example:**

```python
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:**

- Sorts all schema elements alphabetically (types, fields, enums, etc.)
- Disabled by default to maintain backward compatibility
- Works with all schema features (mutations, subscriptions, interfaces, unions,
etc.)
- Compatible with other config options like `auto_camel_case`

This makes GraphQL introspection UIs (typically at `/graphql`) much easier to
navigate by grouping related fields together alphabetically rather than in
definition order.
52 changes: 52 additions & 0 deletions docs/types/schema-configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,58 @@ class CustomInfo(Info):
schema = strawberry.Schema(query=Query, config=StrawberryConfig(info_class=CustomInfo))
```

### sort_schema

By default, Strawberry will preserve the order in which fields are defined in
your code. When you enable schema sorting, all types, fields, and other schema
elements will be sorted alphabetically using GraphQL's `lexicographicSortSchema`
function. This can make GraphQL introspection UIs (like GraphiQL) easier to
navigate by grouping related fields together alphabetically.

```python
schema = strawberry.Schema(query=Query, config=StrawberryConfig(sort_schema=True))
```

**Example:**

```python
import strawberry
from strawberry.schema.config import StrawberryConfig


@strawberry.type
class Query:
user_by_name: str
user_by_id: str
user_by_email: str


schema = strawberry.Schema(query=Query, config=StrawberryConfig(sort_schema=True))
```

With `sort_schema=True`, the schema will be printed as:

```graphql
type Query {
userByEmail: String!
userById: String!
userByName: String!
}
```

Instead of preserving the original definition order:

```graphql
type Query {
userByName: String!
userById: String!
userByEmail: String!
}
```

This option affects all schema elements including types, fields, enums, input
types, interfaces, and unions.

### enable_experimental_incremental_execution

<Note>
Expand Down
67 changes: 56 additions & 11 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ types-pyyaml = "^6.0.12.20240917"
mypy = "^1.15.0"
pyright = "1.1.401"
codeflash = ">=0.9.2"
pre-commit = "^4.3.0"

[tool.poetry.group.integrations]
optional = true
Expand Down
10 changes: 9 additions & 1 deletion strawberry/printer/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
overload,
)

from graphql import GraphQLObjectType, GraphQLSchema, is_union_type
from graphql import (
GraphQLObjectType,
GraphQLSchema,
is_union_type,
lexicographic_sort_schema,
)
from graphql.language.printer import print_ast
from graphql.type import (
is_enum_type,
Expand Down Expand Up @@ -597,6 +602,9 @@ def print_schema(schema: BaseSchema) -> str:
"GraphQLSchema",
schema._schema, # type: ignore
)

if schema.config.sort_schema:
graphql_core_schema = lexicographic_sort_schema(graphql_core_schema)
extras = PrintExtras()

filtered_directives = [
Expand Down
1 change: 1 addition & 0 deletions strawberry/schema/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class StrawberryConfig:
enable_experimental_incremental_execution: bool = False
_unsafe_disable_same_type_validation: bool = False
batching_config: Optional[BatchingConfig] = None
sort_schema: bool = False

def __post_init__(
self,
Expand Down
Loading
Loading