{"id":21459954,"url":"https://github.com/allmonday/pydantic-resolve-demo","last_synced_at":"2025-07-15T02:31:52.303Z","repository":{"id":250003884,"uuid":"833181993","full_name":"allmonday/pydantic-resolve-demo","owner":"allmonday","description":"a pydantic-resolve demo based on FastAPI","archived":false,"fork":false,"pushed_at":"2025-05-03T11:56:34.000Z","size":912,"stargazers_count":8,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-03T12:33:13.472Z","etag":null,"topics":["fastapi","graphql","pydantic","pydantic-resolve"],"latest_commit_sha":null,"homepage":"https://allmonday.github.io/pydantic-resolve/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/allmonday.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-07-24T14:11:16.000Z","updated_at":"2025-05-03T11:56:37.000Z","dependencies_parsed_at":"2024-11-23T06:37:25.851Z","dependency_job_id":"b9b87843-d656-4351-b012-fc13bcf796f7","html_url":"https://github.com/allmonday/pydantic-resolve-demo","commit_stats":null,"previous_names":["allmonday/pydantic-resolve-demo"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/allmonday/pydantic-resolve-demo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allmonday%2Fpydantic-resolve-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allmonday%2Fpydantic-resolve-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allmonday%2Fpydantic-resolve-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allmonday%2Fpydantic-resolve-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/allmonday","download_url":"https://codeload.github.com/allmonday/pydantic-resolve-demo/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allmonday%2Fpydantic-resolve-demo/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265390885,"owners_count":23757605,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastapi","graphql","pydantic","pydantic-resolve"],"created_at":"2024-11-23T06:37:19.709Z","updated_at":"2025-07-15T02:31:52.295Z","avatar_url":"https://github.com/allmonday.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pydantic-resolve-demo\n\nFor FastAPI user, we already have pydantic, openapi support and openapi-ts, pydantic-resolve is the last puzzle for a better development pattern.\n\npydantic-resolve: https://github.com/allmonday/pydantic-resolve\n\nThis repo shows how to define and compose schemas together with pydantic-resolve, and also sync schemas and methods to frontend with openapi-ts.\n\nBased on the business model, there are three basic entities: Blog, Comment, User\n\nafter that, we can easily build new complicated model by inheriting and extending\n\n- sample 1: show blogs\n- sample 2: show blogs with comments\n- sample 3: show author info of comments\n\nfrom 1 to 3, you will progressively add more and more fields into the response model, and the client side can sense it immediately.\n\n![](./sample.jpg)\n\n```python\nclass MyBlogSite(BaseModel):\n    name: str\n    # blogs: list[Blog] = []  # this will not include comments and comment_count\n    blogs: list[MyBlog] = []\n    async def resolve_blogs(self):\n        return await get_blogs()\n\n    comment_count: int = 0\n    def post_comment_count(self):\n        return sum([b.comment_count for b in self.blogs])\n\nclass MyBlog(Blog):\n    # comments: list[Comment] = []  # this will not include user field\n    comments: list[MyComment] = []\n    def resolve_comments(self, loader=LoaderDepend(blog_to_comments_loader)):\n        return loader.load(self.id)\n\n    comment_count: int = 0\n    def post_comment_count(self):\n        return len(self.comments)\n\nclass MyComment(Comment):\n    user: Optional[User] = None\n    def resolve_user(self, loader=LoaderDepend(user_loader)):\n        return loader.load(self.user_id)\n\n```\n\nWe only need to make sure the return value from `resolve_blogs` or `resolve_comments` can be converted to BASE schemas (Blog, Comment)\n\nthen we can replace `blogs: list[Blog] = []` to `blogs: list[MyBlog] = []` to extend more fields.\n\n## BE\n\nprefer python \u003e= 3.10\n\n```shell\npython -m venv venv\nsource venv/bin/activate\npip install -r requirement.pip\nfastapi dev main.py\n```\n\n```python\n@app.get(\"/my-site/{name}\", response_model=MyBlogSite, tags=[\"main\"])\nasync def read_my_site(name: str):\n    site = MyBlogSite(name=name)\n    return await Resolver().resolve(site)\n```\n\nvisit http://localhost:8000/docs#/main/read_my_site\n\n## FE\n\nensure server is running, and then generate the client from `localhost:8000/openapi.json`\n\n```shell\ncd fe\nnpm install\nnpm run openapi-ts\nnpx ts-node main.ts\n```\n\n```ts\nimport { MainService } from './src/client'\n\n// whti this can avoid the impact of renaming schema name\ntype AsyncReturnType\u003cT extends (...args: any) =\u003e any\u003e = Awaited\u003cReturnType\u003cT\u003e\u003e;\n\ntype ReadMySite1 = typeof ModuleAService.readMySite1;\ntype MySite1 = AsyncReturnType\u003cReadMySite1\u003e;\ntype Param1 = Parameters\u003cReadMySite1\u003e[0];\n\n(async () =\u003e {\n    const name: Param['name'] = 'tangkikodo'\n    const data: MySite = await MainService.readMySite({ name: name })\n    console.log(JSON.stringify(data, null, 2))\n})()\n```\n\ninspect the log output\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallmonday%2Fpydantic-resolve-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fallmonday%2Fpydantic-resolve-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallmonday%2Fpydantic-resolve-demo/lists"}