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

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
allmonday authored Apr 8, 2024
2 parents 3cdc82b + b6569bd commit a527ed3
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Advantages:

[Discord](https://discord.com/channels/1197929379951558797/1197929379951558800)


## Install

> If you are using pydantic v1, please use [pydantic-resolve](https://github.com/allmonday/pydantic-resolve) instead.
Expand Down Expand Up @@ -47,7 +48,7 @@ query {
# comment_count
}
}
```


This is how we do queries in GraphQL, dive by describing schema and field names.

Expand Down
61 changes: 61 additions & 0 deletions examples/0_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import asyncio
from pydantic import BaseModel
from pydantic_resolve import Resolver

comments_table = [
dict(id=1, blog_id=1, content='its interesting'),
dict(id=2, blog_id=1, content='i dont understand'),
dict(id=3, blog_id=2, content='why? how?'),
dict(id=4, blog_id=2, content='wow!'),
]

async def query_comments(blog_id: int):
print(f'run query - {blog_id}')
return [c for c in comments_table if c['blog_id'] == blog_id]

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

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

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')
blog = await Resolver().resolve(blog)
print(blog)


async def batch():
my_blog_site = MyBlogSite(
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)
print(my_blog_site.model_dump_json(indent=4))


async def main():
await single()
await batch()

asyncio.run(main())
54 changes: 54 additions & 0 deletions examples/0_demo_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import asyncio
from pydantic import BaseModel
from pydantic_resolve import Resolver, build_list, LoaderDepend

comments_table = [
dict(id=1, blog_id=1, content='its interesting'),
dict(id=2, blog_id=1, content='i dont understand'),
dict(id=3, blog_id=2, content='why? how?'),
dict(id=4, blog_id=2, content='wow!'),
]

async def blog_to_comments_loader(blog_ids: list[int]):
print(blog_ids)
return build_list(comments_table, blog_ids, lambda c: c['blog_id'])


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]

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 = [
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)
print(my_blog_site.model_dump_json(indent=4))


async def main():
await batch()

asyncio.run(main())

0 comments on commit a527ed3

Please sign in to comment.