Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
add post example
Browse files Browse the repository at this point in the history
  • Loading branch information
allmonday committed Mar 3, 2024
1 parent ad6dd10 commit b6569bd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,19 @@ class Blog(BaseModel):
comments: list[Comment] = []
async def resolve_comments(self):
return await query_comments(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 single():
blog = Blog(id=1, title='what is pydantic-resolve')
blog = await Resolver().resolve(blog)
Expand Down Expand Up @@ -128,7 +136,8 @@ output of my_blog_site:
"id": 2,
"content": "i dont understand"
}
]
],
"comment_count": 2
},
{
"id": 2,
Expand All @@ -142,9 +151,11 @@ output of my_blog_site:
"id": 4,
"content": "wow!"
}
]
],
"comment_count": 2
}
]
],
"comment_count": 4
}
```

Expand Down Expand Up @@ -183,10 +194,18 @@ class Blog(BaseModel):
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]

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

async def batch():
my_blog_site = MyBlogSite(
blogs = [
Expand Down
8 changes: 8 additions & 0 deletions examples/0_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@ class Blog(BaseModel):
comments: list[Comment] = []
async def resolve_comments(self):
return await query_comments(self.id)

comment_count: int = 0
def post_comment_count(self):
return len(self.comments)

class MyBlogSite(BaseModel):
blogs: list[Blog]

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


async def single():
blog = Blog(id=1, title='what is pydantic-resolve')
Expand Down
8 changes: 8 additions & 0 deletions examples/0_demo_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ class Blog(BaseModel):
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]

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

async def batch():
my_blog_site = MyBlogSite(
blogs = [
Expand Down

0 comments on commit b6569bd

Please sign in to comment.