diff --git a/starlette/graphql.py b/starlette/graphql.py index 6e5d6ec6a..e55ba749f 100644 --- a/starlette/graphql.py +++ b/starlette/graphql.py @@ -33,6 +33,7 @@ def __init__( schema: "graphene.Schema", executor_class: type = None, graphiql: bool = True, + session: "sqlalchemy.orm.Session" = None, ) -> None: self.schema = schema self.graphiql = graphiql @@ -40,6 +41,7 @@ def __init__( self.is_async = executor_class is not None and issubclass( executor_class, AsyncioExecutor ) + self.session = session async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: if self.executor_class is not None: @@ -94,6 +96,8 @@ async def handle_graphql(self, request: Request) -> Response: background = BackgroundTasks() context = {"request": request, "background": background} + if self.session: + context['session'] = self.session result = await self.execute( query, variables=variables, context=context, operation_name=operation_name