Skip to content

Commit

Permalink
feat: graphql interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
dartt0n committed Jul 23, 2024
1 parent 08da2d8 commit 91c7a3d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
31 changes: 23 additions & 8 deletions src/adapter/external/graphql/tool/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@

if TYPE_CHECKING:
from src.adapter.external.graphql.tool.context import Context
from src.adapter.external.graphql.type.allocation import AllocationType
from src.adapter.external.graphql.type.allocation import (
BaseAllocationType,
)
from src.adapter.external.graphql.type.form_field import (
AnswerType,
BaseAnswerType,
BaseFormFieldType,
ChoiceOptionType,
FormFieldType,
)
from src.adapter.external.graphql.type.participant import ParticipantType
from src.adapter.external.graphql.type.participant import (
BaseParticipantType,
)
from src.adapter.external.graphql.type.room import RoomType
from src.adapter.external.graphql.type.user import UserType


LazyFormFieldType = Annotated[
"FormFieldType", # type: ignore
"BaseFormFieldType", # type: ignore
sb.lazy(module_path="src.adapter.external.graphql.type.form_field"),
]

Expand All @@ -33,7 +37,7 @@
]

LazyAllocationType = Annotated[
"AllocationType", # type: ignore
"BaseAllocationType", # type: ignore
sb.lazy(module_path="src.adapter.external.graphql.type.allocation"),
]

Expand All @@ -43,7 +47,7 @@
]

LazyParticipantType = Annotated[
"ParticipantType", # type: ignore
"BaseParticipantType", # type: ignore
sb.lazy(module_path="src.adapter.external.graphql.type.participant"),
]

Expand All @@ -53,7 +57,7 @@
]

LazyAnswerType = Annotated[
"AnswerType", # type: ignore
"BaseAnswerType", # type: ignore
sb.lazy(module_path="src.adapter.external.graphql.type.form_field"),
]

Expand Down Expand Up @@ -218,3 +222,14 @@ async def load_participant_answers(
answers = await info.context.answer.service.read_all()
selected = [answer.id for answer in answers if answer.respondent_id == root.id]
return await info.context.answer.loader.load_many(selected)


class WithFormFieldId(Protocol):
form_field_id: scalar.ObjectID


async def load_form_field(
root: WithFormFieldId,
info: sb.Info[LazyContext, WithFormFieldId],
) -> LazyFormFieldType:
return await info.context.form_field.loader.load(root.form_field_id)
7 changes: 6 additions & 1 deletion src/adapter/external/graphql/type/form_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,14 @@ class BaseAnswerType:
updated_at: sb.auto
deleted_at: sb.auto

form_field_id: scalar.ObjectID
kind: FormFieldKindType # type: ignore

form_field_id: scalar.ObjectID
form_field: resolver.LazyFormFieldType = sb.field(
permission_classes=[DefaultPermissions],
resolver=resolver.load_form_field,
)

respondent_id: scalar.ObjectID
respondent: resolver.LazyParticipantType = sb.field(
permission_classes=[DefaultPermissions],
Expand Down

0 comments on commit 91c7a3d

Please sign in to comment.