No project description provided
Project description
Rich Strawberry
This is a work in progress!
rich-strawberry
is a small add-on for the strawberry-graphql library that uses rich to print error information nicely.
Basic Example
You can use the RichLoggerExtension
to get improved traceback in your application. However, you'll need to disable the
default logger to avoid logging every exception twice.
Here's a basic example:
import logging
import strawberry
from rich_strawberry import RichLoggerExtension
logger = logging.getLogger("strawberry.execution")
logger.disabled = True
@strawberry.type
class Query:
@strawberry.field
def version(self) -> int:
raise ValueError
schema = strawberry.Schema(query=Query, extensions=[RichLoggerExtension()])
This will give you the following output in the console for the query query { version }
:
Configuration
Suppressing frames
By default, the logger uses a feature from rich
to suppress the frames from graphql
and strawberry-graphql
libraries. You can configure the list of modules for which the frames will be suppressed. For example, if you want the full traceback:
import logging
import strawberry
from rich_strawberry import RichLoggerExtension, RichGraphQLLogger
logger = logging.getLogger("strawberry.execution")
logger.disabled = True
@strawberry.type
class Query:
@strawberry.field
def version(self) -> int:
raise ValueError
debug_logger = RichGraphQLLogger(suppress_traceback_from=[])
schema = strawberry.Schema(
query=Query, extensions=[RichLoggerExtension(logger=debug_logger)]
)
Here's the full console output:
Logging context
You can also configure some values from the context to be logged on error.
import logging
import strawberry
from rich_strawberry import RichLoggerExtension, RichGraphQLLogger
logger = logging.getLogger("strawberry.execution")
logger.disabled = True
@strawberry.type
class Query:
@strawberry.field
def version(self) -> int:
raise ValueError
debug_logger = RichGraphQLLogger(log_context_keys=("request",))
schema = strawberry.Schema(
query=Query, extensions=[RichLoggerExtension(logger=debug_logger)]
)
This will use rich.inspect
to print that context value into the console:
❗ This feature is not very well tested with different integrations so it might not work as expected.
Using with subscription
At the moment of writing, subscriptions don't support extensions (check this merge request, maybe they already do!) So if you want to use the RichGraphQLLogger
for your subscriptions, you'll need
to define a custom Schema
class and overwrite its process_errors
method:
from typing import Optional
import strawberry
from strawberry.types import ExecutionContext
from graphql import GraphQLError
from rich_strawberry.logger import RichGraphQLLogger
class SchemaWithRichLogger(strawberry.Schema):
def __init__(
self,
debug_logger: Optional[RichGraphQLLogger] = None,
*args,
**kwargs,
):
super().__init__(*args, **kwargs)
self.debug_logger = debug_logger or RichGraphQLLogger()
def process_errors(
self,
errors: list[GraphQLError],
execution_context: Optional[ExecutionContext] = None,
) -> None:
self.debug_logger.print_errors(errors, execution_context)
You can see a full example with subscriptions here.
Context values aren't logged for subscriptions.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file rich-strawberry-0.2.0.tar.gz
.
File metadata
- Download URL: rich-strawberry-0.2.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.4.2 CPython/3.11.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42ebca7426ffde321ad868344ad2bd1e010e9b3bedf022f9a3d4e46f40c2b20d |
|
MD5 | e7dda1ba226f1edeec90f75a75958d75 |
|
BLAKE2b-256 | ade66148521e821163f195f7bde953c757a3df14a7e5cfec0e9e4ee4da4c35e3 |
File details
Details for the file rich_strawberry-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: rich_strawberry-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.4.2 CPython/3.11.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | daaafd69ec68f28a71cc2100c07572767b7e6da41d4b8444e259c5e3f3631e84 |
|
MD5 | 63dcf4a92f3c861d2fed2ad5590183b9 |
|
BLAKE2b-256 | c5473bf5f76de1becd9a7655900d21d3a5000f0e404c858a85d44a31dfc94329 |