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

Commit 90cf132

Browse files
committed
resolve Jerry's comments
1 parent 713dee1 commit 90cf132

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

graphql_api/helpers/connection.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from cursor_pagination import CursorPage, CursorPaginator
77
from django.db.models import QuerySet
88

9+
from codecov.commands.exceptions import ValidationError
910
from codecov.db import sync_to_async
1011
from graphql_api.types.enums import OrderingDirection
1112

@@ -85,13 +86,19 @@ def __init__(
8586
self.end_index = len(data)
8687

8788
if first and last:
88-
raise ValueError("Cannot provide both 'first' and 'last'")
89+
raise ValidationError("Cannot provide both 'first' and 'last'")
8990

9091
if after is not None:
91-
self.start_index = int(after) + 1
92+
try:
93+
self.start_index = int(after) + 1
94+
except ValueError:
95+
raise ValidationError("'after' cursor must be an integer")
9296

9397
if before is not None:
94-
self.end_index = min(self.end_index, int(before))
98+
try:
99+
self.end_index = min(self.end_index, int(before))
100+
except ValueError:
101+
raise ValidationError("'before' cursor must be an integer")
95102

96103
# Ensure valid bounds after 'after' and 'before'
97104
self.start_index = max(self.start_index, 0)
@@ -132,10 +139,10 @@ def has_previous(self) -> bool:
132139
class ArrayConnection:
133140
"""Connection wrapper for array pagination."""
134141

135-
def __init__(self, data: List[Any], paginator: ArrayPaginator, page: List[Any]):
136-
self.data = data
142+
def __init__(self, paginator: ArrayPaginator):
143+
self.data = paginator.data
137144
self.paginator = paginator
138-
self.page = page
145+
self.page = paginator.page
139146

140147
@property
141148
def edges(self) -> List[Dict[str, Any]]:
@@ -237,7 +244,7 @@ def queryset_to_connection_sync(
237244
array_paginator = ArrayPaginator(
238245
data, first=first, last=last, after=after, before=before
239246
)
240-
return ArrayConnection(data, array_paginator, array_paginator.page)
247+
return ArrayConnection(array_paginator)
241248

242249
else:
243250
ordering = tuple(field_order(field, ordering_direction) for field in ordering)

graphql_api/helpers/tests/test_connection.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from django.test import TransactionTestCase
33
from shared.django_apps.core.tests.factories import RepositoryFactory
44

5+
from codecov.commands.exceptions import ValidationError
56
from core.models import Repository
67
from graphql_api.types.enums import OrderingDirection, RepositoryOrdering
78

@@ -141,5 +142,13 @@ def test_both_first_and_last(self):
141142

142143
data = [1, 2, 3, 4, 5]
143144

144-
with self.assertRaises(ValueError):
145+
with self.assertRaises(ValidationError):
145146
queryset_to_connection_sync(data, last=3, first=2)
147+
148+
def test_invalid_cursors(self):
149+
from graphql_api.helpers.connection import queryset_to_connection_sync
150+
151+
data = [1, 2, 3, 4, 5]
152+
153+
with self.assertRaises(ValidationError):
154+
queryset_to_connection_sync(data, last=3, before="invalid")

0 commit comments

Comments
 (0)