Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/allmonday/pydantic2-resolve
A hierarchical, schema-based solution for fetching and crafting data, from simple to complicated.
https://github.com/allmonday/pydantic2-resolve
composition-oriented fastapi graphql pydantic
Last synced: about 2 months ago
JSON representation
A hierarchical, schema-based solution for fetching and crafting data, from simple to complicated.
- Host: GitHub
- URL: https://github.com/allmonday/pydantic2-resolve
- Owner: allmonday
- License: mit
- Created: 2023-11-01T02:37:26.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-10-25T04:14:16.000Z (3 months ago)
- Last Synced: 2024-10-26T17:45:40.061Z (3 months ago)
- Topics: composition-oriented, fastapi, graphql, pydantic
- Language: Python
- Homepage: https://allmonday.github.io/pydantic-resolve/
- Size: 1.16 MB
- Stars: 40
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
- License: LICENSE
Awesome Lists containing this project
README
[![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)**This repo has been merged into pydantic-resolve and no more maintained**
**This repo has been merged into pydantic-resolve and no more maintained**
**This repo has been merged into pydantic-resolve and no more maintained**
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 pydantic2-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 csclass 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
```