create nested data structure easily
Project description
Pydantic-resolve is a schema based, hierarchical solution for fetching and crafting data.
It combines the advantages of restful and graphql.
Advantages:
- use declaretive way to define view data, easy to maintain and develop
- enhance the traditional restful response, to support gql-like style data structure.
- provide post_method and other tools to craft resolved data.
Install
If you are using pydantic v2, please use pydantic2-resolve instead.
pip install pydantic-resolve
Concepts from GraphQL to Pydantic-resolve
This is how we do queries in GraphQL
comment_count
is a extra field calculated from length of comment, which is usally process by client after fetching the data because the this kind of calculation is flexible.
cliend side so need to iterate over the blogs to get the length and the sum, which is boring.
query {
MyBlogSite {
name
blogs {
id
title
comments {
id
content
}
# comment_count
}
# comment_count
}
}
In pydantic-resolve, we can process comment_count at server side, by transforming the query into pydantic schemas.
schemas , query functions and loader functions are provided by each service module, so we can declare our customrized schema by simpily INHERIT and EXTEND from base schemas.
This just sounds like columns of values (inherit) and columns of foreign keys (extend).
After transforming GraphQL query into pydantic schemas, post calculation become dead easy.
import blog_service as bs
import comment_service as cs
class MySite(BaseModel):
blogs: list[MySiteBlog] = []
async def resolve_blogs(self):
return await bs.get_blogs()
comment_count: int = 0
def post_comment_count(self):
return sum([b.comment_count for b in self.blogs])
# -------- inherit and extend ----------
class MySiteBlog(bs.Blog):
comments: list[cs.Comment] = []
def resolve_comments(self, loader=LoaderDepend(cs.blog_to_comments_loader)):
return loader.load(self.id)
comment_count: int = 0
def post_comment_count(self):
return len(self.comments)
async def main():
my_blog_site = MyBlogSite(name: "tangkikodo's blog")
my_blog_site = await Resolver().resolve(my_blog_site)
API Reference
https://allmonday.github.io/pydantic-resolve/reference_api/
Composition oriented development-pattern (wip)
https://github.com/allmonday/composition-oriented-development-pattern
Unittest
poetry run python -m unittest # or
poetry run pytest # or
poetry run tox
Coverage
poetry run coverage run -m pytest
poetry run coverage report -m
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pydantic_resolve-1.10.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a89a7718553bba5df989bb432308a7dabe9650523b4ac129509581c8aa6706c |
|
MD5 | 1d4b34d38a292af247e7e4acda9975ad |
|
BLAKE2b-256 | 88a98ecd345571d6ee8a822b0add5e83fe7ec467922dce4750daeef5b205ad78 |