Hello. I just found out that ordering by multiple fields does not work properly when the order is in the variables, rather than in the query itself. Here's a simple example that reproduces the bug.
class MyObject(models.Model):
name = models.CharField(max_length=10)
date = models.DateField()
@strawberry.django.ordering.order(MyObject)
class MyObjectOrder:
name: Ordering
date: Ordering
@strawberry.django.type(MyObject, order=MyObjectOrder)
class MyObject:
name: str
date: datetime.date
@strawberry.django.connection(ListConnectionWithTotalCount[MyObject])
def objects() -> list[MyObject]:
return MyObject.objects.all()
If you include the order (date DESC, name DESC) in the query itself, you get the correctly ordered response:
Request:
{
"operationName": "myQuery",
"variables": {},
"query": "query myQuery {\n objects(order: {date: DESC, name: DESC}) {\n totalCount\n edges {\n node {\n name\n date\n }\n }\n }\n}"
}
Response:
{
"data": {
"objects": {
"totalCount": 6,
"edges": [
{
"node": {
"name": "lemon",
"date": "2024-08-23"
}
},
{
"node": {
"name": "banana",
"date": "2024-08-23"
}
},
{
"node": {
"name": "apple",
"date": "2024-08-23"
}
},
{
"node": {
"name": "lemon",
"date": "2024-08-22"
}
},
{
"node": {
"name": "banana",
"date": "2024-08-22"
}
},
{
"node": {
"name": "apple",
"date": "2024-08-22"
}
}
]
}
}
}
whereas the response from the following exchange is not ordered as expected.
Request:
{
"operationName": "myQuery",
"variables": {
"order": {
"date": "DESC",
"name": "DESC"
}
},
"query": "query myQuery($order: MyObjectOrder) {\n objects(order: $order) {\n totalCount\n edges {\n node {\n name\n date\n }\n }\n }\n}"
}
Response:
{
"data": {
"objects": {
"totalCount": 6,
"edges": [
{
"node": {
"name": "lemon",
"date": "2024-08-23"
}
},
{
"node": {
"name": "lemon",
"date": "2024-08-22"
}
},
{
"node": {
"name": "banana",
"date": "2024-08-23"
}
},
{
"node": {
"name": "banana",
"date": "2024-08-22"
}
},
{
"node": {
"name": "apple",
"date": "2024-08-23"
}
},
{
"node": {
"name": "apple",
"date": "2024-08-22"
}
}
]
}
}
}
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too