feat: add lexicographic_sort_schema config option - #4451
Conversation
Reviewer's GuideAdds 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
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Thanks for adding the Below is the changelog that will be used for the release. Add a new import strawberry
from strawberry.schema.config import StrawberryConfig
schema = strawberry.Schema(
query=Query,
config=StrawberryConfig(lexicographic_sort_schema=True),
)It defaults to |
Greptile SummaryThis PR introduces a
Confidence Score: 4/5The 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
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)"]
Reviews (1): Last reviewed commit: "feat: add lexicographic_sort_schema conf..." | Re-trigger Greptile |
| 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]: ... | ||
|
|
There was a problem hiding this comment.
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!
| 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! | ||
| } | ||
| ``` | ||
|
|
There was a problem hiding this comment.
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, age → age, 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
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Maybe just call this sort_schema?
Summary by Sourcery
Add a configuration option to control lexicographic sorting of generated GraphQL schemas and document its behavior.
New Features:
Documentation:
Tests:
Chores: