lagom
lagom copied to clipboard
WIP integration with strawberry
The TLDR goal of this work is to be able to build @strawberry data loaders like this:
class AuthorLoader(DataLoader[str, Author]):
def __init__(self, some_dep_db: DB):
super().__init__(self.get_authors)
self.db = some_dep_db
async def get_authors(self, ids) -> typing.List["Author"]:
print(f"Bulk loading authors: {ids}")
return await self.db.get_the_authors(ids)
and then use it like
container = StrawberryContainer()
container[AuthorLoader] = AuthorLoader
@strawberry.type
class Book:
title: str
author_id: int
@strawberry.field
@container.attach_field
async def author(self, loader: "AuthorLoader") -> "Author":
return await loader.load(self.author_id)
class MyGraphQL(GraphQL):
async def get_context(self, *args, **kwargs) -> Any:
context: Dict = {}
container.hook_into_context(context)
return context
Sourcery Code Quality Report
β Β Merging this PR will decrease code quality in the affected files by 0.03%.
Quality metrics | Before | After | Change |
---|---|---|---|
Complexity | 1.95 β | 1.95 β | 0.00 |
Method Length | 36.10 β | 36.13 β | 0.03 π |
Working memory | 6.17 β | 6.19 β | 0.02 π |
Quality | 82.90% β | 82.87% β | -0.03% π |
Other metrics | Before | After | Change |
---|---|---|---|
Lines | 457 | 460 | 3 |
Changed files | Quality Before | Quality After | Quality Change |
---|---|---|---|
lagom/container.py | 82.36% β | 82.33% β | -0.03% π |
lagom/updaters.py | 94.84% β | 94.84% β | 0.00% |
Here are some functions in these files that still need a tune-up:
File | Function | Complexity | Length | Working Memory | Quality | Recommendation |
---|---|---|---|---|---|---|
lagom/container.py | partial | 1 β | 119 π | 10 π | 65.51% π | Extract out complex expressions |
lagom/container.py | _infer_dependencies | 3 β | 81 π | 11 π | 66.82% π | Extract out complex expressions |
Legend and Explanation
The emojis denote the absolute quality of the code:
- β excellent
- π good
- π poor
- β very poor
The π and π indicate whether the quality has improved or gotten worse with this pull request.
Please see our documentation here for details on how these metrics are calculated.
We are actively working on this report - lots more documentation and extra metrics to come!
Help us improve this quality report!
Closing for now but this is still potentially interesting to me