strawberry-django icon indicating copy to clipboard operation
strawberry-django copied to clipboard

Ordering order in the variables not respected

Open j30ng opened this issue 6 months ago • 2 comments

Describe the Bug

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"
          }
        }
      ]
    }
  }
}

System Information

  • Strawberry version (if applicable): strawberry-graphql==0.237.3 strawberry-graphql-django==0.47.1

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

j30ng avatar Aug 23 '24 12:08 j30ng