Strawberry

The Strawberry integration captures errors and operations from the Strawberry GraphQL library, which can then be viewed in Sentry.

To get started, install sentry-sdk from PyPI.

Copied
pip install --upgrade sentry-sdk

Add StrawberryIntegration() to your integrations list:

Copied
import sentry_sdk
from sentry_sdk.integrations.strawberry import StrawberryIntegration

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    enable_tracing=True,
    integrations=[
        StrawberryIntegration(
            # Set async_execution to True if you have
            # at least one async resolver
            async_execution=True
        ),
    ],
)

Make sure you have all the prerequisites installed:

Copied
pip install 'strawberry-graphql[debug-server]'
pip install fastapi

Create a schema.py file with the below content:

Copied
import strawberry

sentry_sdk.init(...)  # same as above

async def resolve_hello(root) -> str:
    1 / 0
    return "Hello world!"

@strawberry.type
class Query:
    hello: str = strawberry.field(resolver=resolve_hello)

schema = strawberry.Schema(Query)

To start the web server, run:

Copied
strawberry server schema

Navigate to http://127.0.0.1:8000/graphql in your browser. You should see the GraphiQL graphical interface.

Type query HelloQuery { hello } into the query input field then press the "Execute query" button. Your web server will be queried, which should result in an exception due to the ZeroDivisionError we've snuck into the resolve_hello resolver.

This will create a GraphQLError in the Issues section as well as a transaction in the Performance section of sentry.io. It will take a couple of moments for the data to appear in sentry.io.

There are several options you will get to choose from:

Strawberry supports both synchronous and asynchronous execution of GraphQL queries. If you have at least one async resolver, you should initialize StrawberryIntegration with async_execution=True, otherwise set it to False.

The SDK will make a best-effort guess if async_execution is not provided, based on installed web frameworks.

Copied
sentry_sdk.init(
    # (...) other options
    integrations=[
        StrawberryIntegration(
            async_execution=True # or False
        ),
    ],
)

The Strawberry integration can capture request and response bodies for each GraphQL error that happens. Since these may contain sensitive data, this is the default behavior. To enable capturing request and response bodies, the SDK needs to be initialized with the send_default_pii option set to True.

Copied
sentry_sdk.init(
    # same options as above
    send_default_pii=True,
)

Strawberry comes with a (now deprecated) built-in Sentry tracing extension that this integration is built on. To prevent duplicate traces, the Sentry SDK integration will deactivate the built-in Strawberry extension if you happen to be using both.

  • strawberry-graphql: 0.209.5+
  • Python: 3.8+
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").