-
-
Notifications
You must be signed in to change notification settings - Fork 537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add strawberry.context #1243
Add strawberry.context #1243
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1243 +/- ##
==========================================
- Coverage 97.63% 97.05% -0.58%
==========================================
Files 88 89 +1
Lines 3382 3366 -16
Branches 496 495 -1
==========================================
- Hits 3302 3267 -35
- Misses 44 62 +18
- Partials 36 37 +1 |
async def get_user(self, info: Info, id: strawberry.ID) -> User: | ||
return await info.context["user_loader"].load(id) | ||
async def get_user(self, id: strawberry.ID) -> User: | ||
return await strawberry.context["user_loader"].load(id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think we can:
- type it somehow?
- use context.user_loader instead of context["user_loader"]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- type it somehow?
I'm not sure how we could type it unfortunately. You can always cast it when you import it but that's not great. I think that's one of the downsides of this proposal.
- use context.user_loader instead of context["user_loader"]
That should already work. See tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I wonder if doing:
ctx: Context[Something] = strawberry.context
is annoying or not.
Do we get benefits by using ContextVars and still using parameters (and dependency injection)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we get benefits by using ContextVars and still using parameters (and dependency injection)?
You can start defining (and exporting from Strawberry) stand alone functions that get useful things from the context. E.g. current_user()
The only issue with that is there wouldn't be a way for those functions to be known about early in the execution to register things into the context before resolution happens. E.g. you couldn't do get_dataloader(UserDataloader)
inside a resolver without also registering an extension on the schema that would inject the dataloader into the context. Dependency injection would allow you to that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh it would make the data validation proposal (#820 (comment)) a bit cleaner. Instead of having to pass info to the .validate()
function you could grab the context from the context variable.
Pros:
Cons:
The second con is the one that most concerns me. I've seen Flask codebases where its magic But the fact that this guidance would make its utility mostly about simpler resolver signatures, it makes me wonder if there's enough of an upside to this? Perhaps there is though. |
@AndrewIngram I'm beginning to wonder if there is much benefit to this as well. Especially because you can't type it effectively and the first Pro you list could be solved by #374 |
As discussed in our recent community meeting, I'm going to close this PR since we don't think it provides enough value to be worth the potential confusion. |
Description
This PR introduces
strawberry.context
which gives you access to the current context through the magic of Context Variables. I propose that becomes the recommend way to access the context rather than throughinfo
. What do you think @strawberry-graphql/core ?Types of Changes
Issues Fixed or Closed by This PR
Checklist