From 81ebee20d0f5c80746bad2f9963164340881fbec Mon Sep 17 00:00:00 2001 From: Angel Dijoux <77701490+Angel-Dijoux@users.noreply.github.com> Date: Tue, 23 Jan 2024 19:14:45 +0100 Subject: [PATCH] [GraphQL] write first resolver. (#38) write first resolver --- src/api/formation/formation_resolver.py | 31 +++++++++++++++++++ src/api/schema.py | 15 +++------ src/blueprints/formations.py | 6 ++-- .../formation/get_formation_details.py | 8 ++--- .../scrap/get_repartition_formations.py | 6 +++- .../scrap/utils/format_formations.py | 3 ++ 6 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 src/api/formation/formation_resolver.py diff --git a/src/api/formation/formation_resolver.py b/src/api/formation/formation_resolver.py new file mode 100644 index 0000000..4782f90 --- /dev/null +++ b/src/api/formation/formation_resolver.py @@ -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 + ) diff --git a/src/api/schema.py b/src/api/schema.py index 8708dd7..1ff587d 100644 --- a/src/api/schema.py +++ b/src/api/schema.py @@ -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), diff --git a/src/blueprints/formations.py b/src/blueprints/formations.py index 6174f59..4a5f443 100644 --- a/src/blueprints/formations.py +++ b/src/blueprints/formations.py @@ -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, @@ -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 diff --git a/src/business_logic/formation/get_formation_details.py b/src/business_logic/formation/get_formation_details.py index 3a69054..b302d86 100644 --- a/src/business_logic/formation/get_formation_details.py +++ b/src/business_logic/formation/get_formation_details.py @@ -23,7 +23,7 @@ @dataclass @strawberry.type -class FormationDetail: +class FormationDetails: id: str exceptions: Optional[ParcourSupExpectations] duree: str @@ -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: @@ -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, @@ -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: diff --git a/src/business_logic/formation/scrap/get_repartition_formations.py b/src/business_logic/formation/scrap/get_repartition_formations.py index 6c53b29..e12ce0e 100644 --- a/src/business_logic/formation/scrap/get_repartition_formations.py +++ b/src/business_logic/formation/scrap/get_repartition_formations.py @@ -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"]) diff --git a/src/business_logic/formation/scrap/utils/format_formations.py b/src/business_logic/formation/scrap/utils/format_formations.py index 5a7367a..4a7d5a6 100644 --- a/src/business_logic/formation/scrap/utils/format_formations.py +++ b/src/business_logic/formation/scrap/utils/format_formations.py @@ -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, @@ -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(), )