Skip to content

Commit

Permalink
Add category that's schema type
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp committed Oct 6, 2024
1 parent f4a6c65 commit 1d2f200
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
23 changes: 23 additions & 0 deletions example/fixture.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
8 changes: 8 additions & 0 deletions example/models/category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from dataclasses import dataclass


@dataclass
class Category:
id: int
name: str
parent_id: int | None
3 changes: 2 additions & 1 deletion example/queries/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
22 changes: 22 additions & 0 deletions example/queries/categories.py
Original file line number Diff line number Diff line change
@@ -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)
36 changes: 36 additions & 0 deletions example/types/category.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 1d2f200

Please sign in to comment.