pydantic2-resolve


Namepydantic2-resolve JSON
Version 2.1.1 PyPI version JSON
download
home_pagehttps://github.com/allmonday/pydantic2_resolve
Summarycreate nested data structure easily
upload_time2024-04-20 15:14:02
maintainerNone
docs_urlNone
authortangkikodo
requires_python<4.0,>=3.7
licenseMIT
keywords pydantic2 fastapi
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![pypi](https://img.shields.io/pypi/v/pydantic2-resolve.svg)](https://pypi.python.org/pypi/pydantic2-resolve)
[![Downloads](https://static.pepy.tech/personalized-badge/pydantic2-resolve?period=month&units=abbreviation&left_color=grey&right_color=orange&left_text=Downloads)](https://pepy.tech/project/pydantic2-resolve)
![Python Versions](https://img.shields.io/pypi/pyversions/pydantic2-resolve)
[![CI](https://github.com/allmonday/pydantic2-resolve/actions/workflows/ci.yml/badge.svg)](https://github.com/allmonday/pydantic2-resolve/actions/workflows/ci.yml)
![Test Coverage](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/allmonday/372580ad111c92340dac39987c0c4e9a/raw/covbadge.json)

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

It combines the advantages of restful and graphql.


![img](doc/intro.jpeg)


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](https://discord.com/channels/1197929379951558797/1197929379951558800)


## Install

> If you are using pydantic v1, please use [pydantic-resolve](https://github.com/allmonday/pydantic-resolve) instead.


```shell
pip install pydantic-resolve
```

## Concepts from GraphQL to Pydantic-resolve

```gql
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

```shell
poetry run python -m unittest  # or
poetry run pytest  # or
poetry run tox
```

## Coverage

```shell
poetry run coverage run -m pytest
poetry run coverage report -m
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/allmonday/pydantic2_resolve",
    "name": "pydantic2-resolve",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.7",
    "maintainer_email": null,
    "keywords": "pydantic2, fastapi",
    "author": "tangkikodo",
    "author_email": "allmonday@126.com",
    "download_url": "https://files.pythonhosted.org/packages/d1/64/c6cf91e3fe8d221f51622e2dcf843767dd387d99708f01bcf265465c8f0f/pydantic2_resolve-2.1.1.tar.gz",
    "platform": null,
    "description": "[![pypi](https://img.shields.io/pypi/v/pydantic2-resolve.svg)](https://pypi.python.org/pypi/pydantic2-resolve)\n[![Downloads](https://static.pepy.tech/personalized-badge/pydantic2-resolve?period=month&units=abbreviation&left_color=grey&right_color=orange&left_text=Downloads)](https://pepy.tech/project/pydantic2-resolve)\n![Python Versions](https://img.shields.io/pypi/pyversions/pydantic2-resolve)\n[![CI](https://github.com/allmonday/pydantic2-resolve/actions/workflows/ci.yml/badge.svg)](https://github.com/allmonday/pydantic2-resolve/actions/workflows/ci.yml)\n![Test Coverage](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/allmonday/372580ad111c92340dac39987c0c4e9a/raw/covbadge.json)\n\nPydantic-resolve is a schema based, hierarchical solution for fetching and crafting data.\n\nIt combines the advantages of restful and graphql.\n\n\n![img](doc/intro.jpeg)\n\n\nAdvantages:\n1. use declaretive way to define view data, easy to maintain and develop\n2. enhance the traditional restful response, to support gql-like style data structure.\n3. provide post_method and other tools to craft resolved data.\n\n\n[Discord](https://discord.com/channels/1197929379951558797/1197929379951558800)\n\n\n## Install\n\n> If you are using pydantic v1, please use [pydantic-resolve](https://github.com/allmonday/pydantic-resolve) instead.\n\n\n```shell\npip install pydantic-resolve\n```\n\n## Concepts from GraphQL to Pydantic-resolve\n\n```gql\nquery {\n    MyBlogSite {\n        name\n        blogs {\n            id\n            title\n            comments {\n                id\n                content\n            }\n            # comment_count\n        }\n        # comment_count\n    }\n}\n\n\nThis is how we do queries in GraphQL, dive by describing schema and field names.\n\nAssuming `comment_count` is a extra field (length of comment), which is required and calculated by client after fetching the data.\n\nclient 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).\n\nIn pydantic-resolve, we can handle comment_count at server side, by transforming the query into pydantic schemas and attach some resolve, post methods.\n\n\n```python\nimport blog_service as bs\nimport comment_service as cs\n\nclass MySite(BaseModel):\n    blogs: list[MySiteBlog] = []\n    async def resolve_blogs(self):\n        return await bs.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\n# -------- inherit and extend ----------\nclass MySiteBlog(bs.Blog):  \n    comments: list[cs.Comment] = []\n    def resolve_comments(self, loader=LoaderDepend(cs.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        \nasync def main():\n    my_blog_site = MyBlogSite(name: \"tangkikodo's blog\")\n    my_blog_site = await Resolver().resolve(my_blog_site)\n```\n\nschemas , query functions and loader functions are provided by entity's service modules. \n\nSo that we can declare customrized schema by simpily **INHERIT** and **EXTEND** from base schemas.\n\n> This just sounds like columns of values (inherit) and of foreign keys (extend) in concept of relational database.\n\nAfter transforming GraphQL query into pydantic schemas, post calculation become dead easy, and no more iterations.\n\n> Collector is a powerful feature for adjusting data structures. https://allmonday.github.io/pydantic-resolve/reference_api/#collector\n\n## API Reference\nhttps://allmonday.github.io/pydantic-resolve/reference_api/\n\n## Composition oriented development-pattern (wip)\nhttps://github.com/allmonday/composition-oriented-development-pattern\n\n\n## Unittest\n\n```shell\npoetry run python -m unittest  # or\npoetry run pytest  # or\npoetry run tox\n```\n\n## Coverage\n\n```shell\npoetry run coverage run -m pytest\npoetry run coverage report -m\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "create nested data structure easily",
    "version": "2.1.1",
    "project_urls": {
        "Homepage": "https://github.com/allmonday/pydantic2_resolve",
        "Repository": "https://github.com/allmonday/pydantic2_resolve"
    },
    "split_keywords": [
        "pydantic2",
        " fastapi"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e48a09243ae2b8976663c10513615c8113715e9b41b51ab7b23dc6775c4c6889",
                "md5": "a1e2f1b469c03e11a802bca9e9c29ebe",
                "sha256": "189f5e99d7f842fb8f6d1b138bfe9340f94e3f804b2180e635b65fac5c9523d1"
            },
            "downloads": -1,
            "filename": "pydantic2_resolve-2.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a1e2f1b469c03e11a802bca9e9c29ebe",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.7",
            "size": 14991,
            "upload_time": "2024-04-20T15:14:00",
            "upload_time_iso_8601": "2024-04-20T15:14:00.713540Z",
            "url": "https://files.pythonhosted.org/packages/e4/8a/09243ae2b8976663c10513615c8113715e9b41b51ab7b23dc6775c4c6889/pydantic2_resolve-2.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d164c6cf91e3fe8d221f51622e2dcf843767dd387d99708f01bcf265465c8f0f",
                "md5": "23a1cd13b7d24fb1ba888ed8bb34ddf8",
                "sha256": "85fd6300d804d5d49a47fa03c46a2199895cc2afb33ba588059b13f0b8dd5148"
            },
            "downloads": -1,
            "filename": "pydantic2_resolve-2.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "23a1cd13b7d24fb1ba888ed8bb34ddf8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.7",
            "size": 14380,
            "upload_time": "2024-04-20T15:14:02",
            "upload_time_iso_8601": "2024-04-20T15:14:02.974415Z",
            "url": "https://files.pythonhosted.org/packages/d1/64/c6cf91e3fe8d221f51622e2dcf843767dd387d99708f01bcf265465c8f0f/pydantic2_resolve-2.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-20 15:14:02",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "allmonday",
    "github_project": "pydantic2_resolve",
    "github_not_found": true,
    "lcname": "pydantic2-resolve"
}
        
Elapsed time: 0.22690s