Inspired by GraphQL & dataloader, I build a tiny tool to handle nested schema based on Pydantic #9316
allmonday
started this conversation in
Show and tell
Replies: 2 comments
-
update: |
Beta Was this translation helpful? Give feedback.
0 replies
-
update: https://github.com/allmonday/pydantic-resolve this package can be very helpful if used with @tiangolo, I think this little package can be helpful for fullstack developers to build (rpc + graphql) like APIs. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In daily development, building nested dataset from one or multiple queries is always a boring stuff.
for example:
At first, I tried relationship, but later I find this way create coupling between schema and models, and relationship itself also may not meet the requirements (like have extra filter field besides foreign key).
Then I tried GraphQL, It's powerful, you can easily create nested data structure, and eliminate the N+1 query with the help of dataloader, but for scenarios of internal APIs I don't need the flexibility it provides, I like the way of generating clients directly from
openapi.json
.Inspired by graphene, I create a little tool
pydantic-resolve
https://github.com/allmonday/pydantic-resolveall you need to do is define a
resolve_[field_name]
method, andpydantic-resolve
will recursively resolve it for you. And at the same time you can still enjoy the benefits fromresponse_model
.and I also provides an example using sqlalchemy, sqlite and aiodataloader: https://github.com/allmonday/pydantic-resolve/blob/master/examples/sqla_dataloader.py and it creates data:
and another example of how to use it in fastapi:
I hope this tool can help people who are troubled by the same problem as me.
Beta Was this translation helpful? Give feedback.
All reactions