strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

'request' is overridden by default 'request' when context is inherriting BaseContext

Open pepilipep opened this issue 2 years ago • 1 comments

Hey there, folks, loving the library so far!

We use a typed object as dependency context with FastAPI which is inheriting BaseContext as per the docs.

One small thing that we ran into is that our Context class has a request attribute which happened to be overridden by the default request: https://github.com/strawberry-graphql/strawberry/blob/main/strawberry/fastapi/router.py#L70

I found this issue #1493 which solves the problem for when the context_getter is a dict. But after issue #1541 when this BaseContext was added, it was implemented with a different behavior from the dict one.

pepilipep avatar Jun 07 '22 08:06 pepilipep

Hi @pepilipep, thanks for raising this bug with my implementation :smile:

To be clear, however, does your request attribute within your CustomContext itself inherit from Request? By which I mean, is it also a starlette.requests.Request object or is it some other instance?

I ask this because I feel that it is important to have the request attribute of any context have a strict typing associated with it and that this typing should be starlette.requests.Request. I would assume that overwriting the request attribute with something that isn't a starlette.requests.Request object could break functionality elsewhere in the package.

If your request attribute is something else entirely then I believe that its naming should not collide with the default request.

In any case, this highlights an edge use-case of the BaseContext object, which I will edit the documentation to describe in more detail depending on the decided upon implementation.

tsmith023 avatar Aug 06 '22 10:08 tsmith023