from django.http.request import HttpRequest from graphene_django.views import GraphQLView from graphql import get_operation_ast, parse from sentry_sdk.api import start_transaction # For sentry perfomance monitoring # taken from https://jerrynsh.com/how-to-monitor-python-graphql-api-with-sentry/ class SentryGraphQLView(GraphQLView): def execute_graphql_request( self, request: HttpRequest, data, query, variables, operation_name, show_graphiql, ): """ adapted to use the new GraphQL 3.0 syntax, still need to get the operation type, but the code to do this changed significantly, so the above link only explains the 'what' and 'why', but no longer the 'how' """ document = parse(query) operation_type = get_operation_ast(document, operation_name).operation with start_transaction(op=str(operation_type), name=operation_name): return super().execute_graphql_request( request, data, query, variables, operation_name, show_graphiql )