Skip to content

Commit b1f3c57

Browse files
committed
Support class-style contexts in sqlalchemy mapper, a la fastapi style.
https://strawberry.rocks/docs/integrations/fastapi shows two ways to implement contexts -- dictionary style, as is currently supported in this mapper, and class style, which is more idiomatic in fastapi and results in a "not subscritable" error in the current mapper implementation. I much prefer modifying the mapper to support both styles, but if this is something you'd rather not add, I can in theory implement __getitem__ on my custom context and it'll work just fine. Re: tests, I couldn't find any existing tests that actually test the mapper end-to-end with a real postgres database and server. I would be happy to write an integration test of sorts that uses postgresql and fastapi and tests the mapper end-to-end, just let me know.
1 parent ba50ae3 commit b1f3c57

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/strawberry_sqlalchemy_mapper/mapper.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,12 @@ async def resolve(self, info: Info):
413413
return []
414414
else:
415415
return None
416+
if isinstance(info.context, dict):
417+
loader = info.context["sqlalchemy_loader"]
418+
else:
419+
loader = info.context.sqlalchemy_loader
416420
related_objects = (
417-
await info.context["sqlalchemy_loader"]
421+
await loader
418422
.loader_for(relationship)
419423
.load(relationship_key)
420424
)

0 commit comments

Comments
 (0)