Skip to content

Commit

Permalink
[GraphQL] write first resolver. (#38)
Browse files Browse the repository at this point in the history
write first resolver
  • Loading branch information
Angel-Dijoux authored Jan 23, 2024
1 parent 7791f11 commit 81ebee2
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 17 deletions.
31 changes: 31 additions & 0 deletions src/api/formation/formation_resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import strawberry

from src.business_logic.formation.get_formation_details import (
FormationDetails,
get_formation_details_by_id,
)
from src.business_logic.formation.scrap.get_main_formation import get_main_formations
from src.business_logic.formation.scrap.get_repartition_formations import (
get_libelle_type_formation,
)
from src.business_logic.formation.scrap.types import Facet, FormationsWithTotal
from src.business_logic.formation.scrap.search_formation import (
search_formations,
)


# TODO : Deprecated REST a migrate here for get_feed_formations and search_formations
@strawberry.type
class FormationResolver:
get_formation_details_by_id: FormationDetails = (
strawberry.field(resolver=get_formation_details_by_id),
)
get_libelle_type_formation: list[Facet] = strawberry.field(
resolver=get_libelle_type_formation
)
search_formations: FormationsWithTotal = strawberry.field(
resolver=search_formations
)
get_feed_formations: FormationsWithTotal = strawberry.field(
resolver=get_main_formations
)
15 changes: 5 additions & 10 deletions src/api/schema.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import strawberry
from strawberry.extensions import MaxAliasesLimiter, MaxTokensLimiter, QueryDepthLimiter

# from strawberry.tools import merge_types

@strawberry.type
class Book:
title: str
author: str


@strawberry.type
class Query:
books: list[Book]
from src.api.formation.formation_resolver import FormationResolver

# types: tuple = (FormationResolver, ...)
# Queries = merge_types("Queries", types)

schema = strawberry.Schema(
query=Query,
query=FormationResolver,
extensions=[
QueryDepthLimiter(max_depth=10),
MaxTokensLimiter(max_token_count=1000),
Expand Down
6 changes: 4 additions & 2 deletions src/blueprints/formations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from werkzeug.exceptions import HTTPException

from src.blueprints.route_handler import HttpMethod, route_handler
from src.business_logic.formation.get_formation_details import get_formation_by_id
from src.business_logic.formation.get_formation_details import (
get_formation_details_by_id,
)
from src.business_logic.formation.scrap.get_main_formation import (
auth_get_main_formations,
get_main_formations,
Expand Down Expand Up @@ -44,7 +46,7 @@ def resolve_get_main_formations() -> Tuple[Response, int] | HTTPException:
"../docs/formations/formation.yaml",
)
def resolve_get_formation_by_id(id: str) -> Tuple[Response, int] | HTTPException:
result = get_formation_by_id(id)
result = get_formation_details_by_id(id)
return result, HTTP_200_OK if result else HTTP_204_NO_CONTENT


Expand Down
8 changes: 4 additions & 4 deletions src/business_logic/formation/get_formation_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

@dataclass
@strawberry.type
class FormationDetail:
class FormationDetails:
id: str
exceptions: Optional[ParcourSupExpectations]
duree: str
Expand All @@ -50,7 +50,7 @@ def _read_json_formation(for_id: str) -> Optional[dict[str, Any]]:
return result if len(result) > 0 else None


def _process_formation(for_id: str) -> FormationDetail:
def _process_formation(for_id: str) -> FormationDetails:
formation = _read_json_formation(for_id)

if formation:
Expand All @@ -76,7 +76,7 @@ def _process_formation(for_id: str) -> FormationDetail:
updated_at = datetime.strptime(
formation["modification_date"], DATE_FORMAT
).date()
return FormationDetail(
return FormationDetails(
id=identifiant,
exceptions=exceptions,
duree=duree,
Expand All @@ -91,7 +91,7 @@ def _process_formation(for_id: str) -> FormationDetail:
)


def get_formation_by_id(for_id: str) -> FormationDetail:
def get_formation_details_by_id(for_id: str) -> FormationDetails:
try:
return _process_formation(for_id)
except Exception as e:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
from src.business_logic.formation.scrap.utils.get_onisep_data import get_onisep_data


def _process_data(data: dict) -> list[Facet]:
return [Facet(**facet) for facet in data]


def get_libelle_type_formation(query: str) -> list[Facet]:
params = f"/search?q={query}"
data = get_onisep_data(params)
return data["facets"]["libelle_type_formation"]
return _process_data(data["facets"]["libelle_type_formation"])
3 changes: 3 additions & 0 deletions src/business_logic/formation/scrap/utils/format_formations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from src.business_logic.favoris.is_favorite import check_if_is_favorite
from src.business_logic.formation.scrap.types import (
FormationIsFavorite,
Expand All @@ -16,6 +17,8 @@ def _create_formation_from_dict(formation: dict) -> Formation:
domain=formation.get("domainesous-domaine"),
niveau_de_sortie=formation.get("niveau_de_sortie_indicatif"),
duree=formation.get("duree"),
created_at=datetime.strptime("01/01/2024", "%m/%d/%Y").date(),
updated_at=datetime.strptime("01/01/2024", "%m/%d/%Y").date(),
)


Expand Down

0 comments on commit 81ebee2

Please sign in to comment.