Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit e3a7229

Browse files
Ordering fix
1 parent e06c5d8 commit e3a7229

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

graphql_api/helpers/connection.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,27 @@ class DictCursorPaginator(CursorPaginator):
208208
def position_from_instance(self, instance):
209209
position = []
210210
for order in self.ordering:
211-
parts = order.lstrip("-").split("__")
211+
desc = order.startswith("-")
212+
key = order.lstrip("-")
213+
parts = key.split("__")
212214
attr = instance
213-
while parts:
215+
for part in parts:
214216
try:
215-
attr = getattr(attr, parts[0])
216-
except AttributeError as attr_err:
217+
attr = getattr(attr, part)
218+
except AttributeError as e:
217219
try:
218-
attr = attr[parts[0]]
220+
attr = attr[part]
219221
except (KeyError, TypeError):
220-
raise attr_err from None
221-
parts.pop(0)
222-
position.append(str(attr))
222+
raise e from None
223+
224+
is_null = attr is None
225+
if is_null:
226+
# Make sure None sorts after everything else
227+
sortable_value = (1, "")
228+
else:
229+
sortable_value = (0, attr)
230+
231+
position.append(sortable_value if not desc else ("DESC", sortable_value))
223232
return position
224233

225234

0 commit comments

Comments
 (0)