Ordering¶
| types.py | |
|---|---|
Tip
In most cases order fields should have Optional annotations and default value strawberry.UNSET. Above auto annotation is wrapped in Optional automatically. UNSET is automatically used for fields without field or with strawberry_django.order_field.
The code above generates the following schema:
| schema.graphql | |
|---|---|
Custom order methods¶
You can define custom order method by defining your own resolver.
Warning
Do not use queryset.order_by() directly. Due to order_by not being chainable operation, changes applied this way would be overriden later.
Tip
strawberry_django.Ordering has convenient method resolve that can be used to convert field's name to appropriate F object with correctly applied asc(), desc() method with nulls_first and nulls_last arguments.
The code above generates the following schema:
| schema.graphql | |
|---|---|
Resolver arguments¶
prefix- represents the current path or position- Required
- Important for nested ordering
- In code bellow custom order
nameends up orderingFruitinstead ofColorwithout applyingprefix
value- represents graphql field type- Required, but forbidden for default
ordermethod - must be annotated
- used instead of field's return type
- Using
autois the same asstrawberry_django.Ordering. queryset- can be used for more complex ordering- Optional, but Required for default
ordermethod - usually used to
annotateQuerySet sequence- used to order values on the same level- elements in graphql object are not quaranteed to keep their order as defined by user thus this argument should be used in those cases GraphQL Spec
- usually for custom order field methods does not have to be used
- for advanced usage, look at
strawberry_django.process_orderfunction
Resolver return¶
For custom field methods two return values are supported
- iterable of values acceptable by
QuerySet.order_by->Collection[F | str] - tuple with
QuerySetand iterable of values acceptable byQuerySet.order_by->tuple[QuerySet, Collection[F | str]]
For default order method only second variant is supported.
What about nulls?¶
By default null values are ignored. This can be toggled as such @strawberry_django.order_field(order_none=True)
Overriding the default order method¶
Works similar to field order method, but:
- is responsible for resolution of ordering for entire object
- must be named
order - argument
querysetis Required - argument
valueis Forbidden - should probaly use
sequence
Tip
As seen above strawberry_django.process_order function is exposed and can be reused in custom methods. For order method order skip_object_order_method was used to avoid endless recursion.
Adding orderings to types¶
All fields and mutations inherit orderings from the underlying type by default. So, if you have a field like this:
The fruits field will inherit the order of the type same same way as if it was passed to the field.
Adding orderings directly into a field¶
Orderings added into a field override the default order of this type.