Skip to main content

pypi Downloads Python Versions CI Test Coverage

Pydantic-resolve is a schema based, hierarchical solution for fetching and crafting data.

It combines the advantages of restful and graphql.

img

Advantages:

  1. use declaretive way to define view data, easy to maintain and develop
  2. enhance the traditional restful response, to support gql-like style data structure.
  3. provide post_method and other tools to craft resolved data.

Discord

Install

If you are using pydantic v1, please use pydantic-resolve instead.

pip install pydantic-resolve

Concepts from GraphQL to Pydantic-resolve

query {
    MyBlogSite {
        name
        blogs {
            id
            title
            comments {
                id
                content
            }
            # comment_count
        }
        # comment_count
    }
}


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

Assuming `comment_count` is a extra field (length of comment), which is required and calculated by client after fetching the data.

client side so need to iterate over the blogs to get the length and the sum, which is boring (things gets worse if the structure is deeper).

In pydantic-resolve, we can handle comment_count at server side, by transforming the query into pydantic schemas and attach some resolve, post methods.


```python
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)

schemas , query functions and loader functions are provided by entity's service modules.

So that we can declare customrized schema by simpily INHERIT and EXTEND from base schemas.

This just sounds like columns of values (inherit) and of foreign keys (extend) in concept of relational database.

After transforming GraphQL query into pydantic schemas, post calculation become dead easy, and no more iterations.

Collector is a powerful feature for adjusting data structures. https://allmonday.github.io/pydantic-resolve/reference_api/#collector

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

Release files for pydantic2-resolve 2.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic2-resolve 2.1.1
File Size Uploaded
pydantic2_resolve-2.1.1.tar.gz 14.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic2-resolve 2.1.1
File Interpreter ABI Platform
pydantic2_resolve-2.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 29.4 kB

Release files / pydantic2_resolve-2.1.1.tar.gz

Download URL pydantic2_resolve-2.1.1.tar.gz
Size 14.4 kB
Tags Source
SHA-256 checksum
How to use checksums
85fd6300d804d5d49a47fa03c46a2199895cc2afb33ba588059b13f0b8dd5148
BLAKE2b-256 checksum
How to use checksums
d164c6cf91e3fe8d221f51622e2dcf843767dd387d99708f01bcf265465c8f0f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.4 Darwin/22.2.0

Release files / pydantic2_resolve-2.1.1-py3-none-any.whl

Download URL pydantic2_resolve-2.1.1-py3-none-any.whl
Size 15.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
189f5e99d7f842fb8f6d1b138bfe9340f94e3f804b2180e635b65fac5c9523d1
BLAKE2b-256 checksum
How to use checksums
e48a09243ae2b8976663c10513615c8113715e9b41b51ab7b23dc6775c4c6889
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.4 Darwin/22.2.0

Release history Release notifications | RSS feed

This release

2.1.1 This release

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page