From 1d2f200ec194518db58196b7bb8149dda8c11c53 Mon Sep 17 00:00:00 2001 From: rafalp Date: Sun, 6 Oct 2024 22:39:32 +0200 Subject: [PATCH] Add category that's schema type --- example/fixture.py | 23 ++++++++++++++++++++++ example/models/category.py | 8 ++++++++ example/queries/__init__.py | 3 ++- example/queries/categories.py | 22 +++++++++++++++++++++ example/types/category.py | 36 +++++++++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 example/models/category.py create mode 100644 example/queries/categories.py create mode 100644 example/types/category.py diff --git a/example/fixture.py b/example/fixture.py index 2d99852..6958161 100644 --- a/example/fixture.py +++ b/example/fixture.py @@ -1,11 +1,34 @@ from typing import Any +from .models.category import Category from .models.group import Group from .models.user import User def get_data() -> dict[str, dict[int, Any]]: return { + "categories": { + 1: Category( + id=1, + name="First category", + parent_id=None, + ), + 2: Category( + id=2, + name="Second category", + parent_id=None, + ), + 3: Category( + id=3, + name="Child category", + parent_id=1, + ), + 4: Category( + id=4, + name="Other child category", + parent_id=1, + ), + }, "groups": { 1: Group( id=1, diff --git a/example/models/category.py b/example/models/category.py new file mode 100644 index 0000000..21db613 --- /dev/null +++ b/example/models/category.py @@ -0,0 +1,8 @@ +from dataclasses import dataclass + + +@dataclass +class Category: + id: int + name: str + parent_id: int | None diff --git a/example/queries/__init__.py b/example/queries/__init__.py index 07f0b0a..546fb50 100644 --- a/example/queries/__init__.py +++ b/example/queries/__init__.py @@ -1,8 +1,9 @@ from typing import Any -from . import groups, hello, users +from . import categories, groups, hello, users queries: Any = [ + categories.Query, groups.Query, hello.Query, users.Query, diff --git a/example/queries/categories.py b/example/queries/categories.py new file mode 100644 index 0000000..bbff01f --- /dev/null +++ b/example/queries/categories.py @@ -0,0 +1,22 @@ +from ariadne_graphql_modules import GraphQLObject +from graphql import GraphQLResolveInfo + +from ..database import db +from ..types.category import CategoryType + + +class Query(GraphQLObject): + @GraphQLObject.field() + @staticmethod + async def categories(obj, info: GraphQLResolveInfo) -> list[CategoryType]: + return await db.get_all("categories", parent_id=None) + + @GraphQLObject.field() + @staticmethod + async def category(obj, info: GraphQLResolveInfo, id: str) -> CategoryType | None: + try: + id_int = int(id) + except (TypeError, ValueError): + return None + + return await db.get_row("categories", id=id_int) diff --git a/example/types/category.py b/example/types/category.py new file mode 100644 index 0000000..24d2731 --- /dev/null +++ b/example/types/category.py @@ -0,0 +1,36 @@ +from ariadne_graphql_modules import GraphQLObject +from ariadne import gql +from graphql import GraphQLResolveInfo + +from ..database import db +from ..models.category import Category + + +class CategoryType(GraphQLObject): + __schema__ = gql( + """ + type Category { + id: ID! + name: String! + parent: Category + children: [Category!]! + } + """ + ) + + @GraphQLObject.resolver("parent", "CategoryType") + @staticmethod + async def resolve_parent( + obj: Category, info: GraphQLResolveInfo + ) -> Category | None: + if not obj.parent_id: + return None + + return await db.get_row("categories", id=obj.parent_id) + + @GraphQLObject.resolver("children", list["CategoryType"]) + @staticmethod + async def resolve_children( + obj: Category, info: GraphQLResolveInfo + ) -> list[Category]: + return await db.get_all("categories", parent_id=obj.id)