Connect Graphene to Bugsink

When a Graphene resolver fails, the error normally becomes part of the GraphQL response. Bugsink can report that failure as an issue while your API keeps its usual response behavior.

This guide adds error reporting to an existing Graphene service. You’ll install and initialize the Python SDK, send a named query which fails inside a resolver, and inspect the result in Bugsink. To follow along, create a Bugsink project and copy its DSN; the quickstart guide shows where to find it.

Install the SDK

Install the open-source Sentry Python SDK in the environment which runs your Graphene service. Bugsink accepts the SDK’s error events directly, so the DSN can point at your Bugsink project:

python -m pip install --upgrade sentry-sdk

Initialize the Graphene integration

Import the SDK and Graphene integration in the module where the application is configured:

import os

import graphene
import sentry_sdk
from sentry_sdk.integrations.graphene import GrapheneIntegration

Initialize the SDK before the application starts serving requests. The SDK can discover an installed Graphene package automatically; naming the integration explicitly makes it clear which GraphQL errors this setup captures:

sentry_sdk.init(
    dsn=os.environ["SENTRY_DSN"],
    integrations=[GrapheneIntegration()],
)

Set the DSN in the environment where the application runs:

export SENTRY_DSN=https://key@bugsink.example/1

Trigger a resolver error

Resolver errors normally become entries in GraphQL’s errors array rather than escaping as HTTP server exceptions. The Graphene integration sees that result and reports it automatically; the resolver itself contains no Bugsink-specific call:

class Query(graphene.ObjectType):
    product = graphene.Field(Product, sku=graphene.String(required=True))

    @staticmethod
    def resolve_product(root, info, sku):
        item = inventory.get(sku)
        if item is None:
            raise ProductNotFound(f"Product {sku} is not in inventory") from None
        return Product(sku=sku, **item)

Start the application as usual:

uvicorn app:app

Send a named query which reaches the failing resolver:

curl --json '{"query":"query FindProduct($sku: String!) { product(sku: $sku) { sku name available } }","variables":{"sku":"graphene-missing"},"operationName":"FindProduct"}' http://127.0.0.1:8000/graphql/

Graphene keeps its normal HTTP 200 response. The response contains data.product: null and an entry in errors; that does not mean error reporting failed.

See the resolver error in Bugsink

Open your Bugsink project and select the new issue. The event points to the resolver line, includes the application message and POST request, and records the named FindProduct operation.

Bugsink preview

That’s it: resolver failures will now be reported to Bugsink while Graphene continues to control the GraphQL response.

Limitations

With the tested SDK, Graphene exposes resolver failures to the integration as GraphQLError. Bugsink therefore uses GraphQLError as the issue type rather than the custom ProductNotFound type. The application message and original resolver source line are still retained in the event.

For handled exceptions, logging, messages, and additional context options, continue with the Python guide.