https://github.com/allmonday/pydantic-resolve-demo
主要介绍如何借助 graphql resolver 和 dataloader 这两个工具
将 graphql 中对数据组合的便利性带到 fastapi/ django ninja (支持 pydantic )项目中来。
class MyBlogSite(BaseModel):
name: str
# blogs: list[Blog] = [] # this will not include comments and comment_count
blogs: list[MyBlog] = []
async def resolve_blogs(self):
return await get_blogs()
comment_count: int = 0
def post_comment_count(self):
return sum([b.comment_count for b in self.blogs])
class MyBlog(Blog):
# comments: list[Comment] = [] # this will not include user field
comments: list[MyComment] = []
def resolve_comments(self, loader=LoaderDepend(blog_to_comments_loader)):
return loader.load(self.id)
comment_count: int = 0
def post_comment_count(self):
return len(self.comments)
class MyComment(Comment):
user: Optional[User] = None
def resolve_user(self, loader=LoaderDepend(user_loader)):
return loader.load(self.user_id)