Graphene

Learn about importing the Graphene GraphQL integration and how it captures GraphQL errors.

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

To get started, install sentry-sdk from PyPI.

Copied
pip install --upgrade sentry-sdk

If you have the graphene package in your dependencies, the Graphene integration will be enabled automatically when you initialize the Sentry SDK.

Configuration should happen as early as possible in your application's lifecycle.

Copied
import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # Set profiles_sample_rate to 1.0 to profile 100%
    # of sampled transactions.
    # We recommend adjusting this value in production.
    profiles_sample_rate=1.0,
)

Copied
import graphene

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

class Query(graphene.ObjectType):
    hello = graphene.String()

    def resolve_hello(self, info):
        1 / 0
        return "World"

schema = graphene.Schema(query=Query)

schema.execute("""
  query {
    hello
  }
""")

We've snuck a ZeroDivisionError into our resolve_hello resolver to ensure things are working as intended. When this code snippet is run, a new error event should appear in the Issues section of sentry.io. It will take a couple of moments for the data to appear in sentry.io.

When used together with one of our web framework integrations like FastAPI or Flask, the Graphene integration can capture the request body for each GraphQL error that happens. Since the requests may contain sensitive data, no request bodies will be attached by default.

To capture request bodies:

  • Initialize the SDK with the send_default_pii option set to True.
  • Make sure the integration for the web framework you're using is enabled.
Copied
sentry_sdk.init(
    # same options as above
    send_default_pii=True,
)

  • graphene: 3.3+
  • 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").