Skip to content

Commit

Permalink
Add graphql_request_context context manager to allow hooks around gra…
Browse files Browse the repository at this point in the history
…phql request handling (#23373)

## Summary & Motivation

This allows subclasses to override the `graphql_request_context()`
method and inject code around the graphql handler. Specifically this
code runs in the same thread as the graphph resolver.

## How I Tested These Changes
Existing tests should be sufficient, since we're not changing any
behavior.
  • Loading branch information
shalabhc authored Aug 8, 2024
1 parent b225f5c commit 94a088a
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions python_modules/dagster-webserver/dagster_webserver/graphql.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from abc import ABC, abstractmethod
from asyncio import Task, get_event_loop, run
from contextlib import contextmanager
from enum import Enum
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -241,18 +242,24 @@ async def execute_graphql_request(
request_context = self.make_request_context(request)

def _graphql_request():
return run(
self._graphql_schema.execute_async(
query,
variables=variables,
operation_name=operation_name,
context=request_context,
middleware=self._graphql_middleware,
with self.graphql_request_context(request):
return run(
self._graphql_schema.execute_async(
query,
variables=variables,
operation_name=operation_name,
context=request_context,
middleware=self._graphql_middleware,
)
)
)

return await run_in_threadpool(_graphql_request)

@contextmanager
def graphql_request_context(self, request: Request):
"""This context manager executes within the threadpool and can be used to wrap logic around a single graphql request."""
yield

async def execute_graphql_subscription(
self,
websocket: WebSocket,
Expand Down

0 comments on commit 94a088a

Please sign in to comment.