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.
pip install "sentry-sdk"
pip install "sentry-sdk"
uv add "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="___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
)
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
)
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
}
""")
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. This is enabled 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_collectionoption. Thegraphqlkey'sdocumentsetting controls whether the GraphQL request body is attached. If you sethttp_bodies, it must includeincoming_requestso that your web framework integration captures the body in the first place.
sentry_sdk.init(
# same options as above
data_collection={
"graphql": {"document": True},
},
)
sentry_sdk.init(
# same options as above
data_collection={
"graphql": {"document": True},
},
)
Both are enabled by default. See Data Collected for all available keys.
In SDK versions prior to 3.0, the request body isn't attached by default. To attach it, pass a data_collection dictionary as shown above, or set the send_default_pii option to True. Since send_default_pii is a global SDK option, setting it to True affects all integrations, so make sure to scrub sensitive data from events first.
- 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").