meadsteve / lagom

📦 Autowiring dependency injection container for python 3
https://lagom-di.readthedocs.io/en/latest/
MIT License
278 stars 15 forks source link

WIP integration with strawberry #203

Closed meadsteve closed 1 year ago

meadsteve commented 2 years ago

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-ai[bot] commented 2 years ago

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:

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!

meadsteve commented 1 year ago

Closing for now but this is still potentially interesting to me