lagom icon indicating copy to clipboard operation
lagom copied to clipboard

WIP integration with strawberry

Open meadsteve opened this issue 2 years ago β€’ 1 comments

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

meadsteve avatar Jun 17 '22 12:06 meadsteve

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!

sourcery-ai[bot] avatar Jun 17 '22 12:06 sourcery-ai[bot]

Closing for now but this is still potentially interesting to me

meadsteve avatar Mar 06 '23 13:03 meadsteve