Graphene for Python

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 "sentry-sdk"

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

Copied
import sentry_sdk

sentry_sdk.init(
    dsn="___PUBLIC_DSN___",
    # Check https://docs.sentry.io/platforms/python/data-management/data-collected/ to configure what data is collected
    data_collection={
        "user_info": True,
    },
    # ___PRODUCT_OPTION_START___ performance
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # ___PRODUCT_OPTION_END___ performance
    # ___PRODUCT_OPTION_START___ profiling
    # To collect profiles for all profile sessions,
    # set `profile_session_sample_rate` to 1.0.
    profile_session_sample_rate=1.0,
    # Profiles will be automatically collected while
    # there is an active span.
    profile_lifecycle="trace",
    # ___PRODUCT_OPTION_END___ profiling
)

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:

  • Make sure the integration for the web framework you're using is enabled.
  • Initialize the SDK with the data_collection option. The graphql key's document setting controls whether the GraphQL request body is attached. If you set http_bodies, it must include incoming_request so that your web framework integration captures the body in the first place.
Copied
sentry_sdk.init(
    # same options as above
    data_collection={
        "graphql": {"document": True},
    },
)

Both are enabled by default when you pass a data_collection dictionary. See Data Collected for all available keys.

Alternatively, you can initialize the SDK with the send_default_pii option set to True, though send_default_pii will be removed in version 3.0 of the SDK:

Copied
sentry_sdk.init(
    # same options as above
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
)

  • graphene: 3.3+
  • Python: 3.8+
Was this helpful?
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").