From 1c96ff43dfc9c56de8a795d1bd4c3b7c2778bf4e Mon Sep 17 00:00:00 2001 From: Chris Dilley Date: Tue, 15 Aug 2023 15:49:21 -0400 Subject: [PATCH] Add get_context() to prepare a context object --- channels_graphql_ws/graphql_ws_consumer.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/channels_graphql_ws/graphql_ws_consumer.py b/channels_graphql_ws/graphql_ws_consumer.py index 9af6c83..769d0d4 100644 --- a/channels_graphql_ws/graphql_ws_consumer.py +++ b/channels_graphql_ws/graphql_ws_consumer.py @@ -535,9 +535,7 @@ async def _on_gql_start(self, op_id, payload): variables = payload.get("variables", {}) # Prepare a context object. - context = DictAsObject({}) - context.channels_scope = self.scope - context.channel_name = self.channel_name + context = self.get_context() context.graphql_operation_name = op_name context.graphql_operation_id = op_id @@ -1230,6 +1228,14 @@ def _spawn_background_task(self, awaitable): self._background_tasks.add(background_task) return background_task + def get_context(self): + """Prepare the context object to use during GraphQL execution.""" + context = DictAsObject({}) + context.channels_scope = self.scope + context.channel_name = self.channel_name + return context + + @property def _channel_layer(self): """Channel layer."""