async def handle_start(self, message: OperationMessage) -> None: operation_id = message["id"] payload = cast(StartPayload, message["payload"]) query = payload["query"] operation_name = payload.get("operationName") variables = payload.get("variables") context = await self.get_context() root_value = await self.get_root_value() if self.debug: pretty_print_graphql_operation(operation_name, query, variables) try: result_source = await self.schema.subscribe( query=query, variable_values=variables, operation_name=operation_name, context_value=context, root_value=root_value, ) except GraphQLError as error: error_payload = format_graphql_error(error) await self.send_message(GQL_ERROR, operation_id, error_payload) return if isinstance(result_source, GraphQLExecutionResult): assert result_source.errors error_payload = format_graphql_error(result_source.errors[0]) await self.send_message(GQL_ERROR, operation_id, error_payload) return self.subscriptions[operation_id] = result_source result_handler = self.handle_async_results(result_source, operation_id) self.tasks[operation_id] = asyncio.create_task(result_handler)
def test_pretty_print_variables(mocker): mock = mocker.patch("builtins.print") pretty_print_graphql_operation("Example", "{ query }", variables={"example": 1}) mock.assert_called_with( '{\n \x1b[38;5;28;01m"example"\x1b[39;00m: \x1b[38;5;241m1\x1b[39m\n}\n' )
def test_pretty_print_variables_object(mocker): mock = mocker.patch("builtins.print") pretty_print_graphql_operation("Example", "{ query }", variables={"example": Decimal(1)}) mock.assert_called_with( "{\n\x1b[38;5;250m " '\x1b[39m\x1b[38;5;28;01m"example"\x1b[39;00m:\x1b[38;5;250m ' "\x1b[39m\x1b[38;5;124m\"Decimal('1')\"\x1b[39m\n}\n")
async def execute( self, query, variables=None, context=None, operation_name=None, root_value=None ): if self.debug: pretty_print_graphql_operation(operation_name, query, variables) return await self.schema.execute( query, root_value=root_value, variable_values=variables, operation_name=operation_name, context_value=context, )
async def handle_subscribe(self, message: SubscribeMessage) -> None: if not self.connection_acknowledged: await self.close(code=4401, reason="Unauthorized") return if message.id in self.subscriptions.keys(): reason = f"Subscriber for {message.id} already exists" await self.close(code=4409, reason=reason) return context = await self.get_context() root_value = await self.get_root_value() if self.debug: # pragma: no cover pretty_print_graphql_operation( message.payload.operationName, message.payload.query, message.payload.variables, ) try: result_source = await self.schema.subscribe( query=message.payload.query, variable_values=message.payload.variables, operation_name=message.payload.operationName, context_value=context, root_value=root_value, ) except GraphQLError as error: payload = [format_graphql_error(error)] await self.send_message( ErrorMessage(id=message.id, payload=payload)) self.schema.process_errors([error]) return if isinstance(result_source, GraphQLExecutionResult): assert result_source.errors payload = [format_graphql_error(result_source.errors[0])] await self.send_message( ErrorMessage(id=message.id, payload=payload)) self.schema.process_errors(result_source.errors) return handler = self.handle_async_results(result_source, message.id) self.subscriptions[message.id] = result_source self.tasks[message.id] = asyncio.create_task(handler)
def test_pretty_print(mocker): mock = mocker.patch("builtins.print") pretty_print_graphql_operation("Example", "{ query }", variables={}) mock.assert_called_with("{ \x1b[38;5;125mquery\x1b[39m }\n")