Skip to content

Commit 8803499

Browse files
committed
Address review: standardize missing-id errors and tighten Any types
- Missing-id resolvers now raise GraphQLError consistently (was Model.DoesNotExist on three resolvers, GraphQLError on resolve_assignment) - _get_user_type/corpus_folder_type/annotation_type return type[T] instead of bare type, with TYPE_CHECKING forward refs to avoid circular imports - resolve_feedback_count: int, resolve_total_messages: int, resolve_all_source_node_in_relationship: QuerySet[Relationship] - id_to_children: dict[int | str, list[int | str]] (was dict[Any, list[Any]])
1 parent a0d6340 commit 8803499

5 files changed

Lines changed: 17 additions & 11 deletions

File tree

config/graphql/annotation_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ def resolve_content_modalities(self, info) -> Any:
9393

9494
all_source_node_in_relationship = graphene.List(lambda: RelationshipType)
9595

96-
def resolve_feedback_count(self, info) -> Any:
96+
def resolve_feedback_count(self, info) -> int:
9797
# If feedback_count was annotated on the queryset, use it
9898
if hasattr(self, "feedback_count"):
9999
return self.feedback_count
100100
# Otherwise, count it (but this triggers N+1)
101101
return self.user_feedback.count()
102102

103-
def resolve_all_source_node_in_relationship(self, info) -> Any:
103+
def resolve_all_source_node_in_relationship(self, info) -> QuerySet[Relationship]:
104104
return self.source_node_in_relationships.all()
105105

106106
all_target_node_in_relationship = graphene.List(lambda: RelationshipType)

config/graphql/base_types.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
from __future__ import annotations
44

5-
from typing import Any
5+
from typing import TYPE_CHECKING, Any
66

77
import graphene
88
from graphene.types.generic import GenericScalar
99
from graphql_relay import to_global_id
1010

11+
if TYPE_CHECKING:
12+
from config.graphql.annotation_types import AnnotationType
13+
from config.graphql.corpus_types import CorpusFolderType
14+
from config.graphql.user_types import UserType
15+
1116

1217
def build_flat_tree(
1318
nodes: list[dict[str, Any]],
@@ -31,7 +36,7 @@ def build_flat_tree(
3136
- "children": list of child node global IDs.
3237
"""
3338
# Map node IDs to their immediate children IDs
34-
id_to_children: dict[Any, list[Any]] = {}
39+
id_to_children: dict[int | str, list[int | str]] = {}
3540
for node in nodes:
3641
node_id = node["id"]
3742
parent_id = node["parent_id"]
@@ -231,19 +236,19 @@ class PageAwareAnnotationType(graphene.ObjectType):
231236
page_annotations = graphene.List(lambda: _get_annotation_type())
232237

233238

234-
def _get_user_type() -> type:
239+
def _get_user_type() -> type[UserType]:
235240
from config.graphql.user_types import UserType
236241

237242
return UserType
238243

239244

240-
def _get_corpus_folder_type() -> type:
245+
def _get_corpus_folder_type() -> type[CorpusFolderType]:
241246
from config.graphql.corpus_types import CorpusFolderType
242247

243248
return CorpusFolderType
244249

245250

246-
def _get_annotation_type() -> type:
251+
def _get_annotation_type() -> type[AnnotationType]:
247252
from config.graphql.annotation_types import AnnotationType
248253

249254
return AnnotationType

config/graphql/document_queries.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from django.db.models import QuerySet
1313
from graphene import relay
1414
from graphene_django.filter import DjangoFilterConnectionField
15+
from graphql import GraphQLError
1516
from graphql_jwt.decorators import login_required
1617
from graphql_relay import from_global_id
1718

@@ -142,7 +143,7 @@ def resolve_document_relationship(
142143
"""
143144
relay_id = kwargs.get("id")
144145
if relay_id is None:
145-
raise DocumentRelationship.DoesNotExist()
146+
raise GraphQLError("DocumentRelationship id is required")
146147
django_pk = from_global_id(relay_id)[1]
147148
result = DocumentRelationshipQueryOptimizer.get_relationship_by_id(
148149
user=info.context.user,

config/graphql/user_queries.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def resolve_userimport(
8383
) -> UserImport:
8484
relay_id = kwargs.get("id")
8585
if relay_id is None:
86-
raise UserImport.DoesNotExist()
86+
raise GraphQLError("UserImport id is required")
8787
django_pk = from_global_id(relay_id)[1]
8888
return UserImport.objects.visible_to_user(info.context.user).get(id=django_pk)
8989

@@ -106,7 +106,7 @@ def resolve_userexport(
106106
) -> UserExport:
107107
relay_id = kwargs.get("id")
108108
if relay_id is None:
109-
raise UserExport.DoesNotExist()
109+
raise GraphQLError("UserExport id is required")
110110
django_pk = from_global_id(relay_id)[1]
111111
return UserExport.objects.visible_to_user(info.context.user).get(id=django_pk)
112112

config/graphql/user_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def resolve_reputation_for_corpus(self, info, corpus_id) -> Any:
8282
except Exception:
8383
return 0
8484

85-
def resolve_total_messages(self, info) -> Any:
85+
def resolve_total_messages(self, info) -> int:
8686
"""
8787
Resolve total messages posted by this user.
8888
Only counts messages visible to the requesting user.

0 commit comments

Comments
 (0)