Skip to content

feat: add lexicographic_sort_schema config option - #4451

Open
wjdwl002 wants to merge 1 commit into
strawberry-graphql:mainfrom
wjdwl002:feat/lexicographic-sort-schema
Open

feat: add lexicographic_sort_schema config option#4451
wjdwl002 wants to merge 1 commit into
strawberry-graphql:mainfrom
wjdwl002:feat/lexicographic-sort-schema

Conversation

@wjdwl002

@wjdwl002 wjdwl002 commented Jun 11, 2026

Copy link
Copy Markdown

Summary by Sourcery

Add a configuration option to control lexicographic sorting of generated GraphQL schemas and document its behavior.

New Features:

  • Introduce a lexicographic_sort_schema boolean option on StrawberryConfig to optionally sort schema types, fields, and arguments alphabetically.

Documentation:

  • Document the new lexicographic_sort_schema configuration option with examples of its effect on the schema output.

Tests:

  • Add tests covering the default behavior, sorted schema output, and successful query execution when lexicographic_sort_schema is enabled.

Chores:

  • Add a release note describing the new lexicographic_sort_schema option and its default behavior.

@sourcery-ai

sourcery-ai Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Adds a new StrawberryConfig option lexicographic_sort_schema that, when enabled, applies GraphQL-core’s lexicographic_sort_schema to the underlying schema, affecting introspection and SDL output, along with tests, documentation, and release notes for the new behavior.

File-Level Changes

Change Details Files
Introduce lexicographic_sort_schema option on StrawberryConfig and wire it into schema construction.
  • Extend StrawberryConfig docstring to describe lexicographic_sort_schema behavior and default
  • Add lexicographic_sort_schema: bool = False field to StrawberryConfig
  • Apply graphql-core’s lexicographic_sort_schema to the underlying GraphQLSchema in Schema._get_schema when the config option is enabled
strawberry/schema/config.py
strawberry/schema/schema.py
Document the new configuration option for users.
  • Add a new section to schema configuration docs explaining why and how to use lexicographic_sort_schema
  • Provide before/after SDL examples showing the effect of sorting
docs/types/schema-configurations.md
Add tests verifying default and enabled behavior of lexicographic_sort_schema.
  • Assert the new option defaults to False on StrawberryConfig
  • Verify that, by default, schema SDL preserves definition order
  • Verify that enabling lexicographic_sort_schema produces lexicographically sorted types and fields in SDL
  • Verify that enabling lexicographic_sort_schema does not affect query execution semantics
tests/schema/test_config.py
Record the feature in release notes as a minor release change.
  • Create RELEASE.md describing the new configuration option, its effect, sample usage, and default behavior
RELEASE.md

Possibly linked issues

  • #unknown: The PR adds the lexicographic_sort_schema config option exactly requested to sort schema for introspection and SDL.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for adding the RELEASE.md file!

Below is the changelog that will be used for the release.


Add a new lexicographic_sort_schema option to StrawberryConfig. When enabled,
the schema's types, fields and arguments are sorted alphabetically, affecting both
the introspection result and the exported SDL. This makes it easier to find
related fields (for example userById, userByName) in the GraphiQL UI and in
exported schema.graphql files.

import strawberry
from strawberry.schema.config import StrawberryConfig

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

It defaults to False, preserving the existing definition order.

This release was contributed by @wjdwl002 in #4451

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@greptile-apps

greptile-apps Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR introduces a lexicographic_sort_schema boolean option to StrawberryConfig that, when True, delegates to graphql-core's lexicographic_sort_schema utility right after schema creation, sorting types, fields, and arguments alphabetically in both introspection and SDL output.

  • Adds lexicographic_sort_schema: bool = False to StrawberryConfig and wires it up in Schema._build_schema between GraphQLSchema construction and the _strawberry_schema back-reference attachment.
  • Includes three tests covering the default (preserve order), sorted SDL output, and successful query execution; adds documentation and a RELEASE.md entry.

Confidence Score: 4/5

The change is small and well-scoped, delegating to a well-tested graphql-core utility right after schema construction and before the strawberry back-reference is attached, so existing field/resolver wiring is unaffected.

The feature works correctly for the SDL and execution paths covered by the tests. The documentation claims the option also affects introspection responses, but that path has no test, leaving a gap between the stated contract and verified behavior. No existing behaviour is broken.

tests/schema/test_config.py — the introspection ordering path advertised in the docs is not exercised.

Important Files Changed

Filename Overview
strawberry/schema/schema.py Inserts lexicographic_sort_schema call after schema construction but before _strawberry_schema back-ref; ordering is correct. Tests pass for execution, but introspection result ordering is untested.
strawberry/schema/config.py Adds lexicographic_sort_schema: bool = False dataclass field with clear docstring; straightforward and correct.
tests/schema/test_config.py Three new tests cover default ordering, sorted SDL, and execution; missing a test that verifies introspection response ordering.
docs/types/schema-configurations.md Documents the new option with a before/after SDL example; the User type referenced in the schema snippet is never shown in the example output, making it slightly incomplete.
RELEASE.md Correct minor release note with a usage example and clear description of the default behavior.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["Schema.__init__()"] --> B["Build query/mutation/subscription types\nvia schema_converter"]
    B --> C["GraphQLSchema(query, mutation, ...\nextensions={DEFINITION_BACKREF: self})"]
    C --> D{lexicographic_sort_schema\nenabled?}
    D -- Yes --> E["lexicographic_sort_schema(self._schema)\n-> new GraphQLSchema with sorted\ntypes, fields, args"]
    D -- No --> F["self._schema unchanged"]
    E --> G["self._schema._strawberry_schema = self"]
    F --> G
    G --> H["_warn_for_federation_directives()"]
    H --> I["_resolve_node_ids()"]
    I --> J["_extend_introspection()"]
    J --> K["validate_schema(self._schema)"]
Loading

Reviews (1): Last reviewed commit: "feat: add lexicographic_sort_schema conf..." | Re-trigger Greptile

Comment on lines 40 to +82
StrawberryConfig(info_class=object)

assert str(exc_info.value) == "`info_class` must be a subclass of strawberry.Info"


def test_lexicographic_sort_schema_defaults_to_false():
assert StrawberryConfig().lexicographic_sort_schema is False


def test_lexicographic_sort_schema_preserves_definition_order_by_default():
@strawberry.type
class Query:
@strawberry.field
def zebra(self) -> int: ...

@strawberry.field
def apple(self) -> int: ...

schema = strawberry.Schema(query=Query)

expected = """\
type Query {
zebra: Int!
apple: Int!
}"""

assert str(schema) == textwrap.dedent(expected).strip()


def test_lexicographic_sort_schema_sorts_fields_and_types():
@strawberry.type
class User:
name: str
age: int

@strawberry.type
class Query:
@strawberry.field
def user_by_name(self, name: str) -> User: ...

@strawberry.field
def all_users(self) -> list[User]: ...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Missing introspection ordering test

The RELEASE.md and documentation both state that lexicographic_sort_schema affects "both the introspection result and the exported SDL", but there is no test that verifies the introspection response honours the sorted order. It is possible for the SDL output (via str(schema)) to look sorted while the introspection result served to clients (e.g. GraphiQL) is not, if a future change alters how schema.introspect() iterates the type map. A test calling schema.introspect() and asserting on the ordering of data["__schema"]["types"] or the fields within a type would cover this claim.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment on lines +116 to +145
Setting `lexicographic_sort_schema` to `True` sorts all types, fields and
arguments alphabetically, affecting both the introspection result and the
exported SDL.

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

With sorting enabled a schema like:

```graphql
type Query {
userByName(name: String!): User!
allUsers: [User!]!
userById(id: Int!): User!
}
```

becomes:

```graphql
type Query {
allUsers: [User!]!
userById(id: Int!): User!
userByName(name: String!): User!
}
```

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Before/after example does not show sorted field order within non-Query types

The opening paragraph mentions that types, fields and arguments are sorted, but the before/after SDL blocks only show the Query type. Readers learning about the option won't see what happens to the associated User type's fields (name, ageage, name). Adding the User type to both the "before" and "after" blocks (matching what the test already asserts) would make the example complete and consistent with the introductory description.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@bellini666 bellini666 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like the change! :)

Just made a small suggestion for the name (@patrick91 wtyt?) and should be good to merge

any type (including NewType) to be used as a GraphQL scalar with
proper type checking support.
batching_config: Configuration for operation batching.
lexicographic_sort_schema: Whether to sort the schema's types, fields and

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe just call this sort_schema?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants