Skip to content
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

Refactor queries for strawberry.lazy imports #1023

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions api/graphql/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import Any

from fastapi import Request
from strawberry.dataloader import DataLoader
from strawberry.fastapi import BaseContext

from api.graphql.utils.loaders import loaders
from api.utils.db import get_projectless_db_connection
from db.python.connect import Connection


class GraphQLContext(BaseContext):
"""Basic dict type for GraphQL context to be passed to resolvers"""

def __init__(self, connection: Connection | None, loaders: dict[str, Any]):
super().__init__()
self.connection: Connection | None = connection
self.loaders: dict[str, Any] = {
k: DataLoader(v, cache=False) for k, v in loaders.items()
}


async def get_context(
request: Request, # pylint: disable=unused-argument
connection: Connection = get_projectless_db_connection,
) -> GraphQLContext:
"""Get loaders / cache context for strawberyy GraphQL"""
mapped_loaders = {k: fn(connection) for k, fn in loaders.items()}
return GraphQLContext(
connection=connection,
loaders=mapped_loaders,
)
Loading