I found attribute error when accessing graphiql page. I use custom context class.
This is the original issue #96.
When accessing graphiql endpoint I always see errors in logs. Like this:
TypeError: get_redis_connection() missing 1 required positional argument: 'request'
File "fastapi/applications.py", line 261, in __call__
await super().__call__(scope, receive, send)
File "starlette/applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "starlette/middleware/errors.py", line 146, in __call__
await self.app(scope, receive, send)
File "starlette/middleware/base.py", line 22, in __call__
await self.app(scope, receive, send)
File "starlette/exceptions.py", line 58, in __call__
await self.app(scope, receive, send)
File "fastapi/middleware/asyncexitstack.py", line 21, in __call__
raise e
File "fastapi/middleware/asyncexitstack.py", line 18, in __call__
await self.app(scope, receive, send)
File "starlette/routing.py", line 656, in __call__
await route.handle(scope, receive, send)
File "starlette/routing.py", line 315, in handle
await self.app(scope, receive, send)
File "starlette/routing.py", line 77, in app
await func(session)
File "fastapi/routing.py", line 264, in app
solved_result = await solve_dependencies(
File "fastapi/dependencies/utils.py", line 498, in solve_dependencies
solved_result = await solve_dependencies(
File "fastapi/dependencies/utils.py", line 498, in solve_dependencies
solved_result = await solve_dependencies(
File "fastapi/dependencies/utils.py", line 498, in solve_dependencies
solved_result = await solve_dependencies(
File "fastapi/dependencies/utils.py", line 523, in solve_dependencies
solved = await solve_generator(
File "fastapi/dependencies/utils.py", line 443, in solve_generator
cm = asynccontextmanager(call)(**sub_values)
File "contextlib.py", line 296, in helper
return _AsyncGeneratorContextManager(func, args, kwds)
File "contextlib.py", line 87, in __init__
self.gen = func(*args, **kwds)
This error doesn't affect the application itself. It doesn't affect logic or request handling, but it's constantly appear in logs.
I use custom context like this:
from aio_pika import Channel
from aio_pika.pool import Pool
from aiokafka import AIOKafkaProducer
from fastapi import Depends
from redis.asyncio import Redis
from strawberry.fastapi import BaseContext
from testkaftemp.services.rabbit.dependencies import get_rmq_channel_pool
from testkaftemp.services.redis.dependency import get_redis_connection
class Context(BaseContext):
"""Global graphql context."""
def __init__(
self,
redis: Redis = Depends(get_redis_connection),
rabbit: Pool[Channel] = Depends(get_rmq_channel_pool),
) -> None:
self.redis = redis
self.rabbit = rabbit
def get_context(context: Context = Depends(Context)) -> Context:
"""
Get custom context.
:param context: graphql context.
:return: context
"""
return context
# In my router.py file
...
gql_router = GraphQLRouter(
schema,
graphiql=True,
context_getter=get_context,
)
This works for handlers, but it throws an error when accessing graphiql page.
You can reproduce this issue easily by generating a project with FastAPI-template with graphql api type and with at least one dependency, such as redis.
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