-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
91 additions
and
1 deletion.
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
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,8 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Category: | ||
id: int | ||
name: str | ||
parent_id: int | None |
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,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) |
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,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) |