-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5dd5596
commit 647ea62
Showing
41 changed files
with
636 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import strawberry | ||
|
||
from app.core.domain.groups.dto import GroupCreateDTO | ||
|
||
|
||
@strawberry.input(name="GroupCreateInput") | ||
class GroupCreateInputGQL: | ||
name: str | ||
|
||
def to_dto(self) -> GroupCreateDTO: | ||
return GroupCreateDTO( | ||
name=self.name, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from typing import Annotated | ||
|
||
import strawberry | ||
from aioinject import Inject | ||
from aioinject.ext.strawberry import inject | ||
from result import Err | ||
|
||
from app.adapters.graphql.apps.groups.input import GroupCreateInputGQL | ||
from app.adapters.graphql.apps.groups.payload import GroupCreatePayloadGQL | ||
from app.adapters.graphql.apps.groups.types import GroupGQL | ||
from app.adapters.graphql.context import Info | ||
from app.adapters.graphql.errors import EntityAlreadyExistsErrorGQL | ||
from app.adapters.graphql.extensions import AuthExtension | ||
from app.adapters.graphql.validation import validate_callable | ||
from app.core.domain.groups.commands import GroupCreateCommand | ||
from app.core.errors import EntityAlreadyExistsError | ||
|
||
|
||
@strawberry.type(name="GroupMutations") | ||
class GroupMutationsGQL: | ||
|
||
@strawberry.mutation(extensions=[AuthExtension]) # type: ignore[misc] | ||
@inject | ||
async def create( | ||
self, | ||
command: Annotated[GroupCreateCommand, Inject], | ||
input: GroupCreateInputGQL, | ||
info: Info, | ||
) -> GroupCreatePayloadGQL: | ||
dto = validate_callable(input.to_dto) | ||
if isinstance(dto, Err): | ||
return GroupCreatePayloadGQL(error=dto.err_value) | ||
|
||
result = await command.execute( | ||
dto=dto.ok_value, | ||
user=await info.context.user, | ||
) | ||
if isinstance(result, Err): | ||
match result.err_value: | ||
case EntityAlreadyExistsError(): # pragma: no branch | ||
return GroupCreatePayloadGQL( | ||
error=EntityAlreadyExistsErrorGQL(), | ||
) | ||
|
||
return GroupCreatePayloadGQL( | ||
group=GroupGQL.from_dto(result.ok_value), | ||
error=None, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from typing import Annotated | ||
|
||
import strawberry | ||
|
||
from app.adapters.graphql.apps.groups.types import GroupGQL | ||
from app.adapters.graphql.errors import EntityAlreadyExistsErrorGQL | ||
from app.adapters.graphql.validation import ValidationErrorsGQL | ||
|
||
GroupCreateError = Annotated[ | ||
ValidationErrorsGQL | EntityAlreadyExistsErrorGQL, | ||
strawberry.union("GroupCreateError"), | ||
] | ||
|
||
|
||
@strawberry.type(name="GroupCreatePayload") | ||
class GroupCreatePayloadGQL: | ||
group: GroupGQL | None = None | ||
error: GroupCreateError | None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,19 @@ | ||
from typing import Self | ||
|
||
import strawberry | ||
|
||
from app.adapters.graphql.dto import DTOMixin | ||
from app.db.models import Group | ||
|
||
|
||
@strawberry.type(name="Group") | ||
class GroupGQL: | ||
pass | ||
class GroupGQL(DTOMixin[Group]): | ||
id: strawberry.ID | ||
name: str | ||
|
||
@classmethod | ||
def from_dto(cls, model: Group) -> Self: | ||
return cls( | ||
id=strawberry.ID(str(model.id)), | ||
name=model.name, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,36 @@ | ||
import dataclasses | ||
from collections.abc import Awaitable | ||
from functools import cached_property | ||
from typing import TypeVar | ||
|
||
from starlette.requests import Request | ||
from starlette.responses import Response | ||
from starlette.websockets import WebSocket | ||
from strawberry.types import Info as StrawberryInfo | ||
|
||
from app.core.di import create_container | ||
from app.core.domain.auth.dto import TokenClaims | ||
from app.core.domain.auth.utils import UserGetter | ||
from app.db.models import User | ||
from lib.loaders import Dataloaders | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
@dataclasses.dataclass(slots=True, kw_only=True) | ||
@dataclasses.dataclass(kw_only=True) | ||
class Context: | ||
request: Request | WebSocket | ||
response: Response | None | ||
loaders: Dataloaders | ||
|
||
maybe_access_token: TokenClaims | None | ||
|
||
@cached_property | ||
def user(self) -> Awaitable[User]: | ||
return UserGetter( | ||
container=create_container(), | ||
token=self.maybe_access_token, | ||
) | ||
|
||
|
||
Info = StrawberryInfo[Context, None] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import Any | ||
|
||
from strawberry import BasePermission | ||
from strawberry.permission import PermissionExtension | ||
|
||
from app.adapters.graphql.context import Info | ||
|
||
|
||
class IsAuthenticated(BasePermission): | ||
message = "Not Authenticated" | ||
error_extensions = {"code": "UNAUTHORIZED"} # noqa: RUF012 | ||
|
||
def has_permission( | ||
self, | ||
source: Any, # noqa: ARG002, ANN401 | ||
info: Info, | ||
**kwargs: Any, # noqa: ARG002, ANN401 | ||
) -> bool: | ||
return info.context.maybe_access_token is not None | ||
|
||
|
||
AuthExtension = PermissionExtension([IsAuthenticated()]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from aioinject import Scoped | ||
|
||
from app.core.domain.groups.commands import GroupCreateCommand | ||
from app.core.domain.groups.repositories import GroupRepository | ||
from app.core.domain.groups.services import GroupService | ||
from lib.types import Providers | ||
|
||
providers: Providers = [ | ||
Scoped(GroupRepository), | ||
Scoped(GroupService), | ||
Scoped(GroupCreateCommand), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import dataclasses | ||
|
||
from jwt import PyJWTError | ||
|
||
|
||
@dataclasses.dataclass | ||
class TokenDecodeError: | ||
error: PyJWTError |
Oops, something went wrong.