In a document, it's written that:
get_context allows to provide a custom context object that can be used in your resolver. You can return anything here, by default we return a dictionary with the request.
class MyGraphQLView(AsyncGraphQLView):
async def get_context(self, request: HttpRequest, response: HttpResponse) -> Any:
return {"example": 1}
urlpatterns = [
path('graphql/', MyGraphQLView.as_view(schema=schema)),
]
@strawberry.type
class Query:
@strawberry.field
def example(self, info: Info) -> str:
return str(info.context["example"])
But when I tried it in a query and it returned the following error:
{
"data": {
"example": null
},
"errors": [
{
"message": "'StrawberryChannelsContext' object is not subscriptable",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"user"
]
}
]
}
And when I tried to get a user from the request context, it didn't work either
@strawberry.type
class Query:
@strawberry.field
def user(self, info: Info) -> str:
return str(info.context.request.user)
{
"data": {
"user": null
},
"errors": [
{
"message": "'GraphQLHTTPConsumer' object has no attribute 'user'",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"user"
]
}
]
}
I'm not sure whether it's a bug or the document is outdated.
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