-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: notification queries (#1563)
- Loading branch information
Showing
15 changed files
with
163 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
# Notification module | ||
|
||
## Queries | ||
::: kili.entrypoints.queries.notification.__init__.QueriesNotification | ||
::: kili.presentation.client.notification.NotificationClientMethods |
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
File renamed without changes.
15 changes: 15 additions & 0 deletions
15
src/kili/adapters/kili_api_gateway/notification/mappers.py
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,15 @@ | ||
"""Mappers for notification API calls.""" | ||
|
||
from typing import Dict | ||
|
||
from kili.adapters.kili_api_gateway.user.mappers import user_where_mapper | ||
from kili.domain.notification import NotificationFilter | ||
|
||
|
||
def map_notification_filter(filters: NotificationFilter) -> Dict: | ||
"""Build the GraphQL NotificationWhere variable to be sent in an operation.""" | ||
return { | ||
"hasBeenSeen": filters.has_been_seen, | ||
"id": filters.id, | ||
"user": user_where_mapper(filters.user) if filters.user else None, | ||
} |
18 changes: 18 additions & 0 deletions
18
src/kili/adapters/kili_api_gateway/notification/operations.py
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 @@ | ||
"""Collection of notification's related GraphQL queries and mutations.""" | ||
|
||
GQL_COUNT_NOTIFICATIONS = """ | ||
query countNotifications($where: NotificationWhere!) { | ||
data: countNotifications(where: $where) | ||
} | ||
""" | ||
|
||
|
||
def get_notifications_query(fragment: str) -> str: | ||
"""Get the query for notifications.""" | ||
return f""" | ||
query notifications($where: NotificationWhere!, $first: PageSize!, $skip: Int!) {{ | ||
data: notifications(where: $where, first: $first, skip: $skip) {{ | ||
{fragment} | ||
}} | ||
}} | ||
""" |
35 changes: 35 additions & 0 deletions
35
src/kili/adapters/kili_api_gateway/notification/operations_mixin.py
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,35 @@ | ||
"""Mixin extending Kili API Gateway class with notification-related operations.""" | ||
|
||
from typing import Dict, Generator | ||
|
||
from kili.adapters.kili_api_gateway.base import BaseOperationMixin | ||
from kili.adapters.kili_api_gateway.helpers.queries import ( | ||
PaginatedGraphQLQuery, | ||
QueryOptions, | ||
fragment_builder, | ||
) | ||
from kili.domain.notification import NotificationFilter | ||
from kili.domain.types import ListOrTuple | ||
|
||
from .mappers import map_notification_filter | ||
from .operations import GQL_COUNT_NOTIFICATIONS, get_notifications_query | ||
|
||
|
||
class NotificationOperationMixin(BaseOperationMixin): | ||
"""GraphQL Mixin extending GraphQL Gateway class with notification-related operations.""" | ||
|
||
def list_notifications( | ||
self, filters: NotificationFilter, fields: ListOrTuple[str], options: QueryOptions | ||
) -> Generator[Dict, None, None]: | ||
"""List notifications.""" | ||
fragment = fragment_builder(fields) | ||
query = get_notifications_query(fragment) | ||
where = map_notification_filter(filters=filters) | ||
return PaginatedGraphQLQuery(self.graphql_client).execute_query_from_paginated_call( | ||
query, where, options, "Retrieving notifications", GQL_COUNT_NOTIFICATIONS | ||
) | ||
|
||
def count_notification(self, filters: NotificationFilter) -> int: | ||
"""Count notifications.""" | ||
variables = {"where": map_notification_filter(filters=filters)} | ||
return self.graphql_client.execute(GQL_COUNT_NOTIFICATIONS, variables)["data"] |
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 was deleted.
Oops, something went wrong.
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 @@ | ||
"""Notification domain.""" | ||
|
||
from dataclasses import dataclass | ||
from typing import TYPE_CHECKING, NewType, Optional | ||
|
||
if TYPE_CHECKING: | ||
from .user import UserFilter | ||
|
||
NotificationId = NewType("NotificationId", str) | ||
|
||
|
||
@dataclass | ||
class NotificationFilter: | ||
"""Notification filter.""" | ||
|
||
has_been_seen: Optional[bool] | ||
id: Optional[NotificationId] # noqa: A003 | ||
user: Optional["UserFilter"] |
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
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,24 @@ | ||
"""Notification use cases.""" | ||
|
||
from typing import Dict, Generator | ||
|
||
from kili.adapters.kili_api_gateway.helpers.queries import QueryOptions | ||
from kili.domain.notification import NotificationFilter | ||
from kili.domain.types import ListOrTuple | ||
from kili.use_cases.base import BaseUseCases | ||
|
||
|
||
class NotificationUseCases(BaseUseCases): | ||
"""Notification use cases.""" | ||
|
||
def list_notifications( | ||
self, filters: NotificationFilter, fields: ListOrTuple[str], options: QueryOptions | ||
) -> Generator[Dict, None, None]: | ||
"""List notifications.""" | ||
return self._kili_api_gateway.list_notifications( | ||
filters=filters, fields=fields, options=options | ||
) | ||
|
||
def count_notifications(self, filters: NotificationFilter) -> int: | ||
"""Count notifications.""" | ||
return self._kili_api_gateway.count_notification(filters=filters) |
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,21 @@ | ||
import pytest_mock | ||
|
||
from kili.presentation.client.notification import NotificationClientMethods | ||
from kili.use_cases.notification import NotificationUseCases | ||
|
||
|
||
def test_given_client_when_fetching_notifications_it_works( | ||
mocker: pytest_mock.MockerFixture, kili_api_gateway | ||
): | ||
mocker.patch.object( | ||
NotificationUseCases, "list_notifications", return_value=(n for n in [{"id": "notif_id"}]) | ||
) | ||
# Given | ||
kili = NotificationClientMethods() | ||
kili.kili_api_gateway = kili_api_gateway | ||
|
||
# When | ||
notifs = kili.notifications() | ||
|
||
# Then | ||
assert notifs == [{"id": "notif_id"}] |