Ariadne for Python

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

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

To get started, install sentry-sdk from PyPI.

pip install "sentry-sdk"

If you have the ariadne package in your dependencies, the Ariadne 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 service spans 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
)

Create a file called app.py with the following contents:

from ariadne import QueryType, gql, make_executable_schema
from ariadne.asgi import GraphQL

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

type_defs = gql(
    """
    type Query {
        hello: String!
    }
"""
)

query = QueryType()

@query.field("hello")
def resolve_hello(*_):
    1 / 0
    return "Hello!"

schema = make_executable_schema(type_defs, query)
app = GraphQL(schema, debug=True)

Make sure you have uvicorn installed:

pip install uvicorn

And finally run your GraphQL web server with:

uvicorn app:app

Open http://127.0.0.1:8000 in your browser. You should see the GraphiQL graphical user interface.

Enter { hello } into the query field then press the "Execute query" button. Your web app will be queried and will encounter the ZeroDivisionError error we've snuck into the resolve_hello resolver function.

This will create a corresponding GraphQLError in the Issues section of sentry.io. It will take a couple of moments for the data to appear in Sentry.

The Ariadne integration can capture request and response payload for each GraphQL error that happens. This is enabled by default.

To control which data is attached, use the data_collection option. The graphql key controls the query document and its variables in the request payload, and the outgoing_response entry in http_bodies controls the response payload:

sentry_sdk.init(
    # same options as above
    data_collection={
        "graphql": {"document": True, "variables": True},
        "http_bodies": ["incoming_request", "outgoing_request", "outgoing_response"],
    },
)

These are the defaults, so you only need to set the keys you want to change. See Data Collected for all available keys.

In SDK versions prior to 3.0, this data 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.

  • ariadne: 0.20+
  • 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").