Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【开源自荐】Pydantic-resolve, 构造更精准的视图数据 #2696

Open
allmonday opened this issue Mar 4, 2024 · 0 comments
Open

Comments

@allmonday
Copy link

推荐项目

  • 类别:Python
  • 项目标题:Pydantic-resolve, 为构造完美的视图数据而生
  • 项目描述:解决后端构造前端视图数据中的所有痛点,类似graphql, 用schema 申明式的描述数据,充分发挥dataloader 的优势,将构造复杂结构的视图数据变得易如反掌。(解决了graphql 中只能层层下推无法回溯处理数据的痛点)
  • 亮点:概念简单, API简洁,不需要引入复杂的 graphql 框架,却能生成比graphql 更精准的视图数据

  • 示例代码:(可选)

class Comment(BaseModel):
    id: int
    content: str

class Blog(BaseModel):
    id: int
    title: str

    comments: list[Comment] = []
    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 MyBlogSite(BaseModel):
    blogs: list[Blog]
    name: str

    comment_count: int = 0
    def post_comment_count(self):
        return sum([b.comment_count for b in self.blogs])

async def main():
    my_blog_site = MyBlogSite(
        name: "tangkikodo's blog"
        blogs = [
            Blog(id=1, title='what is pydantic-resolve'),
            Blog(id=2, title='what is composition oriented development pattarn'),
        ]
    )
    my_blog_site = await Resolver().resolve(my_blog_site)

output

{
    "name": "tangkikodo's blog",
    "blogs": [
        {
            "id": 1,
            "title": "what is pydantic-resolve",
            "comments": [
                {
                    "id": 1,
                    "content": "its interesting"
                },
                {
                    "id": 2,
                    "content": "i dont understand"
                }
            ],
            "comment_count": 2
        },
        {
            "id": 2,
            "title": "what is composition oriented development pattarn",
            "comments": [
                {
                    "id": 3,
                    "content": "why? how?"
                },
                {
                    "id": 4,
                    "content": "wow!"
                }
            ],
            "comment_count": 2
        }
    ],
    "comment_count": 4
}
  • 截图:image

  • 后续更新计划:

    • debug模式, 可以完整展示全部字段, 以及字段间的依赖关系。
@allmonday allmonday changed the title 【开源自荐】Pydantic-resolve, 构造完美的视图数据 【开源自荐】Pydantic-resolve, 构造更精准的视图数据 Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants