Some checks failed
Test before pr merge / test-lint (pull_request) Failing after 6s
52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
from cpl.graphql.query_context import QueryContext
|
|
from cpl.graphql.schema.filter.filter import Filter
|
|
from cpl.graphql.schema.graph_type import GraphType
|
|
from cpl.graphql.schema.sort.sort import Sort
|
|
from cpl.graphql.schema.sort.sort_order import SortOrder
|
|
from model.author_dao import AuthorDao
|
|
from model.author_query import AuthorGraphType, AuthorFilter
|
|
from model.post import Post
|
|
|
|
class PostFilter(Filter[Post]):
|
|
def __init__(self):
|
|
Filter.__init__(self)
|
|
self.int_field("id")
|
|
self.filter_field("author", AuthorFilter)
|
|
self.string_field("title")
|
|
self.string_field("content")
|
|
|
|
class PostSort(Sort[Post]):
|
|
def __init__(self):
|
|
Sort.__init__(self)
|
|
self.field("id", SortOrder)
|
|
self.field("title", SortOrder)
|
|
self.field("content", SortOrder)
|
|
|
|
class PostGraphType(GraphType[Post]):
|
|
|
|
def __init__(self, authors: AuthorDao):
|
|
GraphType.__init__(self)
|
|
|
|
self.int_field(
|
|
"id",
|
|
resolver=lambda root: root.id,
|
|
).with_public(True)
|
|
|
|
async def _a(root: Post):
|
|
return await authors.get_by_id(root.author_id)
|
|
|
|
def r_name(ctx: QueryContext):
|
|
return ctx.user.username == "admin"
|
|
|
|
self.object_field("author", AuthorGraphType, resolver=_a).with_require_any(
|
|
[], [r_name]
|
|
)
|
|
self.string_field(
|
|
"title",
|
|
resolver=lambda root: root.title,
|
|
).with_public(True)
|
|
self.string_field(
|
|
"content",
|
|
resolver=lambda root: root.content,
|
|
).with_public(True)
|