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.
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.
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,
)
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.
sentry_sdk.init(
# same options as above
send_default_pii=True,
)
Note
Since send_default_pii
is a global SDK option, setting it to True
affects all integrations, not just Graphene. Please make sure to take care of scrubbing sensitive data from events before enabling this option.
- graphene: 3.3+
- Python: 3.8+
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").