At the moment the Django and Sanic integrations return a custom dataclass from the default implementation of get_context
. This allows gives a nicer developer experience because you can access properties via dict or attribute notation. However if you override the get_context
method to customise the context and return a dictionary (which is what the docs recommend) it can break assumptions made elsewhere.
I propose that we standardise how the context is created by wrapping whatever is returned from get_context
in a StrawberryContext
wrapper class. This would also allow us to store internal data in the context which might be useful in custom fields or extensions.
Something like this:
class MyGraphQLView(GraphQLView):
def get_context(self, request, response):
return {
"request": request,
"response": response,
"current_time": datetime.now(),
}
@strawberry.type
class Query:
@strawberry.field
def current_time(self, info) -> str:
context = info.context # type is StrawberryContext
return context.current_time.isoformat()
Also I think it should be possible (and encouraged) to modify context using extensions rather than having to override the base view.
Only concern I have is how we type this. Thoughts @strawberry-graphql/core ?
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too